server.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import fastapi
  2. import fastapi.staticfiles
  3. import fastapi.responses
  4. import pathlib
  5. import os
  6. from . import *
  7. from .requests import *
  8. class server(fastapi.FastAPI):
  9. def __init__(self, config_file: str) -> None:
  10. super().__init__()
  11. location = pathlib.Path(config_file)
  12. self.__resources = config_loader(app_config) \
  13. .load(location) \
  14. .resources
  15. self.__static()
  16. self.__route_product()
  17. self.__route_users()
  18. self.__route_reservations()
  19. @property
  20. def resources(self) -> app_resources:
  21. return self.__resources
  22. @property
  23. def product_app(self) -> product_app:
  24. return self.__resources.product_app
  25. @property
  26. def users_app(self) -> users_app:
  27. return self.__resources.users_app
  28. @property
  29. def reservation_app(self) -> reservation_app:
  30. return self.__resources.reservation_app
  31. def __static(self) -> None:
  32. @self.get("/")
  33. async def root() -> str:
  34. with pathlib.Path("static/core.html").open() as core:
  35. return fastapi.responses.HTMLResponse(
  36. content = core.read(),
  37. status_code = 200
  38. )
  39. app_directory = fastapi.staticfiles.StaticFiles(
  40. directory = pathlib.Path("static/")
  41. )
  42. covers_directory = fastapi.staticfiles.StaticFiles(
  43. directory = self.resources.config.covers_path
  44. )
  45. self.mount(
  46. "/app",
  47. app_directory,
  48. name = "app_frontend"
  49. )
  50. self.mount(
  51. directory_image.server_path(),
  52. covers_directory,
  53. name = "covers"
  54. )
  55. def __route_reservations(self) -> None:
  56. @self.post("/reservations/user/email/{email}")
  57. async def reservations_email(
  58. email: str,
  59. reservation: reservation_request
  60. ) -> dict:
  61. body = reservation.dict()
  62. body["email"] = email
  63. return self.reservation_app.get_by_user(body)
  64. @self.post("/reservations/user/phone_number/{phone_number}")
  65. async def reservations_email(
  66. phone_number: str,
  67. reservation: reservation_request
  68. ) -> dict:
  69. body = reservation.dict()
  70. body["phone_number"] = phone_number
  71. return self.reservation_app.get_by_user(body)
  72. @self.post("/reservations/product/barcode/{barcode}")
  73. async def reservations_barcode(
  74. barcode: int,
  75. reservation: reservation_request
  76. ) -> dict:
  77. body = reservation.dict()
  78. body["target_barcode"] = barcode
  79. return self.reservation_app.get_by_product(body)
  80. @self.post("/reservations/product/name/{name}")
  81. async def reservations_name(
  82. name: str,
  83. reservation: reservation_request
  84. ) -> dict:
  85. body = reservation.dict()
  86. body["target_name"] = name
  87. return self.reservation_app.get_by_product(body)
  88. @self.post("/rent/product/barcode/{barcode}")
  89. async def rent_barcode(
  90. barcode: int,
  91. reservation: reservation_request
  92. ) -> dict:
  93. body = reservation.dict()
  94. body["target_barcode"] = barcode
  95. return self.reservation_app.rent_product(body)
  96. @self.post("/rent/product/name/{name}")
  97. async def rent_name(
  98. name: str,
  99. reservation: reservation_request
  100. ) -> dict:
  101. body = reservation.dict()
  102. body["target_name"] = name
  103. return self.reservation_app.rent_product(body)
  104. @self.post("/give_back/product/barcode/{barcode}")
  105. async def give_back_barcode(
  106. barcode: int,
  107. reservation: reservation_request
  108. ) -> dict:
  109. body = reservation.dict()
  110. body["target_barcode"] = barcode
  111. return self.reservation_app.give_back_product(body)
  112. @self.post("/give_back/product/name/{name}")
  113. async def give_back_barcode(
  114. name: str,
  115. reservation: reservation_request
  116. ) -> dict:
  117. body = reservation.dict()
  118. body["target_name"] = name
  119. return self.reservation_app.give_back_product(body)
  120. def __route_product(self) -> None:
  121. @self.get("/products")
  122. async def products() -> dict:
  123. return self.product_app.all()
  124. @self.get("/product/get/barcode/{barcode}")
  125. async def product_get_barcode(barcode: int) -> dict:
  126. return self.product_app.get_barcode(str(barcode))
  127. @self.get("/product/get/name/{name}")
  128. async def product_get_name(name: str) -> dict:
  129. return self.product_app.get_name(name)
  130. @self.get("/product/search/name/{name}")
  131. async def product_search_name(name: str) -> dict:
  132. return self.product_app.search_name(name)
  133. @self.get("/product/search/author/{author}")
  134. async def product_search_author(author: str) -> dict:
  135. return self.product_app.search_author(author)
  136. @self.get("/product/check/barcode/{barcode}")
  137. async def product_check_barcode(barcode: int) -> dict:
  138. return self.product_app.check_barcode(str(barcode))
  139. @self.get("/product/check/name/{name}")
  140. async def product_check_name(name: str) -> dict:
  141. return self.product_app.check_name(name)
  142. @self.post("/product/update/barcode/{barcode}")
  143. async def product_barcode_update(
  144. barcode: str,
  145. product: product_update_request
  146. ) -> dict:
  147. body = product.dict()
  148. body["target_barcode"] = barcode
  149. return self.product_app.update(body)
  150. @self.post("/product/update/name/{name}")
  151. async def product_name_update(
  152. name: str,
  153. product: product_update_request
  154. ) -> dict:
  155. body = product.dict()
  156. body["target_name"] = name
  157. return self.product_app.update(body)
  158. @self.post("/product/update/image/barcode/{barcode}")
  159. async def product_image_barcode_update(
  160. barcode: str,
  161. product_image: product_update_image_request
  162. ) -> dict:
  163. body = product_image.dict()
  164. body["target_barcode"] = barcode
  165. return self.product_app.update_image(body)
  166. @self.post("/product/update/image/name/{name}")
  167. async def product_image_barcode_update(
  168. name: str,
  169. product_image: product_update_image_request
  170. ) -> dict:
  171. body = product_image.dict()
  172. body["target_name"] = name
  173. return self.product_app.update_image(body)
  174. @self.delete("/product/barcode/{barcode}")
  175. async def product_barcode_delete(
  176. barcode: str,
  177. request: apikey_request
  178. ) -> dict:
  179. body = request.dict()
  180. body["target_barcode"] = barcode
  181. return self.product_app.delete(body)
  182. @self.delete("/product/name/{name}")
  183. async def product_name_delete(
  184. name: str,
  185. request: apikey_request
  186. ) -> dict:
  187. body = request.dict()
  188. body["target_name"] = name
  189. return self.product_app.delete(body)
  190. @self.post("/product/create")
  191. async def product_create(product: product_create_request) -> dict:
  192. return self.product_app.create(product.dict())
  193. def __route_users(self) -> None:
  194. @self.post("/user/login")
  195. async def user_login(user: user_login_request) -> dict:
  196. return self.users_app.login(user.nick, user.password)
  197. @self.post("/user")
  198. async def user_get(body: user_get_request) -> dict:
  199. return self.users_app.get(body.apikey)
  200. instance = server(os.environ["config_file"])