From c0e9f037c673d0db8f0877bd4885cf016b36fc3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20K=C3=B6pf?= Date: Thu, 15 Dec 2022 10:39:36 +0100 Subject: [PATCH] add config defaults for postgres dev docker image, support setting POSTGRES_PASSWORD --- .gitignore | 1 + backend/app/config.py | 20 ++++++++++++++++++-- backend/app/schemas/protocol.py | 8 ++++---- backend/scripts/run-local.sh | 2 -- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 0d20b648..eb3a37f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.venv *.pyc diff --git a/backend/app/config.py b/backend/app/config.py index 85cd8631..f0218bee 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -# touch -from typing import List, Optional, Union +from typing import Any, Dict, List, Optional, Union from pydantic import AnyHttpUrl, BaseSettings, PostgresDsn, validator @@ -8,8 +7,25 @@ from pydantic import AnyHttpUrl, BaseSettings, PostgresDsn, validator class Settings(BaseSettings): PROJECT_NAME: str = "open-chatGPT backend" API_V1_STR: str = "/api/v1" + + POSTGRES_SERVER: str = "localhost:5432" + POSTGRES_USER: str = "postgres" + POSTGRES_PASSWORD: str = "postgres" + POSTGRES_DB: str = "postgres" DATABASE_URI: Optional[PostgresDsn] = None + @validator("DATABASE_URI", pre=True) + def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any: + if isinstance(v, str): + return v + return PostgresDsn.build( + scheme="postgresql", + user=values.get("POSTGRES_USER"), + password=values.get("POSTGRES_PASSWORD"), + host=values.get("POSTGRES_SERVER"), + path=f"/{values.get('POSTGRES_DB') or ''}", + ) + BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] UPDATE_ALEMBIC: bool = True diff --git a/backend/app/schemas/protocol.py b/backend/app/schemas/protocol.py index a63b9c11..554c49e1 100644 --- a/backend/app/schemas/protocol.py +++ b/backend/app/schemas/protocol.py @@ -9,7 +9,7 @@ from pydantic import BaseModel class TaskRequest(BaseModel): """The frontend asks the backend for a task.""" - type: Literal + type: str user_id: Optional[str] = None @@ -21,14 +21,14 @@ class Task(BaseModel): """A task is a unit of work that the backend gives to the frontend.""" id: UUID = pydantic.Field(default_factory=UUID) - type: Literal + type: str addressed_users: Optional[list[str]] = None class TaskResponse(BaseModel): """A task response is a message from the frontend to acknowledge the given task.""" - type: Literal + type: str status: Literal["success", "failure"] @@ -50,7 +50,7 @@ class TaskDone(Task): class Interaction(BaseModel): """An interaction is a message from the frontend to the backend.""" - type: Literal + type: str user_id: str diff --git a/backend/scripts/run-local.sh b/backend/scripts/run-local.sh index 7c6efabc..d8bd86c6 100755 --- a/backend/scripts/run-local.sh +++ b/backend/scripts/run-local.sh @@ -1,5 +1,3 @@ #!/usr/bin/env bash -export DATABASE_URI=postgresql://postgres:postgres@localhost:5432/postgres - uvicorn app.main:app --reload