diff --git a/pizzicore/docker-compose.yaml b/pizzicore/docker-compose.yaml index 811aa62..8c1721b 100644 --- a/pizzicore/docker-compose.yaml +++ b/pizzicore/docker-compose.yaml @@ -6,5 +6,8 @@ services: # XXX: prima o poi servirà una qualche /var/lib/ in cui tenere il contatore volumes: - .:/src/ + - lib:/var/lib/pizzicore/ ports: - 8000:8000 +volumes: + lib: diff --git a/pizzicore/pizzicore.py b/pizzicore/pizzicore.py index c5b06bf..30539b1 100644 --- a/pizzicore/pizzicore.py +++ b/pizzicore/pizzicore.py @@ -1,4 +1,5 @@ import secrets +import dbm from collections import defaultdict from asyncio.queues import Queue @@ -30,7 +31,22 @@ class BaseStore: return value -class SignalStore: +class PersistStoreMixin: + def __init__(self, *args, **kwargs): + self.db_path = kwargs.pop("db_path") + super().__init__(*args, **kwargs) + with dbm.open(self.db_path, "c") as db: + for key in db.keys(): + self.values[int(key)] = int(db[key]) + + def set(self, key, value: int) -> int: + ret = super().set(key, value) + with dbm.open(self.db_path, "w") as db: + db[str(key)] = str(value) + return ret + + +class SignalStoreMixin: """make any BaseStore manager-aware""" def __init__(self, *args, **kwargs): @@ -44,7 +60,7 @@ class SignalStore: return ret -class Store(SignalStore, BaseStore): +class Store(SignalStoreMixin, PersistStoreMixin, BaseStore): pass @@ -70,7 +86,9 @@ class Manager: app = FastAPI() manager = Manager() -counter_store = Store(n=1, manager=manager) # XXX: pesca da file di conf +counter_store = Store( + n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager +) # XXX: pesca da file di conf security = HTTPBasic()