Switch to gunicorn , need to add config file and proper password loading
This commit is contained in:
@ -1,28 +1,58 @@
|
||||
from flask import Flask
|
||||
from typing import Callable, Dict
|
||||
from src.server.file_manager import create_filemanager_blueprint
|
||||
from gunicorn.app.base import BaseApplication
|
||||
import multiprocessing
|
||||
|
||||
|
||||
class Server:
|
||||
class Server(BaseApplication):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
debug: bool = True,
|
||||
host: str = "0.0.0.0",
|
||||
port: int = 8080,
|
||||
template_functions: Dict[str, Callable] = {},
|
||||
template_functions: Dict[str, Callable] = None,
|
||||
enable_admin_browser: bool = False,
|
||||
workers: int = multiprocessing.cpu_count() // 2 + 1,
|
||||
options=None,
|
||||
):
|
||||
if template_functions is None:
|
||||
template_functions = {}
|
||||
|
||||
self.debug = debug
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.app = Flask(__name__)
|
||||
self.application = self.app
|
||||
self.app.secret_key = "your_secret_key"
|
||||
self.options = options or {
|
||||
"bind": f"{self.host}:{self.port}",
|
||||
"reload": True, # Enable automatic reloading
|
||||
"threads": workers,
|
||||
"accesslog": "-",
|
||||
}
|
||||
for name, func in template_functions.items():
|
||||
self.register_template_function(name, func)
|
||||
|
||||
def run(self):
|
||||
self.app.debug = self.debug
|
||||
self.app.run(host=self.host, port=self.port)
|
||||
super().__init__()
|
||||
for name, func in template_functions.items():
|
||||
self.register_template_function(name, func)
|
||||
super(Server, self).__init__()
|
||||
|
||||
def register_template_function(self, name, func):
|
||||
self.app.jinja_env.globals.update({name: func})
|
||||
|
||||
def load_config(self):
|
||||
config = {
|
||||
key: value
|
||||
for key, value in self.options.items()
|
||||
if key in self.cfg.settings and value is not None
|
||||
}
|
||||
for key, value in config.items():
|
||||
self.cfg.set(key.lower(), value)
|
||||
|
||||
def load(self):
|
||||
return self.application
|
||||
|
||||
def register_route(self, route, func, defaults=None):
|
||||
self.app.add_url_rule(route, func.__name__, func, defaults=defaults)
|
||||
|
Reference in New Issue
Block a user