From 63154c02492186b89f9e0e72e7c65d43ef596a7f Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 17 Aug 2022 19:17:01 +0200 Subject: [PATCH] settings --- pizzicore/pizzicore.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pizzicore/pizzicore.py b/pizzicore/pizzicore.py index 08ee1dd..f7ea378 100644 --- a/pizzicore/pizzicore.py +++ b/pizzicore/pizzicore.py @@ -3,6 +3,7 @@ import logging import dbm from collections import defaultdict from mimetypes import guess_type +from pathlib import Path from asyncio.queues import Queue 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.staticfiles import StaticFiles 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: def __init__(self, n: int): @@ -34,13 +42,13 @@ 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: + with dbm.open(str(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: + with dbm.open(str(self.db_path), "w") as db: db[str(key)] = str(value) return ret @@ -96,9 +104,10 @@ app.add_middleware( app.mount("/static", StaticFiles(directory="static"), name="static") manager = Manager() +settings = Settings() counter_store = Store( - n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager -) # XXX: pesca da file di conf + n=settings.queues_number, db_path=settings.storage_dir / "pizzicore.dbm", manager=manager +) security = HTTPBasic()