-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·64 lines (54 loc) · 1.79 KB
/
run.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
#!/usr/bin/python3
from eve import Eve
from eve_docs import eve_docs
from flask_bootstrap import Bootstrap
from flask import render_template, send_from_directory, request, Response
from functools import wraps
import os
from utils.auth import APIAuth
from settings import init
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
import tornado.options
admin_directory = os.path.dirname(os.path.abspath(__file__)) + '/admin-panel'
# start eve
app = Eve(auth=APIAuth(), static_folder=admin_directory)
init(app)
# basic protection for admin-panel
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'admin' and password == 'fr@@@nd'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
# admin-panel SPA
@app.route('/panel')
@requires_auth
def admin_panel():
return send_from_directory(admin_directory, 'index.html')
# eve_docs addon
Bootstrap(app)
app.register_blueprint(eve_docs, url_prefix='/docs')
if __name__ == '__main__':
# run
tornado.options.parse_command_line()
server = HTTPServer(WSGIContainer(app), ssl_options={
'certfile': 'api.frrand.com.crt',
'keyfile': 'api.frrand.com.key'
})
server.listen(443)
IOLoop.instance().start()