kiwi-vpn/api/kiwi_vpn_api/main.py
2022-03-29 16:35:41 +00:00

60 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Main executable of `kiwi-vpn-api`.
Creates the main `FastAPI` app, mounts endpoints and connects to the
configured database.
If run directly, uses `uvicorn` to run the app.
"""
import uvicorn
from fastapi import FastAPI
from .config import Config, Settings
from .db import Connection, User, UserRead
from .routers import main_router
app = 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",
},
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,
)
app.include_router(main_router, prefix=f"/{Settings._.api_v1_prefix}")
@app.on_event("startup")
async def on_startup() -> None:
# check if configured
if (current_config := Config._) is not None:
# connect to database
Connection.connect(current_config.db.uri)
# some testing
print(UserRead.from_orm(User.get("admin")))
print(User.get("nonexistent"))
def main() -> None:
uvicorn.run(
app="kiwi_vpn_api.main:app",
host="0.0.0.0",
port=8000,
reload=True,
)
if __name__ == "__main__":
main()