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