kiwi-vpn/api/kiwi_vpn_api/main.py

102 lines
2.3 KiB
Python
Raw Normal View History

#!/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.
"""
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-20 02:32:40 +00:00
from .db import Connection
2022-03-28 00:50:21 +00:00
# from .db.schemata import User
from .db_new import Capability
from .db_new import Connection as Connection_new
from .db_new import Device, User
from .routers import main_router
2022-03-18 18:22:17 +00:00
2022-03-20 03:59:25 +00:00
settings = Settings.get()
2022-03-14 00:10:10 +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",
},
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)
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-19 02:38:32 +00:00
if (current_config := await Config.load()) is not None:
2022-03-20 03:45:40 +00:00
# connect to database
2022-03-19 02:28:18 +00:00
Connection.connect(await current_config.db.db_engine)
2022-03-18 18:22:17 +00:00
2022-03-28 00:50:21 +00:00
# # some testing
# with Connection.use() as db:
# print(User.from_db(db, "admin"))
# print(User.from_db(db, "nonexistent"))
2022-03-18 17:36:44 +00:00
2022-03-28 00:50:21 +00:00
Connection_new.connect("sqlite:///tmp/v2.db")
2022-03-27 01:17:48 +00:00
2022-03-28 00:50:21 +00:00
User.create(
name="Uwe",
password_clear="ulf",
email="uwe@feh.de",
)
print(User.get(name="Uwe"))
print(User.authenticate("Uwe", "uwe"))
2022-03-27 01:17:48 +00:00
2022-03-28 00:50:21 +00:00
uwe = User.authenticate("Uwe", "ulf")
2022-03-27 13:47:18 +00:00
2022-03-28 00:50:21 +00:00
uwe.set_capabilities([Capability.admin])
uwe.update()
print(uwe.get_capabilities())
2022-03-27 01:17:48 +00:00
2022-03-28 00:50:21 +00:00
uwe.set_capabilities([])
uwe.update()
print(uwe.get_capabilities())
2022-03-27 13:47:38 +00:00
2022-03-28 00:50:21 +00:00
with Connection_new.session as db:
db.add(uwe)
print(uwe.devices)
ipad = Device.create(
owner_name="Uwe",
name="iPad",
type="tablet",
)
# ipad = Device.
print(ipad)
2022-03-27 13:47:38 +00:00
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()