Skip to content

Commit

Permalink
GH-4: Use PostgreSQL when running locally with Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 16, 2023
1 parent 52e9b38 commit aa9bbe5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
15 changes: 15 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 12 additions & 4 deletions schemes/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)


Expand Down
1 change: 1 addition & 0 deletions schemes/config.py
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down

0 comments on commit aa9bbe5

Please sign in to comment.