-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
executable file
·79 lines (59 loc) · 1.37 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python3
#
# flask
#
from flask import Flask, Response, redirect
from flask_cors import CORS
#
# std lib
#
from bson import json_util
import json
import ssl
#
# private
#
import settings
#
# app starts here!
#
application = Flask(__name__)
CORS(application)
#
# routes
#
# redirect for all posts shorthand
@application.route('/posts', methods=['GET'])
def all_posts():
return redirect('/posts/0', code=302)
# retrieve a number of posts in reverse chron
@application.route('/posts/<count>', methods=['GET'])
def posts(count):
try:
count = int(count)
except:
return Response(response='In /posts/[count]; requests, [count] must be an integer!', status=400)
if count == 0:
posts = settings.mdb.posts.find({'published': True}).sort('created_on', -1)
else:
posts = settings.mdb.posts.find({'published': True}).sort('created_on', -1).limit(count)
return Response(
response=json.dumps(list(posts), default=json_util.default),
status=200,
mimetype="application/json"
)
#
# __main__
#
if __name__ == '__main__':
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(
'/etc/letsencrypt/live/toconnell.info/fullchain.pem',
'/etc/letsencrypt/live/toconnell.info/privkey.pem',
)
application.run(
host=settings.get('api','host'),
port=settings.getint('api','port'),
debug=settings.getboolean('api','debug'),
ssl_context=context
)