feat: base flask x gunicorn server
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import typing
|
||||
|
||||
import flask
|
||||
import gunicorn.app.base
|
||||
|
||||
from app import PKG_NAME
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from app.params import Parameters
|
||||
|
||||
|
||||
class Server(gunicorn.app.base.BaseApplication):
|
||||
def __init__(self, params: Parameters) -> None:
|
||||
self.env = params.env
|
||||
self.bind = params.bind
|
||||
self.port = params.port
|
||||
self.debug = params.debug
|
||||
self.workers = params.workers
|
||||
self.timeout = params.timeout
|
||||
self.app = flask.Flask(PKG_NAME)
|
||||
super().__init__()
|
||||
|
||||
def register_routes(self) -> typing.Self:
|
||||
@self.app.route("/")
|
||||
def hello_world() -> str:
|
||||
return "<p>Hello, World!</p>"
|
||||
|
||||
return self
|
||||
|
||||
@typing.override
|
||||
def load_config(self) -> None:
|
||||
if self.cfg is not None:
|
||||
self.cfg.set("bind", f"{self.bind}:{self.port}")
|
||||
self.cfg.set("workers", self.workers)
|
||||
self.cfg.set("timeout", self.timeout)
|
||||
self.cfg.set("logger_class", "app.logs.GunicornLogger")
|
||||
self.cfg.set("errorlog", "-")
|
||||
self.cfg.set("accesslog", "-")
|
||||
|
||||
def load(self) -> flask.Flask:
|
||||
return self.app
|
||||
|
||||
def start(self) -> None:
|
||||
if self.env == "production":
|
||||
self.run()
|
||||
else:
|
||||
self.app.run(
|
||||
host=self.bind, port=self.port, debug=self.debug, load_dotenv=False
|
||||
)
|
||||
Reference in New Issue
Block a user