server.py 8.2 KB

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