Skip to content

Commit

Permalink
add settings that read env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtedn21 committed Nov 20, 2023
1 parent b17bd6b commit 03f875a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
4 changes: 3 additions & 1 deletion martin_eden/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion martin_eden/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
23 changes: 23 additions & 0 deletions martin_eden/settings.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "martin-eden"
version = "0.10.1"
version = "0.10.2"
description = "async backend framework on sockets"
authors = ["mrtedn21 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 03f875a

Please sign in to comment.