29 lines
830 B
Python
29 lines
830 B
Python
|
from flask import Flask
|
||
|
from typing import Callable, Dict
|
||
|
|
||
|
|
||
|
class Server:
|
||
|
def __init__(
|
||
|
self,
|
||
|
debug: bool = True,
|
||
|
host: str = "0.0.0.0",
|
||
|
port: int = 8080,
|
||
|
template_functions: Dict[str, Callable] = {},
|
||
|
):
|
||
|
self.debug = debug
|
||
|
self.host = host
|
||
|
self.port = port
|
||
|
self.app = Flask(__name__)
|
||
|
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)
|
||
|
|
||
|
def register_template_function(self, name, func):
|
||
|
self.app.jinja_env.globals.update({name: func})
|
||
|
|
||
|
def register_route(self, route, func, defaults=None):
|
||
|
self.app.add_url_rule(route, func.__name__, func, defaults=defaults)
|