pizzicore.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import secrets
  2. import logging
  3. import dbm
  4. from collections import defaultdict
  5. from mimetypes import guess_type
  6. from asyncio.queues import Queue
  7. from fastapi import FastAPI, WebSocket, HTTPException, Depends, status, Response
  8. from starlette.websockets import WebSocketDisconnect
  9. from fastapi.security import HTTPBasic, HTTPBasicCredentials
  10. from fastapi.encoders import jsonable_encoder
  11. from fastapi.staticfiles import StaticFiles
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from pydantic import BaseModel
  14. class BaseStore:
  15. def __init__(self, n: int):
  16. self.values = {i: 0 for i in range(n)}
  17. def get(self, key) -> int:
  18. return self.values[key]
  19. def incr(self, key) -> int:
  20. newval = self.get(key) + 1
  21. return self.set(key, newval)
  22. def set(self, key, value: int) -> int:
  23. self.values[key] = value
  24. return value
  25. class PersistStoreMixin:
  26. def __init__(self, *args, **kwargs):
  27. self.db_path = kwargs.pop("db_path")
  28. super().__init__(*args, **kwargs)
  29. with dbm.open(self.db_path, "c") as db:
  30. for key in db.keys():
  31. self.values[int(key)] = int(db[key])
  32. def set(self, key, value: int) -> int:
  33. ret = super().set(key, value)
  34. with dbm.open(self.db_path, "w") as db:
  35. db[str(key)] = str(value)
  36. return ret
  37. class SignalStoreMixin:
  38. """make any BaseStore manager-aware"""
  39. def __init__(self, *args, **kwargs):
  40. manager = kwargs.pop("manager")
  41. super().__init__(*args, **kwargs)
  42. self.manager = manager
  43. def set(self, key, value: int) -> int:
  44. ret = super().set(key, value)
  45. self.manager.notify(key, value)
  46. return ret
  47. class Store(SignalStoreMixin, PersistStoreMixin, BaseStore):
  48. pass
  49. class Manager:
  50. """Handle notifications logic (for websocket)."""
  51. def __init__(self):
  52. self.registry = defaultdict(list)
  53. def subscribe(self, key, q: Queue):
  54. self.registry[key].append(q)
  55. def unsubscribe(self, key, q: Queue):
  56. try:
  57. self.registry[key].remove(q)
  58. except ValueError: # not found
  59. pass
  60. def notify(self, key, val):
  61. for queue in self.registry[key]:
  62. queue.put_nowait(val)
  63. app = FastAPI()
  64. app.add_middleware(
  65. CORSMiddleware,
  66. allow_origins=['*'],
  67. allow_credentials=True,
  68. allow_methods=["*"],
  69. allow_headers=["*"],
  70. )
  71. app.mount("/static", StaticFiles(directory="static"), name="static")
  72. manager = Manager()
  73. counter_store = Store(
  74. n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager
  75. ) # XXX: pesca da file di conf
  76. security = HTTPBasic()
  77. class Value(BaseModel):
  78. counter: int
  79. value: int
  80. def get_current_role(credentials: HTTPBasicCredentials = Depends(security)):
  81. # XXX: read user/pass from config
  82. correct_username = secrets.compare_digest(credentials.username, "avanti")
  83. correct_password = secrets.compare_digest(credentials.password, "prossimo")
  84. if not (correct_username and correct_password):
  85. raise HTTPException(
  86. status_code=status.HTTP_401_UNAUTHORIZED,
  87. detail="Incorrect username or password",
  88. headers={"WWW-Authenticate": "Basic"},
  89. )
  90. return "admin"
  91. @app.get("/v1/counter/{cid}")
  92. async def get_value(cid: int):
  93. try:
  94. val = counter_store.get(cid)
  95. except KeyError:
  96. raise HTTPException(status_code=404, detail="Item not found")
  97. else:
  98. return Value(counter=cid, value=val)
  99. @app.post("/v1/counter/{cid}/increment")
  100. async def increment(cid: int, role: str = Depends(get_current_role)):
  101. if role != "admin":
  102. raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
  103. val = counter_store.incr(cid)
  104. return Value(counter=cid, value=val)
  105. @app.websocket("/v1/ws/counter/{cid}")
  106. async def websocket_counter(websocket: WebSocket, cid: int):
  107. await websocket.accept()
  108. q: Queue = Queue()
  109. manager.subscribe(cid, q)
  110. while True:
  111. try:
  112. val = counter_store.get(cid)
  113. await websocket.send_json(jsonable_encoder(Value(counter=cid, value=val)))
  114. except WebSocketDisconnect:
  115. logging.debug("client disconnected")
  116. manager.unsubscribe(cid, q)
  117. return
  118. except Exception:
  119. logging.exception("unexpected error")
  120. manager.unsubscribe(cid, q)
  121. return
  122. await q.get()
  123. async def get_page(fname):
  124. with open(fname) as f:
  125. content = f.read()
  126. content_type, _ = guess_type(fname)
  127. return Response(content, media_type=content_type)
  128. @app.get("/")
  129. async def root_page():
  130. return await get_page("pages/index.html")
  131. @app.get("/prenota")
  132. async def prenota_page():
  133. return await get_page("pages/prenotati.html")