2021-04-21 16:47:18 +00:00
|
|
|
"""
|
|
|
|
This is the runner for the server.
|
|
|
|
"""
|
2021-04-19 14:14:51 +00:00
|
|
|
import os
|
|
|
|
import werkzeug.middleware.proxy_fix
|
2021-04-19 14:23:50 +00:00
|
|
|
from .routes import *
|
2021-04-22 14:54:27 +00:00
|
|
|
from .database import Base, tables
|
2021-04-21 16:47:18 +00:00
|
|
|
import psycopg2
|
2021-04-22 16:46:41 +00:00
|
|
|
from .gestione import *
|
2021-04-25 13:41:27 +00:00
|
|
|
from flask_cors import CORS
|
2021-04-22 16:46:41 +00:00
|
|
|
from flask_jwt_extended import *
|
2021-04-27 14:34:55 +00:00
|
|
|
from .app import app
|
2021-05-05 20:21:04 +00:00
|
|
|
from api_spec import spec
|
|
|
|
from swagger import swagger_ui_blueprint, SWAGGER_URL
|
2021-04-19 14:14:51 +00:00
|
|
|
|
2021-04-27 14:34:55 +00:00
|
|
|
Base.init_app(app=app)
|
2021-04-27 20:48:43 +00:00
|
|
|
jwt = JWTManager(app)
|
|
|
|
cors = CORS(app)
|
|
|
|
app.config['CORS_HEADERS'] = 'Content-Type'
|
|
|
|
|
2021-04-19 14:14:51 +00:00
|
|
|
reverse_proxy_app = werkzeug.middleware.proxy_fix.ProxyFix(app=app, x_for=1, x_proto=0, x_host=1, x_port=0, x_prefix=0)
|
|
|
|
# Routes setup
|
2021-04-22 16:46:41 +00:00
|
|
|
|
2021-04-22 14:54:27 +00:00
|
|
|
app.add_url_rule("/doa", view_func=page_doa, methods=["GET", "POST"])
|
2021-04-29 19:59:05 +00:00
|
|
|
app.add_url_rule("/api/v1/login", view_func=page_login, methods=["POST"])
|
|
|
|
app.add_url_rule("/api/v1/users", view_func=page_users, methods=["GET", "POST"])
|
|
|
|
app.add_url_rule("/api/v1/users/<string:email>", view_func=page_user, methods=["GET", "PATCH", "DELETE"])
|
|
|
|
app.add_url_rule("/api/v1/repositories/", view_func=page_repositories, methods=["GET", "POST"])
|
2021-05-06 15:20:07 +00:00
|
|
|
app.add_url_rule("/api/v1/repositories/<int:rid>", view_func=page_repository, methods=["GET", "PATCH", "DELETE", "PUT"])
|
2021-05-05 20:21:04 +00:00
|
|
|
app.add_url_rule("/api/v1/repositories/<int:rid>/conditions", view_func=page_repository_conditions,
|
|
|
|
methods=["GET", "POST"])
|
2021-05-06 12:12:11 +00:00
|
|
|
app.add_url_rule("/api/v1/conditions/<int:cid>", view_func=page_condition, methods=["GET", "PATCH", "DELETE"])
|
2021-04-19 14:14:51 +00:00
|
|
|
|
2021-04-28 22:07:43 +00:00
|
|
|
app.register_error_handler(Exception, error_handler)
|
2021-05-05 20:21:04 +00:00
|
|
|
app.register_blueprint(swagger_ui_blueprint, url_prefix=SWAGGER_URL)
|
|
|
|
|
|
|
|
|
|
|
|
with app.test_request_context():
|
2021-05-06 12:12:11 +00:00
|
|
|
print(" * Getting docs ready...")
|
2021-05-05 20:21:04 +00:00
|
|
|
for fn_name in app.view_functions:
|
|
|
|
if fn_name == 'static':
|
|
|
|
continue
|
|
|
|
view_fn = app.view_functions[fn_name]
|
|
|
|
spec.path(view=view_fn)
|
2021-05-06 12:12:11 +00:00
|
|
|
print(" * Docs have been compiled on http://127.0.0.1:5000/docs")
|
2021-05-05 20:21:04 +00:00
|
|
|
|
|
|
|
|
2021-05-06 00:55:11 +00:00
|
|
|
@app.route("/docs/swagger.json")
|
2021-05-05 20:21:04 +00:00
|
|
|
def create_swagger_doc():
|
|
|
|
return jsonify(spec.to_dict())
|
|
|
|
|
2021-04-28 22:07:43 +00:00
|
|
|
|
2021-04-19 14:14:51 +00:00
|
|
|
if __name__ == "__main__":
|
2021-04-27 14:34:55 +00:00
|
|
|
with app.app_context():
|
|
|
|
Base.create_all(app=app)
|
|
|
|
if not User.query.filter_by(isAdmin=True).all():
|
2021-04-28 22:07:43 +00:00
|
|
|
Base.session.add(
|
|
|
|
User(email="admin@admin.com", password=gen_password("password"), username="admin", isAdmin=True))
|
2021-04-27 14:34:55 +00:00
|
|
|
Base.session.commit()
|
|
|
|
debug = True
|
|
|
|
if os.getenv("DISABLE_DEBUG"):
|
|
|
|
debug = False
|
2021-04-26 14:26:25 +00:00
|
|
|
app.run(debug=debug)
|