1414from opengeodeweb_back .routes .create import blueprint_create
1515from opengeodeweb_microservice .database import connection
1616
17- """ Global config """
18- app : Flask = flask .Flask (__name__ )
19-
20- """ Config variables """
21- FLASK_DEBUG = True if os .environ .get ("FLASK_DEBUG" , default = None ) == "True" else False
22- if FLASK_DEBUG == False :
23- app .config .from_object (app_config .ProdConfig )
24- else :
25- app .config .from_object (app_config .DevConfig )
26- DEFAULT_HOST : str = app .config .get ("DEFAULT_HOST" ) or "localhost"
27- DEFAULT_PORT : int = int (app .config .get ("DEFAULT_PORT" ) or 5000 )
28- DEFAULT_DATA_FOLDER_PATH : str = app .config .get ("DEFAULT_DATA_FOLDER_PATH" ) or "./data"
29- ORIGINS : Any = app .config .get ("ORIGINS" )
30- TIMEOUT : int = int (app .config .get ("MINUTES_BEFORE_TIMEOUT" ) or 30 )
31- SSL : Any = app .config .get ("SSL" )
32- SECONDS_BETWEEN_SHUTDOWNS : float = float (
33- app .config .get ("SECONDS_BETWEEN_SHUTDOWNS" ) or 60.0
34- )
35-
36-
37- @app .before_request
38- def before_request () -> flask .Response | None :
39- if flask .request .method == "OPTIONS" :
40- response = flask .make_response ()
41- response .headers ["Access-Control-Allow-Methods" ] = "GET,POST,PUT,DELETE,OPTIONS"
42- return response
43- utils_functions .before_request (flask .current_app )
44- return None
45-
46-
47- @app .teardown_request
48- def teardown_request (exception : BaseException | None ) -> None :
49- utils_functions .teardown_request (flask .current_app , exception )
50-
51-
52- app .register_blueprint (
53- blueprint_routes .routes ,
54- url_prefix = "/opengeodeweb_back" ,
55- name = "opengeodeweb_back" ,
56- )
57- app .register_blueprint (
58- blueprint_models .routes ,
59- url_prefix = "/opengeodeweb_back/models" ,
60- name = "opengeodeweb_models" ,
61- )
62- app .register_blueprint (
63- blueprint_create .routes ,
64- url_prefix = "/opengeodeweb_back/create" ,
65- name = "opengeodeweb_create" ,
66- )
67-
68- if FLASK_DEBUG == False :
69- utils_functions .set_interval (
70- utils_functions .kill_task , SECONDS_BETWEEN_SHUTDOWNS , app
17+ def create_app (name : str ) -> flask .Flask :
18+ app = flask .Flask (name )
19+
20+ """ Config variables """
21+ FLASK_DEBUG = True if os .environ .get ("FLASK_DEBUG" , default = None ) == "True" else False
22+ if FLASK_DEBUG == False :
23+ app .config .from_object (app_config .ProdConfig )
24+ else :
25+ app .config .from_object (app_config .DevConfig )
26+
27+ if FLASK_DEBUG == False :
28+ SECONDS_BETWEEN_SHUTDOWNS : float = float (
29+ app .config .get ("SECONDS_BETWEEN_SHUTDOWNS" ) or 60.0
30+ )
31+ utils_functions .set_interval (
32+ utils_functions .kill_task , SECONDS_BETWEEN_SHUTDOWNS , app
33+ )
34+
35+ @app .before_request
36+ def before_request () -> flask .Response | None :
37+ if flask .request .method == "OPTIONS" :
38+ response = flask .make_response ()
39+ response .headers ["Access-Control-Allow-Methods" ] = "GET,POST,PUT,DELETE,OPTIONS"
40+ return response
41+ utils_functions .before_request (flask .current_app )
42+ return None
43+
44+ @app .teardown_request
45+ def teardown_request (exception : BaseException | None ) -> None :
46+ utils_functions .teardown_request (flask .current_app , exception )
47+
48+ @app .errorhandler (HTTPException )
49+ def errorhandler (exception : HTTPException ) -> tuple [dict [str , Any ], int ] | Response :
50+ return utils_functions .handle_exception (exception )
51+
52+
53+ @app .errorhandler (Exception )
54+ def handle_generic_exception (exception : Exception ) -> Response :
55+ print ("\033 [91mError:\033 [0m \033 [91m" + str (exception ) + "\033 [0m" , flush = True )
56+ return flask .make_response ({"description" : str (exception )}, 500 )
57+
58+
59+ @app .route (
60+ "/error" ,
61+ methods = ["POST" ],
7162 )
63+ def return_error () -> Response :
64+ flask .abort (500 , f"Test" )
65+ return flask .make_response ({}, 500 )
7266
7367
74- @app .errorhandler (HTTPException )
75- def errorhandler (exception : HTTPException ) -> tuple [dict [str , Any ], int ] | Response :
76- return utils_functions .handle_exception (exception )
68+ @app .route ("/" , methods = ["POST" ])
69+ @cross_origin ()
70+ def root () -> Response :
71+ return flask .make_response ({}, 200 )
7772
7873
79- @app .errorhandler (Exception )
80- def handle_generic_exception (exception : Exception ) -> Response :
81- print ("\033 [91mError:\033 [0m \033 [91m" + str (exception ) + "\033 [0m" , flush = True )
82- return flask .make_response ({"description" : str (exception )}, 500 )
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 )
8379
80+ return app
8481
85- @app .route (
86- "/error" ,
87- methods = ["POST" ],
88- )
89- def return_error () -> Response :
90- flask .abort (500 , f"Test" )
91- return flask .make_response ({}, 500 )
92-
93-
94- @app .route ("/" , methods = ["POST" ])
95- @cross_origin ()
96- def root () -> Response :
97- return flask .make_response ({}, 200 )
98-
99-
100- @app .route ("/kill" , methods = ["POST" ])
101- @cross_origin ()
102- def kill () -> None :
103- print ("Manual server kill, shutting down..." , flush = True )
104- os ._exit (0 )
105-
82+ def register_ogw_back_blueprints (app : flask .Flask ) -> None :
83+ app .register_blueprint (
84+ blueprint_routes .routes ,
85+ url_prefix = "/opengeodeweb_back" ,
86+ name = "opengeodeweb_back" ,
87+ )
88+ app .register_blueprint (
89+ blueprint_models .routes ,
90+ url_prefix = "/opengeodeweb_back/models" ,
91+ name = "opengeodeweb_models" ,
92+ )
93+ app .register_blueprint (
94+ blueprint_create .routes ,
95+ url_prefix = "/opengeodeweb_back/create" ,
96+ name = "opengeodeweb_create" ,
97+ )
10698
107- def run_server () -> None :
99+ def run_server (app : Flask ) -> None :
108100 parser = argparse .ArgumentParser (
109101 prog = "OpenGeodeWeb-Back" , description = "Backend server for OpenGeodeWeb"
110102 )
111- parser .add_argument ("--host" , type = str , default = DEFAULT_HOST , help = "Host to run on" )
103+ parser .add_argument ("--host" , type = str , default = app . config . get ( " DEFAULT_HOST" ) , help = "Host to run on" )
112104 parser .add_argument (
113- "-p" , "--port" , type = int , default = DEFAULT_PORT , help = "Port to listen on"
105+ "-p" , "--port" , type = int , default = app . config . get ( " DEFAULT_PORT" ) , help = "Port to listen on"
114106 )
115107 parser .add_argument (
116108 "-d" ,
117109 "--debug" ,
118- default = FLASK_DEBUG ,
110+ default = app . config . get ( " FLASK_DEBUG" ) ,
119111 help = "Whether to run in debug mode" ,
120112 action = "store_true" ,
121113 )
122114 parser .add_argument (
123115 "-dfp" ,
124116 "--data_folder_path" ,
125117 type = str ,
126- default = DEFAULT_DATA_FOLDER_PATH ,
118+ default = app . config . get ( " DEFAULT_DATA_FOLDER_PATH" ) ,
127119 help = "Path to the folder where data is stored" ,
128120 )
129121 parser .add_argument (
130122 "-ufp" ,
131123 "--upload_folder_path" ,
132124 type = str ,
133- default = DEFAULT_DATA_FOLDER_PATH ,
125+ default = app . config . get ( " DEFAULT_DATA_FOLDER_PATH" ) ,
134126 help = "Path to the folder where uploads are stored" ,
135127 )
136128 parser .add_argument (
137129 "-origins" ,
138130 "--allowed_origins" ,
139- default = ORIGINS ,
131+ default = app . config . get ( " ORIGINS" ) ,
140132 help = "Origins that are allowed to connect to the server" ,
141133 )
142134 parser .add_argument (
143135 "-t" ,
144136 "--timeout" ,
145- default = TIMEOUT ,
137+ default = app . config . get ( "MINUTES_BEFORE_TIMEOUT" ) ,
146138 help = "Number of minutes before the server times out" ,
147139 )
148140 args = parser .parse_args ()
149141 app .config .update (DATA_FOLDER_PATH = args .data_folder_path )
150142 app .config .update (
151- EXTENSIONS_FOLDER_PATH = os .path .join (args .data_folder_path , "extensions" )
143+ EXTENSIONS_FOLDER_PATH = os .path .join (str ( args .data_folder_path ) , "extensions" )
152144 )
153145 app .config .update (UPLOAD_FOLDER = args .upload_folder_path )
154146 app .config .update (MINUTES_BEFORE_TIMEOUT = args .timeout )
@@ -161,16 +153,12 @@ def run_server() -> None:
161153 )
162154
163155 db_filename : str = app .config .get ("DATABASE_FILENAME" ) or "project.db"
164- db_path = os .path .join (args .data_folder_path , db_filename )
156+ db_path = os .path .join (str ( args .data_folder_path ) , db_filename )
165157 os .makedirs (os .path .dirname (db_path ), exist_ok = True )
166158 app .config ["SQLALCHEMY_DATABASE_URI" ] = f"sqlite:///{ db_path } "
167159 app .config ["SQLALCHEMY_TRACK_MODIFICATIONS" ] = False
168160
169161 connection .init_database (db_path )
170162 print (f"Database initialized at: { db_path } " , flush = True )
171- app .run (debug = args .debug , host = args .host , port = args .port , ssl_context = SSL )
172-
173-
174- # ''' Main '''
175- if __name__ == "__main__" :
176- run_server ()
163+ app .run (debug = args .debug , host = args .host , port = args .port , ssl_context = app .config .get ("SSL" ))
164+ print ("Server stopped" , flush = True )
0 commit comments