You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.8 KiB

  1. from fastapi import FastAPI, Depends
  2. from starlette.requests import Request
  3. from .server_data import ServerData
  4. from .database.db import database
  5. # from .utils.dependencies import right_checkers, get_current_user
  6. from .utils.dependencies import get_current_user
  7. from .utils.responses import (
  8. ResponseStructure,
  9. get_base_url,
  10. validate_response,
  11. )
  12. from .utils.users import check_user_rights
  13. from .schemas.responses import GetRootResponse
  14. from .schemas.users import User
  15. from .routers.users import router as users_router
  16. from .routers.commands import router as commands_router
  17. # TODO
  18. # 1. Добавить список отклоняемых токенов и их обработку.
  19. # 2. Добавить предварительный вариант генерации access_tokens.
  20. # 3. Продумать, наконец, концепцию cid и wid, в ключе их применения при
  21. # настройке команд и запуске воркеров.
  22. # 4. Решить сколько команд может запускать пользователь единовременно.
  23. # 5. Добавить способ указания принадлежности воркеров и команд пользователям.
  24. # 6. Разобраться с местом раcположения базы данных.
  25. data = ServerData()
  26. app = FastAPI()
  27. @app.on_event("startup")
  28. async def startup():
  29. await database.connect()
  30. @app.on_event("shutdown")
  31. async def shutdown():
  32. await database.disconnect()
  33. @app.get("/", response_model=GetRootResponse, tags=["Home"])
  34. async def get_primary_commands(request: Request,
  35. user: User = Depends(get_current_user)):
  36. check_user_rights(user, "read")
  37. resp = ResponseStructure(get_base_url(request))
  38. resp.add_data(name="Calculate Server", version="0.1")
  39. resp.add_link("commands", "/commands?gui={is_gui}", templated=True)
  40. resp.add_link("find_command", "/commands/{console_command}?gui={is_gui}",
  41. templated=True)
  42. resp.add_link("workers", "/workers")
  43. return validate_response(resp.get_dict(), GetRootResponse,
  44. media_type="application/hal+json")
  45. # @app.get("/workers/{wid}", tags=["Workers management"],
  46. # dependencies=[Depends(right_checkers["write"])])
  47. # async def get_worker(wid: int):
  48. # '''Тестовый обработчик.'''
  49. # worker = data.get_worker_object(wid=wid)
  50. # worker.run()
  51. # print(f"worker: {type(worker)} object {worker}")
  52. # await worker.send({"text": "INFO"})
  53. # worker_data = await worker.get()
  54. # if worker_data['type'] == 'log':
  55. # data.log_message[data['level']](data['msg'])
  56. # return worker_data
  57. # Authentification and users management.
  58. app.include_router(users_router)
  59. # Commands running and management.
  60. app.include_router(commands_router)