|
| 1 | +"""Packages""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import time |
| 6 | + |
| 7 | +import flask |
| 8 | +import flask_cors |
| 9 | +from flask_cors import cross_origin |
| 10 | +from werkzeug.exceptions import HTTPException |
| 11 | + |
| 12 | +from src.opengeodeweb_back import utils_functions, app_config |
| 13 | +from src.opengeodeweb_back.routes import blueprint_routes |
| 14 | +from src.opengeodeweb_back.routes.models import blueprint_models |
| 15 | +from opengeodeweb_microservice.database.connection import init_database |
| 16 | + |
| 17 | + |
| 18 | +""" Global config """ |
| 19 | +app = flask.Flask(__name__) |
| 20 | + |
| 21 | +""" Config variables """ |
| 22 | +FLASK_DEBUG = True if os.environ.get("FLASK_DEBUG", default=None) == "True" else False |
| 23 | + |
| 24 | +if FLASK_DEBUG == False: |
| 25 | + app.config.from_object(app_config.ProdConfig) |
| 26 | +else: |
| 27 | + app.config.from_object(app_config.DevConfig) |
| 28 | + |
| 29 | +DEFAULT_HOST = app.config.get("DEFAULT_HOST") |
| 30 | +DEFAULT_PORT = int(app.config.get("DEFAULT_PORT")) |
| 31 | +DEFAULT_DATA_FOLDER_PATH = app.config.get("DEFAULT_DATA_FOLDER_PATH") |
| 32 | +ORIGINS = app.config.get("ORIGINS") |
| 33 | +TIMEOUT = int(app.config.get("MINUTES_BEFORE_TIMEOUT")) |
| 34 | +SSL = app.config.get("SSL") |
| 35 | +SECONDS_BETWEEN_SHUTDOWNS = float(app.config.get("SECONDS_BETWEEN_SHUTDOWNS")) |
| 36 | + |
| 37 | + |
| 38 | +def get_db_path_from_config(): |
| 39 | + database_uri = f"{os.path.abspath( |
| 40 | + os.path.join(app.config.get('DATA_FOLDER_PATH'), app.config.get('DATABASE_FILENAME')) |
| 41 | + )}" |
| 42 | + return database_uri |
| 43 | + |
| 44 | + |
| 45 | +app.register_blueprint( |
| 46 | + blueprint_routes.routes, |
| 47 | + url_prefix="/opengeodeweb_back", |
| 48 | + name="opengeodeweb_back", |
| 49 | +) |
| 50 | + |
| 51 | +app.register_blueprint( |
| 52 | + blueprint_models.routes, |
| 53 | + url_prefix="/opengeodeweb_back/models", |
| 54 | + name="opengeodeweb_models", |
| 55 | +) |
| 56 | + |
| 57 | +if FLASK_DEBUG == False: |
| 58 | + utils_functions.set_interval( |
| 59 | + utils_functions.kill_task, SECONDS_BETWEEN_SHUTDOWNS, app |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@app.errorhandler(HTTPException) |
| 64 | +def errorhandler(e): |
| 65 | + return utils_functions.handle_exception(e) |
| 66 | + |
| 67 | + |
| 68 | +@app.route("/", methods=["POST"]) |
| 69 | +@cross_origin() |
| 70 | +def root(): |
| 71 | + return flask.make_response({}, 200) |
| 72 | + |
| 73 | + |
| 74 | +@app.route("/kill", methods=["POST"]) |
| 75 | +@cross_origin() |
| 76 | +def kill() -> None: |
| 77 | + print("Manual server kill, shutting down...", flush=True) |
| 78 | + os._exit(0) |
| 79 | + |
| 80 | + |
| 81 | +def run_server(): |
| 82 | + parser = argparse.ArgumentParser( |
| 83 | + prog="OpenGeodeWeb-Back", description="Backend server for OpenGeodeWeb" |
| 84 | + ) |
| 85 | + parser.add_argument("--host", type=str, default=DEFAULT_HOST, help="Host to run on") |
| 86 | + parser.add_argument( |
| 87 | + "-p", "--port", type=int, default=DEFAULT_PORT, help="Port to listen on" |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "-d", |
| 91 | + "--debug", |
| 92 | + default=FLASK_DEBUG, |
| 93 | + help="Whether to run in debug mode", |
| 94 | + action="store_true", |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + "-dfp", |
| 98 | + "--data_folder_path", |
| 99 | + type=str, |
| 100 | + default=DEFAULT_DATA_FOLDER_PATH, |
| 101 | + help="Path to the folder where data is stored", |
| 102 | + ) |
| 103 | + parser.add_argument( |
| 104 | + "-ufp", |
| 105 | + "--upload_folder_path", |
| 106 | + type=str, |
| 107 | + default=DEFAULT_DATA_FOLDER_PATH, |
| 108 | + help="Path to the folder where uploads are stored", |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "-origins", |
| 112 | + "--allowed_origins", |
| 113 | + default=ORIGINS, |
| 114 | + help="Origins that are allowed to connect to the server", |
| 115 | + ) |
| 116 | + parser.add_argument( |
| 117 | + "-t", |
| 118 | + "--timeout", |
| 119 | + default=TIMEOUT, |
| 120 | + help="Number of minutes before the server times out", |
| 121 | + ) |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + app.config.update(DATA_FOLDER_PATH=args.data_folder_path) |
| 125 | + app.config.update(UPLOAD_FOLDER=args.upload_folder_path) |
| 126 | + app.config.update(MINUTES_BEFORE_TIMEOUT=args.timeout) |
| 127 | + |
| 128 | + flask_cors.CORS(app, origins=args.allowed_origins) |
| 129 | + |
| 130 | + print( |
| 131 | + f"Host: {args.host}, Port: {args.port}, Debug: {args.debug}, " |
| 132 | + f"Data folder path: {args.data_folder_path}, Timeout: {args.timeout}, " |
| 133 | + f"Origins: {args.allowed_origins}", |
| 134 | + flush=True, |
| 135 | + ) |
| 136 | + |
| 137 | + db_path = get_db_path_from_config() |
| 138 | + print("db_path", db_path, flush=True) |
| 139 | + if db_path: |
| 140 | + db_dir = os.path.dirname(db_path) |
| 141 | + if db_dir and not os.path.exists(db_dir): |
| 142 | + os.makedirs(db_dir, exist_ok=True) |
| 143 | + init_database(db_path) |
| 144 | + print(f"Database initialized at: {db_path}") |
| 145 | + |
| 146 | + app.run(debug=args.debug, host=args.host, port=args.port, ssl_context=SSL) |
| 147 | + |
| 148 | + |
| 149 | +# ''' Main ''' |
| 150 | +if __name__ == "__main__": |
| 151 | + run_server() |
0 commit comments