|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from typing import Annotated |
| 4 | +import uvicorn |
| 5 | +from fastapi import FastAPI, APIRouter, Depends, Request |
| 6 | +from fastapi.staticfiles import StaticFiles |
| 7 | +from carthage import AsyncInjector, inject, InjectionKey, CarthagePlugin |
| 8 | +from carthage.modeling import CarthageLayout |
| 9 | + |
| 10 | +def get_ainjector(request:Request)->AsyncInjector: |
| 11 | + return request.app.state.ainjector |
| 12 | + |
| 13 | +ainjector_dependency = Annotated[AsyncInjector, Depends(get_ainjector)] |
| 14 | + |
| 15 | + |
| 16 | +api_v1 = APIRouter(prefix="/api/v1") |
| 17 | + |
| 18 | +@api_v1.get("/devices") |
| 19 | +async def get_devices(ainjector:ainjector_dependency): |
| 20 | + raise NotImplementedError |
| 21 | + |
| 22 | +@inject( |
| 23 | + layout=InjectionKey(CarthageLayout, _ready=False), |
| 24 | + plugin=InjectionKey(CarthagePlugin, name='viper-whs')) |
| 25 | +async def start_web_server(layout, plugin): |
| 26 | + app = FastAPI() |
| 27 | + app.state.ainjector = layout.ainjector |
| 28 | + app.include_router(api_v1) |
| 29 | + if (plugin.resource_dir/'../dist').exists(): |
| 30 | + breakpoint() |
| 31 | + app.mount('/', StaticFiles(directory=plugin.resource_dir/'../dist', html=True), name='frontend') |
| 32 | + config = uvicorn.Config(app, |
| 33 | + port=int(os.environ.get('PORT', 8080)), |
| 34 | + host='0.0.0.0', |
| 35 | + ) |
| 36 | + server = uvicorn.Server(config) |
| 37 | + asyncio.get_event_loop().create_task(server.serve()) |
| 38 | + |
| 39 | +web_server_key = InjectionKey('viper_whs.webserver') |
0 commit comments