Skip to content
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

Moved flask app out of global space, moved routes to a class, updated… #277

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions backend/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@

from flask import Flask
from flask_cors import CORS
import config

app = Flask(__name__)
CORS(app)
from typing import Callable, Optional

from api import routes

class FlaskApp:

_flask_app = None

def get_flask_app(self) -> Flask:
if self._flask_app is None:
self._flask_app = Flask(__name__)
CORS(self._flask_app)

return self._flask_app

def add_api_route(self, rule: str, endpoint_function: Callable, methods: Optional[list[str]] = None):
flask_app = self.get_flask_app()

if methods:
flask_app.add_url_rule(rule, view_func=endpoint_function, methods=methods)
else:
flask_app.add_url_rule(rule, view_func=endpoint_function)
20 changes: 14 additions & 6 deletions backend/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
"""
import logging

import api.routes
from config import microlabConfig as config
from api.app import app
from api.routes import RouteManager
from api.server import WaitressAPIServer
from api.app import FlaskApp
from microlab.interface import MicrolabInterface


def runFlask(in_queue, out_queue):
def run_flask(in_queue, out_queue):

logging.info("### STARTING API ###")
werkzeugLogger = logging.getLogger("werkzeug")
# suppresses logging of individual requests to endpoints. Prevents log spam
werkzeugLogger.setLevel(logging.WARNING)

api.routes.microlabInterface = MicrolabInterface(in_queue, out_queue)
app.run(host="0.0.0.0", port=config.apiPort)
microlab_interface = MicrolabInterface(in_queue, out_queue)

flask_app = FlaskApp()

# This handles the routes and registering them to the flask_app instance
RouteManager(flask_app, microlab_interface)

server = WaitressAPIServer(flask_app.get_flask_app())
server.set_microlab_interface(microlab_interface)
server.run()
Loading