fix: Initial barebones UI and API running

This commit is contained in:
2025-05-08 22:41:14 +12:00
parent a2942f6d7d
commit 5046ac67f1
3 changed files with 443 additions and 168 deletions

View File

@@ -3,6 +3,7 @@ from sqlalchemy import create_engine
from accounting.models import Base
from accounting.api import AccountingAPI
import os
import json
from config import DATABASE_URI
@@ -12,6 +13,16 @@ def CORS():
cherrypy.response.headers["Access-Control-Allow-Headers"] = "Content-Type"
# JSON Tools for response serialization
class JSONEncoder(object):
def __init__(self):
self.json_encoder = json.JSONEncoder()
def __call__(self, value):
# Convert the Python object to a JSON string then to bytes
return json.dumps(value).encode('utf-8')
class Root:
@cherrypy.expose
def index(self):
@@ -40,8 +51,11 @@ def main():
if not os.path.exists(static_path):
os.makedirs(static_path)
# CherryPy configuration
conf = {
# Register tools
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
# Root application config
root_conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
@@ -50,23 +64,31 @@ def main():
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': 'static',
},
'/api': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.CORS.on': True,
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
}
}
# Register CORS tool
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
# API application config
api_conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.CORS.on': True,
'tools.json_out.on': True, # Use the built-in JSON serializer
'tools.encode.on': True,
'tools.encode.encoding': 'utf-8',
# Process JSON request bodies
'request.body.processors': {
'application/json': cherrypy.lib.jsontools.json_processor
}
}
}
# Create application
# Create and mount Root application
root = Root()
root.api = AccountingAPI(db_engine)
cherrypy.tree.mount(root, '/', root_conf)
cherrypy.tree.mount(root, '/', conf)
# Create and mount API as a separate application
api = AccountingAPI(db_engine)
cherrypy.tree.mount(api, '/api', api_conf)
# Start server
cherrypy.config.update({
@@ -82,4 +104,4 @@ def main():
if __name__ == '__main__':
main()
main()