-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
250 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pathlib | ||
from typing import Any, Dict, List, Optional | ||
|
||
from pydantic import BaseSettings, HttpUrl, PostgresDsn, validator | ||
from pydantic.networks import AnyHttpUrl | ||
|
||
from trapdata.cli import read_settings | ||
from trapdata.settings import Settings as BaseSettings | ||
|
||
|
||
class Settings(BaseSettings): | ||
PROJECT_NAME: str = "AMI Data Manager" | ||
|
||
SENTRY_DSN: Optional[HttpUrl] = None | ||
|
||
API_PATH: str = "/api/v1" | ||
|
||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 7 * 24 * 60 # 7 days | ||
|
||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] | ||
|
||
# The following variables need to be defined in environment | ||
|
||
TEST_DATABASE_URL: Optional[PostgresDsn] | ||
|
||
SECRET_KEY: str | ||
# END: required environment variables | ||
|
||
# STATIC_ROOT: str = "static" | ||
|
||
# @validator("STATIC_ROOT") | ||
# def validate_static_root(cls, v): | ||
# path = cls.user_data_path / v | ||
# path.mkdir(parents=True, exist_ok=True) | ||
# return path | ||
|
||
|
||
# settings = read_settings(SettingsClass=Settings, SECRET_KEY="secret") | ||
settings = Settings(SECRET_KEY="secret") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from fastapi import FastAPI | ||
from fastapi.routing import APIRoute | ||
from fastapi.staticfiles import StaticFiles | ||
from starlette.middleware.cors import CORSMiddleware | ||
from starlette.requests import Request | ||
from starlette.responses import FileResponse, RedirectResponse | ||
|
||
from trapdata.api.config import settings | ||
from trapdata.api.views import api_router | ||
|
||
|
||
def create_app(): | ||
description = f"{settings.PROJECT_NAME} API" | ||
app = FastAPI( | ||
title=settings.PROJECT_NAME, | ||
openapi_url=f"{settings.API_PATH}/openapi.json", | ||
docs_url="/docs/", | ||
description=description, | ||
redoc_url="/redoc/", | ||
) | ||
setup_routers(app) | ||
setup_cors_middleware(app) | ||
serve_static_app(app) | ||
return app | ||
|
||
|
||
def setup_routers(app: FastAPI) -> None: | ||
app.include_router(api_router, prefix=settings.API_PATH) | ||
# The following operation needs to be at the end of this function | ||
use_route_names_as_operation_ids(app) | ||
|
||
|
||
def serve_static_app(app): | ||
app.mount( | ||
"/static/crops", | ||
StaticFiles(directory=settings.user_data_path / "crops"), | ||
name="crops", | ||
) | ||
app.mount( | ||
"/", | ||
StaticFiles(directory="trapdata/webui/public"), | ||
name="static", | ||
) | ||
|
||
@app.middleware("http") | ||
async def _add_404_middleware(request: Request, call_next): | ||
"""Serves static assets on 404""" | ||
response = await call_next(request) | ||
path = request["path"] | ||
if path.startswith(settings.API_PATH) or path.startswith("/docs"): | ||
return response | ||
if response.status_code == 404: | ||
return FileResponse("trapdata/webui/public/index.html") | ||
return response | ||
|
||
|
||
def setup_cors_middleware(app): | ||
if settings.BACKEND_CORS_ORIGINS: | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
expose_headers=["Content-Range", "Range"], | ||
allow_headers=["Authorization", "Range", "Content-Range"], | ||
) | ||
|
||
|
||
def use_route_names_as_operation_ids(app: FastAPI) -> None: | ||
""" | ||
Simplify operation IDs so that generated API clients have simpler function | ||
names. | ||
Should be called only after all routes have been added. | ||
""" | ||
route_names = set() | ||
for route in app.routes: | ||
if isinstance(route, APIRoute): | ||
if route.name in route_names: | ||
raise Exception("Route function names should be unique") | ||
route.operation_id = route.name | ||
route_names.add(route.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from trapdata import logger | ||
from trapdata.api.factory import create_app | ||
|
||
app = create_app() | ||
|
||
|
||
def run(): | ||
import uvicorn | ||
|
||
logger.info("Starting uvicorn in reload mode") | ||
uvicorn.run( | ||
"main:app", | ||
host="0.0.0.0", | ||
reload=True, | ||
port=int("8000"), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from fastapi import APIRouter | ||
|
||
from trapdata.api.views import deployments, stats | ||
|
||
api_router = APIRouter() | ||
|
||
api_router.include_router(stats.router, tags=["stats"]) | ||
api_router.include_router(deployments.router, tags=["deployments"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Any, List, Optional | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy import func, orm, select | ||
from starlette.responses import Response | ||
|
||
from trapdata.api.config import settings | ||
from trapdata.api.deps.db import get_session | ||
from trapdata.api.deps.request_params import parse_react_admin_params | ||
from trapdata.api.request_params import RequestParams | ||
from trapdata.db import Base | ||
from trapdata.db.models.deployments import DeploymentListItem, list_deployments | ||
|
||
router = APIRouter(prefix="/deployments") | ||
|
||
|
||
@router.get("", response_model=List[DeploymentListItem]) | ||
async def get_deployments( | ||
response: Response, | ||
session: orm.Session = Depends(get_session), | ||
# request_params: RequestParams = Depends(parse_react_admin_params(Base)), | ||
) -> Any: | ||
deployments = list_deployments(session) | ||
return deployments | ||
|
||
|
||
@router.post("/process", response_model=List[DeploymentListItem]) | ||
async def process_deployment( | ||
response: Response, | ||
session: orm.Session = Depends(get_session), | ||
# request_params: RequestParams = Depends(parse_react_admin_params(Base)), | ||
) -> Any: | ||
from trapdata.ml.pipeline import start_pipeline | ||
|
||
start_pipeline( | ||
session=session, image_base_path=settings.image_base_path, settings=settings | ||
) | ||
deployments = list_deployments(session) | ||
return deployments |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:) |