-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLind.py
96 lines (74 loc) · 2.19 KB
/
Lind.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from os.path import dirname, join
from sys import version_info
from flask import Flask, render_template, request
from waitress import create_server
from backend.db import close_db, maintain_db, setup_db
from frontend.api import api
from frontend.ui import ui
HOST = "0.0.0.0"
PORT = "8080"
THREADS = 10
def _folder_path(*folders):
"""Receive absolute path of file or folder
Returns:
str: Absolute path of combination of *folders
"""
return join(dirname(__file__), *folders)
def create_app() -> Flask:
"""Create the app instance for the web server
Returns:
Flask: The app instance
"""
app = Flask(
__name__,
template_folder=_folder_path("frontend", "templates"),
static_folder=_folder_path("frontend", "static"),
static_url_path="/static"
)
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
# add error handlers
@app.errorhandler(404)
def not_found(e):
if request.path.startswith("/api"):
return {"error": "NotFound", "result": {}}, 404
else:
return render_template("page_not_found.html")
@app.errorhandler(400)
def bad_request(e):
return {"error": "Bad request", "result": {}}, 400
@app.errorhandler(405)
def method_not_allowed(e):
return {"error": "Method not allowed", "result": {}}, 405
@app.errorhandler(500)
def internal_error(e):
return {"error": "Internal error", "result": {}}, 500
app.register_blueprint(ui)
app.register_blueprint(api, url_prefix="/api")
# setup database
app.teardown_appcontext(close_db)
return app
def Lind() -> None:
"""Main function of Lind to run the software.
"""
# check python version
if (version_info.major < 3) or (version_info.major == 3 and version_info.minor < 7):
print(
f"Error: the minimum python version required is python3.7 (currently {version_info.major}.{version_info.minor}.{version_info.micro})"
)
app = create_app()
# setup database
with app.app_context():
setup_db()
# create server instance
server = create_server(app, host=HOST, port=PORT, threads=THREADS)
# start application
print(f"Lind running on http://{HOST}:{PORT}/")
server.run()
# shutdown application
maintain_db().maintain()
print("\nBye!")
return
if __name__ == "__main__":
Lind()