tabletop-frog/ttfrog/webserver/application.py

63 lines
1.7 KiB
Python
Raw Normal View History

2024-01-28 00:46:19 -08:00
import logging
from tg import MinimalApplicationConfigurator
from tg.configurator.components.statics import StaticsConfigurationComponent
from tg.configurator.components.sqlalchemy import SQLAlchemyConfigurationComponent
from tg.util.bunch import Bunch
from wsgiref.simple_server import make_server
import webhelpers2
2024-01-28 14:31:50 -08:00
import tw2.core
2024-01-28 00:46:19 -08:00
2024-01-28 22:14:50 -08:00
from ttfrog.webserver.controllers.root import RootController
2024-01-28 00:46:19 -08:00
from ttfrog.db import db
import ttfrog.path
def app_globals():
return Bunch
def application():
"""
Create a TurboGears2 application
"""
config = MinimalApplicationConfigurator()
config.register(StaticsConfigurationComponent)
config.register(SQLAlchemyConfigurationComponent)
config.update_blueprint({
# rendering
2024-01-28 14:31:50 -08:00
'root_controller': RootController(),
2024-01-28 00:46:19 -08:00
'default_renderer': 'jinja',
'renderers': ['jinja'],
'tg.jinja_filters': {},
'auto_reload_templates': True,
# helpers
'app_globals': app_globals,
'helpers': webhelpers2,
2024-01-28 14:31:50 -08:00
'use_toscawidgets2': True,
2024-01-28 00:46:19 -08:00
# assets
'serve_static': True,
'paths': {
'static_files': ttfrog.path.static_files(),
'templates': [ttfrog.path.templates()],
},
# db
'use_sqlalchemy': True,
'sqlalchemy.url': db.url,
'model': db,
})
2024-01-28 14:31:50 -08:00
# wrap the core wsgi app in a ToscaWidgets2 app
return tw2.core.make_middleware(config.make_wsgi_app(), default_engine='jinja')
2024-01-28 00:46:19 -08:00
def start(host: str, port: int, debug: bool = False) -> None:
logging.debug(f"Configuring webserver with {host=}, {port=}, {debug=}")
make_server(host, int(port), application()).serve_forever()