This commit is contained in:
boyska 2022-08-17 19:17:01 +02:00
parent cc9b57cc1b
commit 63154c0249

View file

@ -3,6 +3,7 @@ import logging
import dbm import dbm
from collections import defaultdict from collections import defaultdict
from mimetypes import guess_type from mimetypes import guess_type
from pathlib import Path
from asyncio.queues import Queue from asyncio.queues import Queue
from fastapi import FastAPI, WebSocket, HTTPException, Depends, status, Response from fastapi import FastAPI, WebSocket, HTTPException, Depends, status, Response
@ -11,8 +12,15 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel from pydantic import BaseModel, BaseSettings
class Settings(BaseSettings):
app_name: str = "Numeretti"
storage_dir: Path = Path("/var/lib/pizzicore")
queues_number: int = 1
class Config:
env_file = "pizzicore.env"
class BaseStore: class BaseStore:
def __init__(self, n: int): def __init__(self, n: int):
@ -34,13 +42,13 @@ class PersistStoreMixin:
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.db_path = kwargs.pop("db_path") self.db_path = kwargs.pop("db_path")
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
with dbm.open(self.db_path, "c") as db: with dbm.open(str(self.db_path), "c") as db:
for key in db.keys(): for key in db.keys():
self.values[int(key)] = int(db[key]) self.values[int(key)] = int(db[key])
def set(self, key, value: int) -> int: def set(self, key, value: int) -> int:
ret = super().set(key, value) ret = super().set(key, value)
with dbm.open(self.db_path, "w") as db: with dbm.open(str(self.db_path), "w") as db:
db[str(key)] = str(value) db[str(key)] = str(value)
return ret return ret
@ -96,9 +104,10 @@ app.add_middleware(
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")
manager = Manager() manager = Manager()
settings = Settings()
counter_store = Store( counter_store = Store(
n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager n=settings.queues_number, db_path=settings.storage_dir / "pizzicore.dbm", manager=manager
) # XXX: pesca da file di conf )
security = HTTPBasic() security = HTTPBasic()