1+ #
2+ # Copyright (C) 2024 RapidSilicon
3+ # Authorized use only
4+ #
15import argparse
26import os
3- import sys
47import signal
8+ import sys
59from flask import Flask , request , jsonify
610from flasgger import Swagger
711from submodule .rs_device_manager import RsDeviceManager
1620from api .utils import attrs_api
1721from api .project import project_api
1822
19- # Create Flask app
20- app = Flask (__name__ )
21- app .url_map .strict_slashes = False
22-
23- # Set up Swagger for API documentation
24- swagger_template = {
25- "swagger" : "2.0" ,
26- "info" : {
27- "title" : "RPE Backend API" ,
28- "description" : "The RPE Backend APIs consumed by the RPE frontend for power and thermal estimation of Rapid Silicon devices." ,
29- "version" : "0.1.0"
30- }
31- }
32- swagger = Swagger (app , template = swagger_template )
33-
34- # Register API blueprints with the Flask app
35- app .register_blueprint (device_api )
36- app .register_blueprint (clock_api )
37- app .register_blueprint (dsp_api )
38- app .register_blueprint (fabric_le_api )
39- app .register_blueprint (bram_api )
40- app .register_blueprint (io_api )
41- app .register_blueprint (peripherals_api )
42- app .register_blueprint (attrs_api )
43- app .register_blueprint (project_api )
44-
45- # Hook up request signal to log requests from the UI
46- @app .before_request
47- def before_request ():
48- log (f"{ request .method } { request .url } " )
49-
50- @app .after_request
51- def after_request (response ):
52- log (f"{ request .method } { request .url } { response .status_code } - DONE" )
53- return response
54-
55- # Graceful shutdown function
56- def shutdown_server ():
57- log ("Shutting down server..." )
58- func = request .environ .get ('werkzeug.server.shutdown' )
59- if func is not None :
60- func ()
61- else :
62- log ("Server shutdown function not found." , RsLogLevel .ERROR )
63-
64- # Signal handler for smooth shutdown
65- def signal_handler (signal_received , frame ):
66- log (f"Signal { signal_received } received, initiating shutdown..." )
67- shutdown_server ()
68- sys .exit (0 )
69-
70- # Register the signal handler for SIGINT (Ctrl+C) and SIGTERM
71- signal .signal (signal .SIGINT , signal_handler )
72- signal .signal (signal .SIGTERM , signal_handler )
73-
23+ #
24+ # Main entry point
25+ #
7426def main ():
75- # Parse command- line arguments
76- parser = argparse .ArgumentParser (description = 'Rapid Power Estimator REST API Server command-line arguments.' )
77- parser .add_argument ('device_file' , type = str , help = 'Path to the input device XML file' )
27+ # create and parse command line args
28+ parser = argparse .ArgumentParser (description = 'Rapid Power Estimator Rest API Server command-line arguments.' )
29+ parser .add_argument ('device_file' , type = str , help = 'Path to the input device xml file' )
7830 parser .add_argument ('--port' , type = int , default = 5000 , help = 'Specify TCP Port to use for REST server' )
7931 parser .add_argument ('--debug' , default = False , action = 'store_true' , help = 'Enable/Disable debug mode' )
8032 parser .add_argument ('--logfile' , type = str , default = "rpe.log" , help = 'Specify log file name' )
81- parser .add_argument ('--maxbytes' , type = int , default = 2048 , help = 'Specify maximum log file size in kilobytes before rollover' )
82- parser .add_argument ('--backupcount' , type = int , default = 20 , help = 'Specify number of backup log files' )
33+ parser .add_argument ('--maxbytes' , type = int , default = 2048 , help = 'Specify maximun log file size in kilobytes before rollover' )
34+ parser .add_argument ('--backupcount' , type = int , default = 20 , help = 'Specify no. of backup log files' )
8335 args = parser .parse_args ()
8436
85- # Set up logger
37+ # setup app logger
8638 log_setup (filename = args .logfile , max_bytes = args .maxbytes * 1024 , backup_count = args .backupcount )
8739
8840 # Check if the device_file exists
@@ -94,11 +46,67 @@ def main():
9446 devicemanager = RsDeviceManager .get_instance ()
9547 devicemanager .load_xml (args .device_file )
9648
97- # Log server start message
49+ # create flask app object
50+ app = Flask (__name__ )
51+ app .url_map .strict_slashes = False
52+
53+ # auto generate restapi documentation
54+ template = {
55+ "swagger" : "2.0" ,
56+ "info" : {
57+ "title" : "RPE Backend API" ,
58+ "description" : "The RPE Backend APIs which are consumed by the RPE frontend for power and thermal estimation of the Rapid Silicon devices." ,
59+ "version" : "0.1.0"
60+ }
61+ }
62+ swagger = Swagger (app , template = template )
63+
64+ # bind device api objects onto the flask app
65+ app .register_blueprint (device_api )
66+ app .register_blueprint (clock_api )
67+ app .register_blueprint (dsp_api )
68+ app .register_blueprint (fabric_le_api )
69+ app .register_blueprint (bram_api )
70+ app .register_blueprint (io_api )
71+ app .register_blueprint (peripherals_api )
72+ app .register_blueprint (attrs_api )
73+ app .register_blueprint (project_api )
74+
75+ # hook up request signal to log request by UI
76+ @app .before_request
77+ def before_request ():
78+ log (f"{ request .method } { request .url } " )
79+
80+ @app .after_request
81+ def after_request (response ):
82+ log (f"{ request .method } { request .url } { response .status_code } - DONE" )
83+ return response
84+
85+ # Graceful shutdown function
86+ def shutdown_server ():
87+ log ("Shutting down server..." )
88+ func = request .environ .get ('werkzeug.server.shutdown' )
89+ if func is not None :
90+ func ()
91+ else :
92+ log ("Server shutdown function not found." , RsLogLevel .ERROR )
93+
94+ # Signal handler for smooth shutdown
95+ def signal_handler (signal_received , frame ):
96+ log (f"Signal { signal_received } received, initiating shutdown..." )
97+ shutdown_server ()
98+ sys .exit (0 )
99+
100+ # Register the signal handler for SIGINT (Ctrl+C) and SIGTERM
101+ signal .signal (signal .SIGINT , signal_handler )
102+ signal .signal (signal .SIGTERM , signal_handler )
103+
104+ # log app server started
98105 log ("App server is running..." )
99106
100- # Start the Flask app
107+ # Start Rest API server
101108 app .run (debug = args .debug , port = args .port )
102109
110+
103111if __name__ == "__main__" :
104112 main ()
0 commit comments