kiwi-vpn/api/kiwi_vpn_api/main.py

58 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import uvicorn
2022-03-18 18:22:17 +00:00
from fastapi import FastAPI
2022-03-18 22:43:02 +00:00
from .config import Config, Settings
2022-03-18 23:45:09 +00:00
from .db import Connection, schemas
2022-03-18 23:04:28 +00:00
from .routers import admin, user
2022-03-18 18:22:17 +00:00
2022-03-18 22:43:02 +00:00
settings = Settings.get()
2022-03-15 17:38:24 +00:00
2022-03-17 17:06:00 +00:00
api = FastAPI(
title="kiwi-vpn API",
description="This API enables the `kiwi-vpn` service.",
contact={
"name": "Jörn-Michael Miehe",
"email": "40151420+ldericher@users.noreply.github.com",
},
license_info={
"name": "MIT License",
"url": "https://opensource.org/licenses/mit-license.php",
},
2022-03-18 22:43:02 +00:00
openapi_url=settings.openapi_url,
docs_url=settings.docs_url if not settings.production_mode else None,
redoc_url=settings.redoc_url if not settings.production_mode else None,
)
2022-03-14 00:10:10 +00:00
app = FastAPI()
app.mount("/api", api)
2022-03-18 23:45:09 +00:00
api.include_router(admin.router)
api.include_router(user.router)
2022-03-14 00:10:10 +00:00
2022-03-18 18:22:17 +00:00
2022-03-18 23:45:09 +00:00
@app.on_event("startup")
async def on_startup() -> None:
2022-03-18 22:43:02 +00:00
if (current_config := await Config.get()) is not None:
2022-03-18 23:04:28 +00:00
Connection.connect(current_config.db_engine)
2022-03-18 18:22:17 +00:00
# some testing
2022-03-18 23:45:09 +00:00
async for db in Connection.get():
print(schemas.User.get(db, "admin"))
print(schemas.User.get(db, "nonexistent"))
2022-03-18 17:36:44 +00:00
2022-03-18 23:45:09 +00:00
def main() -> None:
uvicorn.run(
"kiwi_vpn_api.main:app",
host="0.0.0.0",
port=8000,
reload=True,
)
2022-03-14 00:10:10 +00:00
if __name__ == "__main__":
main()