2022-03-15 16:19:37 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-03-20 03:45:40 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2022-03-15 16:19:37 +00:00
|
|
|
import uvicorn
|
2022-03-18 18:22:17 +00:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
2022-04-05 21:33:48 +00:00
|
|
|
from .config import SETTINGS, Config
|
2022-03-28 02:02:12 +00:00
|
|
|
from .db import Connection, User
|
2022-03-24 23:44:51 +00:00
|
|
|
from .routers import main_router
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-24 23:44:51 +00:00
|
|
|
app = FastAPI(
|
2022-03-20 03:59:25 +00:00
|
|
|
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-04-05 21:33:48 +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-20 03:59:25 +00:00
|
|
|
)
|
|
|
|
|
2022-03-30 10:53:36 +00:00
|
|
|
app.include_router(main_router)
|
2022-03-20 03:59:25 +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-20 03:45:40 +00:00
|
|
|
# check if configured
|
2022-03-31 16:32:07 +00:00
|
|
|
if (current_config := Config.load()) is not None:
|
2022-03-20 03:45:40 +00:00
|
|
|
# connect to database
|
2022-03-28 02:00:58 +00:00
|
|
|
Connection.connect(current_config.db.uri)
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-28 02:02:12 +00:00
|
|
|
# some testing
|
|
|
|
print(User.get("admin"))
|
|
|
|
print(User.get("nonexistent"))
|
2022-03-18 17:36:44 +00:00
|
|
|
|
|
|
|
|
2022-03-18 23:45:09 +00:00
|
|
|
def main() -> None:
|
2022-03-15 16:19:37 +00:00
|
|
|
uvicorn.run(
|
2022-03-28 02:33:56 +00:00
|
|
|
app="kiwi_vpn_api.main:app",
|
2022-03-15 16:19:37 +00:00
|
|
|
host="0.0.0.0",
|
|
|
|
port=8000,
|
|
|
|
reload=True,
|
|
|
|
)
|
2022-03-14 00:10:10 +00:00
|
|
|
|
|
|
|
|
2022-03-15 16:19:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|