pizzicore.py 5.2 KB

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