diff --git a/compose.yaml b/compose.yaml index 8edf5ca3..ef20dd50 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,21 @@ services: + app: build: . env_file: .env + environment: + FLASK_SQLALCHEMY_DATABASE_URI: "postgresql+pg8000://schemes:password@database/schemes" ports: - "5000:5000" + depends_on: + database: + condition: service_healthy + + database: + image: postgres:15 + environment: + POSTGRES_USER: schemes + POSTGRES_PASSWORD: password + healthcheck: + test: pg_isready -U $$POSTGRES_USER + interval: 5s diff --git a/pyproject.toml b/pyproject.toml index ee4dba9b..f5a3ce8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ dependencies = [ "govuk-frontend-jinja~=2.7.0", "gunicorn~=21.2.0", "inject~=5.0.0", + "pg8000~=1.30.0", "python-dotenv~=1.0.0", "requests~=2.31.0", "sqlalchemy~=2.0.0" diff --git a/schemes/__init__.py b/schemes/__init__.py index 9fc497e6..33a95438 100644 --- a/schemes/__init__.py +++ b/schemes/__init__.py @@ -1,10 +1,10 @@ import os -from typing import Any, Mapping +from typing import Any, Callable, Mapping import inject from authlib.integrations.flask_client import OAuth from authlib.oauth2.rfc7523 import PrivateKeyJWT -from flask import Flask, Response, render_template, request, url_for +from flask import Config, Flask, Response, render_template, request, url_for from inject import Binder from jinja2 import ChoiceLoader, FileSystemLoader, PackageLoader, PrefixLoader from sqlalchemy import Engine, MetaData, create_engine @@ -22,7 +22,11 @@ def create_app(test_config: Mapping[str, Any] | None = None) -> Flask: app.config.from_prefixed_env() app.config.from_mapping(test_config) - inject.configure(_bindings, bind_in_runtime=False) + def bindings(binder: Binder) -> None: + binder.bind(Config, app.config) + _bindings(binder) + + inject.configure(bindings, bind_in_runtime=False) _configure_error_pages(app) _configure_basic_auth(app) @@ -43,7 +47,11 @@ def create_app(test_config: Mapping[str, Any] | None = None) -> Flask: def _bindings(binder: Binder) -> None: - binder.bind(Engine, create_engine("sqlite+pysqlite:///file::memory:?uri=true")) + @inject.autoparams() + def engine(config: Config) -> Engine: + return create_engine(config["SQLALCHEMY_DATABASE_URI"]) + + binder.bind_to_constructor(Engine, engine) binder.bind_to_constructor(UserRepository, DatabaseUserRepository) diff --git a/schemes/config.py b/schemes/config.py index 39eb59c8..c88d7422 100644 --- a/schemes/config.py +++ b/schemes/config.py @@ -1,4 +1,5 @@ class Config: + SQLALCHEMY_DATABASE_URI = "sqlite+pysqlite:///file::memory:?uri=true" GOVUK_SERVER_METADATA_URL = "https://oidc.integration.account.gov.uk/.well-known/openid-configuration" GOVUK_TOKEN_ENDPOINT = "https://oidc.integration.account.gov.uk/token" GOVUK_PROFILE_URL = "https://home.integration.account.gov.uk/"