Skip to content

Commit

Permalink
Merge branch '1-revoir-la-gestion-de-la-config-pour-centraliser' into…
Browse files Browse the repository at this point in the history
… 'dev'

Resolve "Revoir la gestion de la config pour centraliser"

Closes #1

See merge request natural-solutions/geonature/annotation!34
  • Loading branch information
Maxime Vergez committed Nov 22, 2022
2 parents a6d32a5 + 48e8959 commit e2808e1
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 335 deletions.
4 changes: 2 additions & 2 deletions api/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ verify_ssl = true
name = "pypi"

[packages]
fastapi = "*"
fastapi = "==0.86.0"
uvicorn = "*"
sqlalchemy = ">=1.4.17,<=1.4.35"
python-multipart = "*"
psycopg2-binary = "*"
boto3 = "*"
python-decouple = "*"
unipath = "*"
sqlmodel = "*"
sqlalchemy-utils = "*"
Expand All @@ -22,6 +21,7 @@ black = "*"
isort = "*"
pillow = "*"
alembic = "*"
pydantic = {extras = ["dotenv"], version = "*"}

[dev-packages]

Expand Down
661 changes: 342 additions & 319 deletions api/Pipfile.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions api/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from logging.config import fileConfig

from decouple import config as app_config
from sqlalchemy import engine_from_config, pool
from sqlmodel import SQLModel

from alembic import context
from src.config import settings

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down Expand Up @@ -67,7 +67,7 @@ def run_migrations_offline() -> None:
script output.
"""
url = app_config("DB_URL")
url = settings.DB_URL
context.configure(
url=url,
target_metadata=target_metadata,
Expand All @@ -86,7 +86,7 @@ def run_migrations_online() -> None:
and associate a connection with the context.
"""
url = app_config("DB_URL")
url = settings.DB_URL
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
Expand Down
17 changes: 17 additions & 0 deletions api/src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseSettings, AnyHttpUrl, PostgresDsn


class Settings(BaseSettings):
API_ROOT_PATH: str = "/api/v1"
DB_URL: PostgresDsn = "postgresql://annotation:password@db/annotation"
MINIO_ENTRYPOINT_URL: AnyHttpUrl = "http://localhost:9000"
MINIO_ROOT_USER: str = "test"
MINIO_ROOT_PASSWORD: str = "password"
MINIO_BUCKET_NAME: str = "bucket"

class Config:
env_file = '.env'
case_sensitive = True


settings = Settings()
4 changes: 2 additions & 2 deletions api/src/connectors/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from datetime import datetime as dt
from pathlib import Path

from decouple import config
from sqlmodel import Session, SQLModel, create_engine

from src.config import settings
from src.models.deployment import DeploymentBase, Deployments
from src.models.device import DeviceBase, Devices
from src.models.models import Roles
Expand All @@ -13,7 +13,7 @@
from src.schemas.user import UserCreate
from src.services import deployment, device, files, project, site, user

DATABASE_URL = config("DB_URL")
DATABASE_URL = settings.DB_URL

engine = create_engine(DATABASE_URL, echo=True)

Expand Down
11 changes: 6 additions & 5 deletions api/src/connectors/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError
from decouple import config

MINIO_ENTRYPOINT_URL = config("MINIO_ENTRYPOINT_URL")
MINIO_ROOT_USER = config("MINIO_ROOT_USER")
MINIO_ROOT_PASSWORD = config("MINIO_ROOT_PASSWORD")
MINIO_BUCKET_NAME = config("MINIO_BUCKET_NAME")
from src.config import settings

MINIO_ENTRYPOINT_URL = settings.MINIO_ENTRYPOINT_URL
MINIO_ROOT_USER = settings.MINIO_ROOT_USER
MINIO_ROOT_PASSWORD = settings.MINIO_ROOT_PASSWORD
MINIO_BUCKET_NAME = settings.MINIO_BUCKET_NAME

config_dict = {
"endpoint_url": "http://minio:9000",
Expand Down
4 changes: 2 additions & 2 deletions api/src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from decouple import config
from fastapi import Depends, FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware

from src.config import settings
from src.connectors.s3 import init_bucket
from src.dependencies import get_query_token, get_token_header
from src.internal import admin
Expand All @@ -16,7 +16,7 @@
users,
)

ROOT_PATH = config("API_ROOT_PATH")
ROOT_PATH = settings.API_ROOT_PATH

app = FastAPI(
root_path=ROOT_PATH,
Expand Down
5 changes: 3 additions & 2 deletions api/tests/utils/test_db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from decouple import config
from sqlalchemy_utils import create_database, database_exists, drop_database
from sqlmodel import SQLModel, create_engine

DATABASE_URL = config("DB_URL")
from src.config import settings

DATABASE_URL = settings.DB_URL
db_test_uri = f"{DATABASE_URL}_test"

engine = create_engine(db_test_uri)
Expand Down

0 comments on commit e2808e1

Please sign in to comment.