Skip to content

Commit

Permalink
update to python 312
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Mar 4, 2024
1 parent 42d5fbd commit fd9e19f
Show file tree
Hide file tree
Showing 9 changed files with 640 additions and 904 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ENVIRONMENT=dev
TESTING=0
REDIS_URL=redis://redis
REDIS_URL=redis://redis-chem
UP=up
DOWN=down
WEB_SERVER=web_server
Expand Down
49 changes: 25 additions & 24 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
name: Unit Tests
name: CI

on: [pull_request]
on:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
fail-fast: false
matrix:
python-version: [ "3.12" ]
poetry-version: [ "1.7.1" ]

env:
PYTHONDONTWRITEBYTECODE: 1
PYTHONUNBUFFERED: 1
ENVIRONMENT: test
TESTING: 0
UP: up
DOWN: down
WEB_SERVER: web_server
REDIS_URL: redis://127.0.0.1
REDIS_DB: 0
REDIS_TEST_KEY: covid-19-test
REDIS_PORT: 6379

services:
redis:
Expand All @@ -17,32 +35,15 @@ jobs:
- 6379:6379
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
uses: abatilo/actions-poetry@v2
with:
path: .venv
key: venv-${{ runner.os }}-3.11-${{ hashFiles('**/poetry.lock') }}
poetry-version: ${{ matrix.poetry-version }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Test Code
run: poetry run pytest
env:
PYTHONDONTWRITEBYTECODE: 1
PYTHONUNBUFFERED: 1
ENVIRONMENT: test
TESTING: 0
UP: up
DOWN: down
WEB_SERVER: web_server
REDIS_URL: redis://127.0.0.1
REDIS_DB: 0
REDIS_TEST_KEY: covid-19-test
REDIS_PORT: 6379
24 changes: 10 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
FROM python:3.11-slim-buster AS base
FROM python:3.12.2-slim-bookworm AS base
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends curl git build-essential \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/apt/lists/* \
&& rm -rf /var/cache/apt/*

&& apt-get autoremove -y
ENV POETRY_HOME="/opt/poetry"
ENV PATH="$POETRY_HOME/bin:$PATH" \
POETRY_VERSION=1.4.0
RUN curl -sSL https://install.python-poetry.org | python3 - \
&& poetry config virtualenvs.create false \
&& mkdir -p /cache/poetry \
&& poetry config cache-dir /cache/poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

FROM base AS install
WORKDIR /home/code

# allow controlling the poetry installation of dependencies via external args
ARG INSTALL_ARGS="--no-root"
ENV POETRY_HOME="/opt/poetry"
ENV PATH="$POETRY_HOME/bin:$PATH"
COPY pyproject.toml poetry.lock ./

# install without virtualenv, since we are inside a container
RUN --mount=type=cache,target=/cache/poetry \
poetry install --no-root --only main
RUN poetry config virtualenvs.create false \
&& poetry install $INSTALL_ARGS

# cleanup
RUN curl -sSL https://install.python-poetry.org | python3 - --uninstall
Expand All @@ -35,6 +29,8 @@ RUN apt-get purge -y curl git build-essential \

FROM install as app-image

ENV PYTHONPATH=/home/code/ PYTHONHASHSEED=0

COPY tests/ tests/
COPY app/ app/
COPY .env logger.ini ./
Expand Down
13 changes: 3 additions & 10 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import os
from functools import lru_cache

from pydantic import AnyUrl, BaseSettings

from app.logging import AppLogger

logger = AppLogger.__call__().get_logger()
from pydantic import AnyUrl
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
Expand All @@ -20,7 +16,4 @@ class Settings(BaseSettings):
web_server: str = os.getenv("WEB_SERVER", "web_server")


@lru_cache()
def get_settings() -> BaseSettings:
logger.info("Loading config settings from the environment...")
return Settings()
settings = Settings()
9 changes: 4 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from fastapi import Depends, FastAPI

from app import config
from app.config import settings
from app.logging import AppLogger
from app.redis import init_redis_pool
from app.routers import smiles_router
from app.service import MoleculesRepository
from app.config import settings as global_settings

logger = AppLogger.__call__().get_logger()
global_settings = config.Settings()

logger = AppLogger().get_logger()

app = FastAPI(title="ChemCompoundsAPI", version="0.0.0")
app.include_router(smiles_router)
Expand All @@ -28,7 +27,7 @@ async def shutdown_event():


@app.get("/health-check")
async def health_check(settings: config.Settings = Depends(config.get_settings)):
async def health_check():
try:
await app.state.redis.set(str(settings.redis_url), settings.up)
value = await app.state.redis.get(str(settings.redis_url))
Expand Down
14 changes: 10 additions & 4 deletions app/redis.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import redis.asyncio as redis

from app import config

global_settings = config.Settings()
from app.config import settings as global_settings


async def init_redis_pool() -> redis.Redis:
redis_c = await redis.from_url(
global_settings.redis_url,
global_settings.redis_url.unicode_string(),
encoding="utf-8",
db=global_settings.redis_db,
decode_responses=True,
)
return redis_c


# async def get_redis():
# return await redis.from_url(
# global_settings.redis_url.unicode_string(),
# encoding="utf-8",
# decode_responses=True,
# )
Loading

0 comments on commit fd9e19f

Please sign in to comment.