clan-app: init clan http api

clan-app: nix fmt
This commit is contained in:
Qubasa
2025-07-09 13:32:28 +07:00
parent 28d5294292
commit 1c269d1eaa
6 changed files with 699 additions and 13 deletions

View File

@@ -25,6 +25,9 @@ log = logging.getLogger(__name__)
class ClanAppOptions:
content_uri: str
debug: bool
http_api: bool = False
http_host: str = "127.0.0.1"
http_port: int = 8080
@profile
@@ -55,19 +58,58 @@ def app_run(app_opts: ClanAppOptions) -> int:
load_in_all_api_functions()
API.overwrite_fn(open_file)
webview = Webview(
debug=app_opts.debug, title="Clan App", size=Size(1280, 1024, SizeHint.NONE)
)
# Start HTTP API server if requested
http_server = None
if app_opts.http_api:
from clan_app.deps.http.http_server import HttpApiServer
# Add middleware to the webview
webview.add_middleware(ArgumentParsingMiddleware(api=API))
webview.add_middleware(LoggingMiddleware(log_manager=log_manager))
webview.add_middleware(MethodExecutionMiddleware(api=API))
http_server = HttpApiServer(
api=API,
log_manager=log_manager,
host=app_opts.http_host,
port=app_opts.http_port,
)
http_server.start()
# Init BAKEND_THREADS global in tasks module
tasks.BAKEND_THREADS = webview.threads
# Create webview if not running in HTTP-only mode
if not app_opts.http_api:
webview = Webview(
debug=app_opts.debug, title="Clan App", size=Size(1280, 1024, SizeHint.NONE)
)
# Add middleware to the webview
webview.add_middleware(ArgumentParsingMiddleware(api=API))
webview.add_middleware(LoggingMiddleware(log_manager=log_manager))
webview.add_middleware(MethodExecutionMiddleware(api=API))
# Create the bridge
webview.create_bridge()
# Init BAKEND_THREADS global in tasks module
tasks.BAKEND_THREADS = webview.threads
webview.bind_jsonschema_api(API, log_manager=log_manager)
webview.navigate(content_uri)
webview.run()
else:
# HTTP-only mode - keep the server running
log.info("HTTP API server running...")
log.info(
f"Available API methods at: http://{app_opts.http_host}:{app_opts.http_port}/api/methods"
)
log.info(
f"Example request: curl -X POST http://{app_opts.http_host}:{app_opts.http_port}/api/call/list_log_days"
)
log.info("Press Ctrl+C to stop the server")
try:
# Keep the main thread alive
import time
while True:
time.sleep(1)
except KeyboardInterrupt:
log.info("Shutting down HTTP API server...")
if http_server:
http_server.stop()
webview.bind_jsonschema_api(API, log_manager=log_manager)
webview.navigate(content_uri)
webview.run()
return 0