-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/refacto web server #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
496d4b9
sql alchemy replacing flask sql alchemy
MaxNumerique f967f9b
fix(database): use SQLAlchemy database to fit new version of OGW-Micr…
MaxNumerique d324cba
removed debug logs, moved SQLAlchemy variables to Vease Back.
MaxNumerique 75ab1d1
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 5294752
Apply prepare changes
MaxNumerique 9d2c21e
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 5c3d2a5
Apply prepare changes
MaxNumerique 6cc055e
removed dep : Flask-SQLAlchemy
MaxNumerique eedd720
Apply prepare changes
MaxNumerique 2a51cbf
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique d0d5324
Apply prepare changes
MaxNumerique 74b0b3d
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 6519da6
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 1ca693b
refacto app.py & conftest
MaxNumerique e20995e
moving tests folder into src folder
MaxNumerique 582c05c
requirements
MaxNumerique 900689b
requirements ?
MaxNumerique 66162b0
test
MaxNumerique b6b6e3b
tests' paths fixed
MaxNumerique 78398b5
fix(refacto): app and app config from Vease-back
MaxNumerique 6da9952
Apply prepare changes
MaxNumerique d6f313d
revert moving tests folder
MaxNumerique b2ff0a4
Merge branch 'feat/refacto_web_server' of https://github.com/Geode-so…
MaxNumerique c0e82c4
finished moving tests, better app.py
MaxNumerique 2c9badb
Apply prepare changes
MaxNumerique 638fdcb
type app.py
MaxNumerique 2352ce4
Merge branch 'feat/refacto_web_server' of https://github.com/Geode-so…
MaxNumerique 39e2bb5
typ app.py
MaxNumerique File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Packages""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import time | ||
|
|
||
| import flask | ||
| import flask_cors | ||
| from flask_cors import cross_origin | ||
| from werkzeug.exceptions import HTTPException | ||
|
|
||
| from opengeodeweb_back import utils_functions, app_config | ||
| from opengeodeweb_back.routes import blueprint_routes | ||
| from opengeodeweb_back.routes.models import blueprint_models | ||
| from opengeodeweb_microservice.database.connection import init_database | ||
|
|
||
|
|
||
| """ Global config """ | ||
| app = flask.Flask(__name__) | ||
|
|
||
| """ Config variables """ | ||
| FLASK_DEBUG = True if os.environ.get("FLASK_DEBUG", default=None) == "True" else False | ||
|
|
||
| if FLASK_DEBUG == False: | ||
| app.config.from_object(app_config.ProdConfig) | ||
| else: | ||
| app.config.from_object(app_config.DevConfig) | ||
|
|
||
| DEFAULT_HOST = app.config.get("DEFAULT_HOST") | ||
| DEFAULT_PORT = int(app.config.get("DEFAULT_PORT")) | ||
| DEFAULT_DATA_FOLDER_PATH = app.config.get("DEFAULT_DATA_FOLDER_PATH") | ||
| ORIGINS = app.config.get("ORIGINS") | ||
| TIMEOUT = int(app.config.get("MINUTES_BEFORE_TIMEOUT")) | ||
| SSL = app.config.get("SSL") | ||
| SECONDS_BETWEEN_SHUTDOWNS = float(app.config.get("SECONDS_BETWEEN_SHUTDOWNS")) | ||
|
|
||
|
|
||
| def get_db_path_from_config(): | ||
|
MaxNumerique marked this conversation as resolved.
Outdated
|
||
| database_uri = f"{os.path.abspath( | ||
| os.path.join(app.config.get('DATA_FOLDER_PATH'), app.config.get('DATABASE_FILENAME')) | ||
| )}" | ||
| return database_uri | ||
|
|
||
|
|
||
| app.register_blueprint( | ||
| blueprint_routes.routes, | ||
| url_prefix="/opengeodeweb_back", | ||
| name="opengeodeweb_back", | ||
| ) | ||
|
|
||
| app.register_blueprint( | ||
| blueprint_models.routes, | ||
| url_prefix="/opengeodeweb_back/models", | ||
| name="opengeodeweb_models", | ||
| ) | ||
|
|
||
| if FLASK_DEBUG == False: | ||
| utils_functions.set_interval( | ||
| utils_functions.kill_task, SECONDS_BETWEEN_SHUTDOWNS, app | ||
| ) | ||
|
|
||
|
|
||
| @app.errorhandler(HTTPException) | ||
| def errorhandler(e): | ||
| return utils_functions.handle_exception(e) | ||
|
|
||
|
|
||
| @app.route( | ||
| "/error", | ||
| methods=["POST"], | ||
| ) | ||
| def return_error(): | ||
| flask.abort(500, f"Test") | ||
|
|
||
|
|
||
| @app.route("/", methods=["POST"]) | ||
| @cross_origin() | ||
| def root(): | ||
| return flask.make_response({}, 200) | ||
|
|
||
|
|
||
| @app.route("/kill", methods=["POST"]) | ||
| @cross_origin() | ||
| def kill() -> None: | ||
| print("Manual server kill, shutting down...", flush=True) | ||
| os._exit(0) | ||
|
|
||
|
|
||
| def run_server(): | ||
| parser = argparse.ArgumentParser( | ||
| prog="OpenGeodeWeb-Back", description="Backend server for OpenGeodeWeb" | ||
| ) | ||
| parser.add_argument("--host", type=str, default=DEFAULT_HOST, help="Host to run on") | ||
| parser.add_argument( | ||
| "-p", "--port", type=int, default=DEFAULT_PORT, help="Port to listen on" | ||
| ) | ||
| parser.add_argument( | ||
| "-d", | ||
| "--debug", | ||
| default=FLASK_DEBUG, | ||
| help="Whether to run in debug mode", | ||
| action="store_true", | ||
| ) | ||
| parser.add_argument( | ||
| "-dfp", | ||
| "--data_folder_path", | ||
| type=str, | ||
| default=DEFAULT_DATA_FOLDER_PATH, | ||
| help="Path to the folder where data is stored", | ||
| ) | ||
| parser.add_argument( | ||
| "-ufp", | ||
| "--upload_folder_path", | ||
| type=str, | ||
| default=DEFAULT_DATA_FOLDER_PATH, | ||
| help="Path to the folder where uploads are stored", | ||
| ) | ||
| parser.add_argument( | ||
| "-origins", | ||
| "--allowed_origins", | ||
| default=ORIGINS, | ||
| help="Origins that are allowed to connect to the server", | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--timeout", | ||
| default=TIMEOUT, | ||
| help="Number of minutes before the server times out", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| app.config.update(DATA_FOLDER_PATH=args.data_folder_path) | ||
| app.config.update(UPLOAD_FOLDER=args.upload_folder_path) | ||
| app.config.update(MINUTES_BEFORE_TIMEOUT=args.timeout) | ||
|
|
||
| flask_cors.CORS(app, origins=args.allowed_origins) | ||
|
|
||
| print( | ||
| f"Host: {args.host}, Port: {args.port}, Debug: {args.debug}, " | ||
| f"Data folder path: {args.data_folder_path}, Timeout: {args.timeout}, " | ||
| f"Origins: {args.allowed_origins}", | ||
| flush=True, | ||
| ) | ||
|
|
||
| db_path = get_db_path_from_config() | ||
| print("db_path", db_path, flush=True) | ||
| if db_path: | ||
|
MaxNumerique marked this conversation as resolved.
Outdated
|
||
| db_dir = os.path.dirname(db_path) | ||
| if db_dir and not os.path.exists(db_dir): | ||
| os.makedirs(db_dir, exist_ok=True) | ||
| init_database(db_path) | ||
| print(f"Database initialized at: {db_path}") | ||
|
|
||
| app.run(debug=args.debug, host=args.host, port=args.port, ssl_context=SSL) | ||
|
|
||
|
|
||
| # ''' Main ''' | ||
| if __name__ == "__main__": | ||
| run_server() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.