selfprivacy-rest-api/selfprivacy_api/app.py

61 lines
1.3 KiB
Python
Raw Normal View History

2021-11-11 18:31:28 +00:00
#!/usr/bin/env python3
2021-11-16 16:14:01 +00:00
"""SelfPrivacy server management API"""
import logging
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from strawberry.fastapi import GraphQLRouter
2024-05-15 11:29:20 +00:00
from strawberry.subscriptions import GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL
2021-12-06 06:48:29 +00:00
import uvicorn
2021-12-06 06:48:29 +00:00
from selfprivacy_api.dependencies import get_api_version
2022-06-24 17:12:32 +00:00
from selfprivacy_api.graphql.schema import schema
from selfprivacy_api.migrations import run_migrations
2022-06-24 13:05:18 +00:00
2021-11-11 18:31:28 +00:00
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=getattr(logging, log_level, logging.INFO), format="%(levelname)s: %(message)s"
)
app = FastAPI()
2021-11-11 18:31:28 +00:00
2024-05-15 11:29:20 +00:00
graphql_app: GraphQLRouter = GraphQLRouter(
schema,
2024-05-15 11:29:20 +00:00
subscription_protocols=[
GRAPHQL_TRANSPORT_WS_PROTOCOL,
GRAPHQL_WS_PROTOCOL,
],
)
2021-11-16 16:14:01 +00:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2021-11-11 18:31:28 +00:00
app.include_router(graphql_app, prefix="/graphql")
2021-11-16 16:14:01 +00:00
@app.get("/api/version")
async def get_version():
"""Get the version of the server"""
return {"version": get_api_version()}
2022-06-24 13:05:18 +00:00
2021-11-16 16:14:01 +00:00
@app.on_event("startup")
async def startup():
run_migrations()
2021-11-11 18:31:28 +00:00
if __name__ == "__main__":
2022-08-26 17:01:14 +00:00
uvicorn.run(
"selfprivacy_api.app:app", host="127.0.0.1", port=5050, log_level="info"
)