[+] New blueprint method

[+] Allows subdomain filtering for routes
[+] Allows routes scalability

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-06-29 17:31:47 +01:00
parent ef6bc53269
commit 71d7833bd7
2 changed files with 5 additions and 3 deletions

View File

@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
function sendRequest(endpoint, payload) { function sendRequest(endpoint, payload) {
return fetch(`http://192.168.1.94:3000/${endpoint}`, { return fetch(`http://api.servii.fr:3000/${endpoint}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

6
app.py
View File

@ -1,7 +1,7 @@
import http import http
import inspect import inspect
from flask import Flask, Response, jsonify, request from flask import Blueprint, Flask, Response, jsonify, request
from flask_cors import CORS from flask_cors import CORS
import firebase_manager import firebase_manager
@ -10,6 +10,7 @@ import generic_executor
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
cors = CORS(app, origins=['*']) cors = CORS(app, origins=['*'])
apiBP = Blueprint('api', 'api', subdomain="api")
def generic_response_maker(status_code: http.HTTPStatus, _message: str = None) -> tuple[Response, int]: def generic_response_maker(status_code: http.HTTPStatus, _message: str = None) -> tuple[Response, int]:
@ -90,7 +91,7 @@ route_handlers = {
} }
@app.route('/<path:path>', methods=['POST']) @apiBP.route('/<path:path>', methods=['POST'])
def dynamic_route_handler(path): def dynamic_route_handler(path):
if path not in route_handlers: if path not in route_handlers:
return generic_response_maker(http.HTTPStatus.METHOD_NOT_ALLOWED) return generic_response_maker(http.HTTPStatus.METHOD_NOT_ALLOWED)
@ -114,4 +115,5 @@ def dynamic_route_handler(path):
if __name__ == '__main__': if __name__ == '__main__':
ssl_context = ('/fullchain.pem', '/privkey.pem') ssl_context = ('/fullchain.pem', '/privkey.pem')
app.register_blueprint(apiBP)
app.run(host='0.0.0.0', port=3000, debug=False) app.run(host='0.0.0.0', port=3000, debug=False)