Skip to content

Commit

Permalink
Merge pull request #36 from lioramilbaum/db
Browse files Browse the repository at this point in the history
Get handle of the db engine
  • Loading branch information
lmilbaum committed Oct 28, 2020
2 parents efba5fe + 20d3ab6 commit e9a8836
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 10 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ disable=print-statement,
# we use black code style and don't need additional checks
bad-continuation,
line-too-long,
invalid-name,

4 changes: 4 additions & 0 deletions exodus_gw/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from fastapi import FastAPI

from .database import get_db

db = get_db()

app = FastAPI(title="exodus-gw")
16 changes: 16 additions & 0 deletions exodus_gw/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sqlalchemy import create_engine

from .settings import get_settings


def get_db():
"""Get db engine
The database name is identical to the user name
See additional info:
https://devopstuto-docker.readthedocs.io/en/latest/docker_library/postgresql/postgresql.html#postgres-user # noqa
"""
SQLALCHEMY_DATABASE_URL = (
"postgresql://{s.db_service_user}:{s.db_service_pass}@"
"{s.db_service_host}:{s.db_service_port}/{s.db_service_user}"
).format(s=get_settings())
return create_engine(SQLALCHEMY_DATABASE_URL)
15 changes: 12 additions & 3 deletions exodus_gw/settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
import configparser
from typing import List
import os
from functools import lru_cache
from typing import List

from pydantic import BaseSettings
from fastapi import HTTPException
from pydantic import BaseSettings


class Environment(object):
Expand All @@ -29,6 +29,15 @@ class Settings(BaseSettings):
environments: List[Environment] = []
"""List of environment objects derived from exodus-gw.ini"""

db_service_user: str = "exodus-gw"
"""db service user name"""
db_service_pass: str = "exodus-gw"
"""db service user password"""
db_service_host: str = "exodus-gw-db"
"""db service host"""
db_service_port: str = "5432"
"""db service port"""

class Config:
env_prefix = "exodus_gw_"

Expand Down
9 changes: 6 additions & 3 deletions openshift/containers/exodus-gw/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ RUN \
microdnf -y install shadow-utils \
# Install extra commands needed for build
&& microdnf -y install python3 python3-devel gcc make \
# Install packages needed for psycopg2 installation
&& microdnf -y install postgresql-devel \
&& cd /usr/local/src/exodus-gw \
# Install application itself
&& pip3 install --require-hashes -r requirements.txt \
&& pip3 install --no-deps . \
# Clean up unnecessary data
&& microdnf clean all && rm -rf /var/cache/yum && rm -rf /usr/local/src/exodus-gw


# Run as a non-root user
RUN adduser exodus-gw
USER exodus-gw
Expand All @@ -25,6 +28,6 @@ EXPOSE 8080

# Run the application
ENTRYPOINT ["gunicorn", \
"-k", "uvicorn.workers.UvicornWorker", \
"--bind", "0.0.0.0:8080", \
"exodus_gw:application"]
"-k", "uvicorn.workers.UvicornWorker", \
"--bind", "0.0.0.0:8080", \
"exodus_gw:application"]
2 changes: 2 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ defusedxml
uvicorn[standard]
gunicorn
dataclasses;python_version<'3.7'
psycopg2
sqlalchemy
55 changes: 54 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --generate-hashes requirements.in
# pip-compile --generate-hashes --output-file=requirements.txt requirements.in
#
aioboto3==8.0.5 \
--hash=sha256:0942ca5feff0b95b96a79ba93a70042daf9304015f15f489801709173796c486 \
Expand Down Expand Up @@ -144,6 +144,21 @@ multidict==4.7.6 \
--hash=sha256:fcfbb44c59af3f8ea984de67ec7c306f618a3ec771c2843804069917a8f2e255 \
--hash=sha256:feed85993dbdb1dbc29102f50bca65bdc68f2c0c8d352468c25b54874f23c39d \
# via aiohttp, yarl
psycopg2==2.8.5 \
--hash=sha256:132efc7ee46a763e68a815f4d26223d9c679953cd190f1f218187cb60decf535 \
--hash=sha256:2327bf42c1744a434ed8ed0bbaa9168cac7ee5a22a9001f6fc85c33b8a4a14b7 \
--hash=sha256:27c633f2d5db0fc27b51f1b08f410715b59fa3802987aec91aeb8f562724e95c \
--hash=sha256:2c0afb40cfb4d53487ee2ebe128649028c9a78d2476d14a67781e45dc287f080 \
--hash=sha256:2df2bf1b87305bd95eb3ac666ee1f00a9c83d10927b8144e8e39644218f4cf81 \
--hash=sha256:440a3ea2c955e89321a138eb7582aa1d22fe286c7d65e26a2c5411af0a88ae72 \
--hash=sha256:6a471d4d2a6f14c97a882e8d3124869bc623f3df6177eefe02994ea41fd45b52 \
--hash=sha256:6b306dae53ec7f4f67a10942cf8ac85de930ea90e9903e2df4001f69b7833f7e \
--hash=sha256:a0984ff49e176062fcdc8a5a2a670c9bb1704a2f69548bce8f8a7bad41c661bf \
--hash=sha256:ac5b23d0199c012ad91ed1bbb971b7666da651c6371529b1be8cbe2a7bf3c3a9 \
--hash=sha256:acf56d564e443e3dea152efe972b1434058244298a94348fc518d6dd6a9fb0bb \
--hash=sha256:d3b29d717d39d3580efd760a9a46a7418408acebbb784717c90d708c9ed5f055 \
--hash=sha256:f7d46240f7a1ae1dd95aab38bd74f7428d46531f69219954266d669da60c0818 \
# via -r requirements.in
pydantic==1.6.1 \
--hash=sha256:1783c1d927f9e1366e0e0609ae324039b2479a1a282a98ed6a6836c9ed02002c \
--hash=sha256:2dc946b07cf24bee4737ced0ae77e2ea6bc97489ba5a035b603bd1b40ad81f7e \
Expand Down Expand Up @@ -192,6 +207,40 @@ six==1.15.0 \
--hash=sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259 \
--hash=sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced \
# via python-dateutil
sqlalchemy==1.3.19 \
--hash=sha256:072766c3bd09294d716b2d114d46ffc5ccf8ea0b714a4e1c48253014b771c6bb \
--hash=sha256:107d4af989831d7b091e382d192955679ec07a9209996bf8090f1f539ffc5804 \
--hash=sha256:15c0bcd3c14f4086701c33a9e87e2c7ceb3bcb4a246cd88ec54a49cf2a5bd1a6 \
--hash=sha256:26c5ca9d09f0e21b8671a32f7d83caad5be1f6ff45eef5ec2f6fd0db85fc5dc0 \
--hash=sha256:276936d41111a501cf4a1a0543e25449108d87e9f8c94714f7660eaea89ae5fe \
--hash=sha256:3292a28344922415f939ee7f4fc0c186f3d5a0bf02192ceabd4f1129d71b08de \
--hash=sha256:33d29ae8f1dc7c75b191bb6833f55a19c932514b9b5ce8c3ab9bc3047da5db36 \
--hash=sha256:3bba2e9fbedb0511769780fe1d63007081008c5c2d7d715e91858c94dbaa260e \
--hash=sha256:465c999ef30b1c7525f81330184121521418a67189053bcf585824d833c05b66 \
--hash=sha256:51064ee7938526bab92acd049d41a1dc797422256086b39c08bafeffb9d304c6 \
--hash=sha256:5a49e8473b1ab1228302ed27365ea0fadd4bf44bc0f9e73fe38e10fdd3d6b4fc \
--hash=sha256:618db68745682f64cedc96ca93707805d1f3a031747b5a0d8e150cfd5055ae4d \
--hash=sha256:6547b27698b5b3bbfc5210233bd9523de849b2bb8a0329cd754c9308fc8a05ce \
--hash=sha256:6557af9e0d23f46b8cd56f8af08eaac72d2e3c632ac8d5cf4e20215a8dca7cea \
--hash=sha256:73a40d4fcd35fdedce07b5885905753d5d4edf413fbe53544dd871f27d48bd4f \
--hash=sha256:8280f9dae4adb5889ce0bb3ec6a541bf05434db5f9ab7673078c00713d148365 \
--hash=sha256:83469ad15262402b0e0974e612546bc0b05f379b5aa9072ebf66d0f8fef16bea \
--hash=sha256:860d0fe234922fd5552b7f807fbb039e3e7ca58c18c8d38aa0d0a95ddf4f6c23 \
--hash=sha256:883c9fb62cebd1e7126dd683222b3b919657590c3e2db33bdc50ebbad53e0338 \
--hash=sha256:8afcb6f4064d234a43fea108859942d9795c4060ed0fbd9082b0f280181a15c1 \
--hash=sha256:96f51489ac187f4bab588cf51f9ff2d40b6d170ac9a4270ffaed535c8404256b \
--hash=sha256:9e865835e36dfbb1873b65e722ea627c096c11b05f796831e3a9b542926e979e \
--hash=sha256:aa0554495fe06172b550098909be8db79b5accdf6ffb59611900bea345df5eba \
--hash=sha256:b595e71c51657f9ee3235db8b53d0b57c09eee74dfb5b77edff0e46d2218dc02 \
--hash=sha256:b6ff91356354b7ff3bd208adcf875056d3d886ed7cef90c571aef2ab8a554b12 \
--hash=sha256:b70bad2f1a5bd3460746c3fb3ab69e4e0eb5f59d977a23f9b66e5bdc74d97b86 \
--hash=sha256:c7adb1f69a80573698c2def5ead584138ca00fff4ad9785a4b0b2bf927ba308d \
--hash=sha256:c898b3ebcc9eae7b36bd0b4bbbafce2d8076680f6868bcbacee2d39a7a9726a7 \
--hash=sha256:e49947d583fe4d29af528677e4f0aa21f5e535ca2ae69c48270ebebd0d8843c0 \
--hash=sha256:eb1d71643e4154398b02e88a42fc8b29db8c44ce4134cf0f4474bfc5cb5d4dac \
--hash=sha256:f2e8a9c0c8813a468aa659a01af6592f71cd30237ec27c4cc0683f089f90dcfc \
--hash=sha256:fe7fe11019fc3e6600819775a7d55abc5446dda07e9795f5954fdbf8a49e1c37 \
# via -r requirements.in
starlette==0.13.6 \
--hash=sha256:bd2ffe5e37fb75d014728511f8e68ebf2c80b0fa3d04ca1479f4dc752ae31ac9 \
--hash=sha256:ebe8ee08d9be96a3c9f31b2cb2a24dbdf845247b745664bd8a3f9bd0c977fdbc \
Expand Down Expand Up @@ -270,3 +319,7 @@ yarl==1.5.1 \
--hash=sha256:f18d68f2be6bf0e89f1521af2b1bb46e66ab0018faafa81d70f358153170a317 \
--hash=sha256:f379b7f83f23fe12823085cd6b906edc49df969eb99757f58ff382349a3303c6 \
# via aiohttp

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
# setuptools
7 changes: 4 additions & 3 deletions tests/settings/test_get_settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from fastapi import HTTPException

from exodus_gw.settings import get_settings, get_environment

from exodus_gw.settings import get_environment, get_settings
from fastapi import HTTPException

# Note: get_settings is wrapped in lru_cache.
# During tests, we want to test the real original function
Expand All @@ -21,6 +20,8 @@ def test_get_settings_default():
"test2",
"test3",
]
assert settings.db_service_user == "exodus-gw"
assert settings.db_service_pass == "exodus-gw"


def test_get_settings_override(monkeypatch):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from sqlalchemy.engine import Engine

from exodus_gw import database


def test_get_db():
assert isinstance(database.get_db(), Engine)

0 comments on commit e9a8836

Please sign in to comment.