From 30f74f043e161c144fd7c4de33e871d78b9abe02 Mon Sep 17 00:00:00 2001 From: mrtedn21 Date: Mon, 20 Nov 2023 12:38:21 +0700 Subject: [PATCH] add settings that read env vars --- .github/workflows/test.yml | 7 ++++++- martin_eden/core.py | 4 +++- martin_eden/database.py | 4 +++- martin_eden/settings.py | 23 +++++++++++++++++++++++ pyproject.toml | 2 +- 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 martin_eden/settings.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2b0da90..26fbbee 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,8 +2,13 @@ name: Testing on: ["push", "pull_request"] +env: + POSTGRES_URL: ${{ vars.POSTGRES_URL }} + SERVER_HOST: ${{ vars.SERVER_HOST }} + SERVER_PORT: ${{ vars.SERVER_PORT }} + jobs: - test_and_publish: + test: runs-on: ubuntu-latest strategy: matrix: diff --git a/martin_eden/core.py b/martin_eden/core.py index ba5d7e0..b3414b5 100644 --- a/martin_eden/core.py +++ b/martin_eden/core.py @@ -16,6 +16,7 @@ FindControllerError, get_controller, register_route) from martin_eden.utils import get_argument_names +from martin_eden.settings import Settings db = DataBase() @@ -165,7 +166,8 @@ def _configure_sockets(self) -> None: socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) - server_address = ('localhost', 8001) + settings = Settings() + server_address = (settings.server_host, settings.server_port) self.server_socket.setblocking(False) self.server_socket.bind(server_address) diff --git a/martin_eden/database.py b/martin_eden/database.py index 53740b3..44d46e4 100644 --- a/martin_eden/database.py +++ b/martin_eden/database.py @@ -2,6 +2,7 @@ from dataclasses import field, make_dataclass from datetime import date, datetime from typing import Any, Callable, Iterable +from martin_eden.settings import Settings from marshmallow.fields import Date, DateTime, Int, Nested, Str from marshmallow_enum import EnumField as MarshmallowEnum @@ -139,8 +140,9 @@ def __new__(cls, name: str, bases: Iterable, fields: dict) -> type: class DataBase: def __init__(self) -> None: + settings = Settings() self.engine: AsyncEngine = create_async_engine( - 'postgresql+asyncpg://alexander.bezgin:123@localhost/framework', + settings.postgres_url, echo=True, ) self.create_session: Callable = async_sessionmaker(self.engine) diff --git a/martin_eden/settings.py b/martin_eden/settings.py new file mode 100644 index 0000000..faa0605 --- /dev/null +++ b/martin_eden/settings.py @@ -0,0 +1,23 @@ +import os + + +def read_env(var_name, default=None): + if default is None: + return os.environ[var_name] + else: + return os.environ.get(var_name, default=default) + + +def read_int(var_name, default=None): + return int(read_env(var_name, default=default)) + + +def read_str(var_name, default=None): + return read_env(var_name, default=default) + + +class Settings: + server_host = read_str('SERVER_HOST') + server_port = read_int('SERVER_PORT') + + postgres_url = read_str('POSTGRES_URL') diff --git a/pyproject.toml b/pyproject.toml index 1245197..c385cbb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "martin-eden" -version = "0.10.1" +version = "0.10.3" description = "async backend framework on sockets" authors = ["mrtedn21 "] readme = "README.md"