Skip to content

Commit

Permalink
add config
Browse files Browse the repository at this point in the history
  • Loading branch information
optk2k committed Dec 25, 2023
1 parent 2086b8c commit d1cc02d
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 47 deletions.
35 changes: 27 additions & 8 deletions df_designer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import dff
import typer
import uvicorn
from df_designer import settings

from df_designer.settings import app

def main():
cli = typer.Typer()


@cli.command()
def build():
print("in developing ...")


@cli.command()
def meta():
print(f"dff version: {dff.__version__}")


@cli.command()
def run_app(
ip_address: str = app.conf_host,
port: int = app.conf_port,
):
"""Run the application."""
config = uvicorn.Config(
app=settings.app,
host=settings.host,
port=settings.port,
log_level=settings.log_level,
reload=settings.reload,
app=app.conf_app,
host=ip_address,
port=port,
log_level=app.conf_log_level,
reload=app.conf_reload,
)
server = uvicorn.Server(config)
server.run()


if __name__ == "__main__":
main()
cli()
25 changes: 12 additions & 13 deletions df_designer/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from pathlib import Path
import aiofiles
import dff
from fastapi import FastAPI, Request
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles

from df_designer.logic import get_data, save_data
from df_designer.settings import path_to_save

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
from df_designer.settings import app

app.mount(
"/static",
StaticFiles(directory=app.static_files),
name="static",
)
# TODO: добавить версию v1
# TODO: заглушка для dff
# TODO: тесты дописать
Expand All @@ -19,11 +20,10 @@
@app.get("/")
async def main_page() -> HTMLResponse:
"""Return frontend."""
start_page = "static/index.html"
if not Path(start_page).exists():
if not app.start_page.exists():
html = "frontend is not build"
else:
async with aiofiles.open(start_page) as file:
async with aiofiles.open(app.start_page) as file:
html = await file.read()
return HTMLResponse(content=html, status_code=200)

Expand All @@ -32,15 +32,15 @@ async def main_page() -> HTMLResponse:
@app.get("/flows")
async def flows_get():
"""(get flows from db) - returns JSON of all saved flows"""
result = await get_data(path_to_save)
result = await get_data(app.path_to_save)
return {"status": "ok", "data": result}


@app.post("/flows")
async def flows_post(request: Request) -> dict[str, str]:
"""(add new flows) - receives JSON of new flows"""
data = await request.json()
await save_data(path=path_to_save, data=data)
await save_data(path=app.path_to_save, data=data)
return {"status": "ok"}


Expand Down Expand Up @@ -75,9 +75,8 @@ async def service_health_get() -> dict[str, str]:
return {"status": "ok"}


# TODO: rename meta :do
# TODO: команды для Максима
@app.get("/service/version")
@app.get("/meta")
async def service_version_get() -> dict[str, str]:
"""(get dff curr version)"""
version = dff.__version__
Expand Down
26 changes: 18 additions & 8 deletions df_designer/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from pathlib import Path

# server settings
app = "df_designer.main:app"
host = "127.0.0.1"
port = 8000
log_level = "info"
reload = True
from fastapi import FastAPI

app = FastAPI()

# app settings
path_to_save = Path().home().joinpath("flows.json")

class Application(FastAPI):
package_dir = Path(__file__).absolute()
static_files = package_dir.with_name("static")
start_page = static_files.joinpath("index.html")
work_directory = "."
path_to_save = Path(work_directory).joinpath("flows.json")

conf_app = "df_designer.main:app"
conf_host = "127.0.0.1"
conf_port = 8000
conf_log_level = "info"
conf_reload = True


app = Application()
File renamed without changes.
93 changes: 92 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ fastapi = "^0.104.1"
uvicorn = "^0.24.0.post1"
aiofiles = "^23.2.1"
dff = "^0.6.3"
pytest-mock = "^3.12.0"
typer = "^0.9.0"
rich = "^13.7.0"



Expand Down
16 changes: 9 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from pathlib import Path

import pytest

# @pytest.fixture(scope="session", autouse=True)
# def test_folder():
# """create a folder"""
# print("]---starting test---[")
# yield
# print("]---ending test---[")

@pytest.fixture(scope="session", autouse=True)
def test_folder():
"""create a folder"""
print("]---starting test---[")
Path("test_directory").mkdir()
yield
Path("test_directory").rmdir()
print("]---ending test---[")


@pytest.fixture
Expand Down
23 changes: 13 additions & 10 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from pathlib import Path

from fastapi.testclient import TestClient

from df_designer import settings
from df_designer.main import app

client = TestClient(app)
# client.app.path_to_save = "/tmp/flows.json"


def test_main_main_page():
Expand All @@ -19,10 +18,11 @@ def test_flows_get():


def test_flows_post():
client.app.path_to_save = "/tmp/flows.json"
response = client.post("/flows", json={"key": "value"})
assert response.status_code == 200
assert response.json() == {"status": "ok"}
Path(settings.path_to_save).unlink()
Path(client.app.path_to_save).unlink()


def test_flows_patch():
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_service_health_get():


def test_service_version_get():
response = client.get("/service/version")
response = client.get("/meta")
assert response.status_code == 200


Expand Down Expand Up @@ -81,9 +81,12 @@ def test_build_get():


def test_settings():
assert settings.app == "df_designer.main:app"
assert settings.host == "127.0.0.1"
assert settings.port == 8000
assert settings.log_level == "info"
assert settings.reload is True
assert settings.path_to_save == Path().home().joinpath("flows.json")
assert app.conf_app == "df_designer.main:app"
assert app.conf_host == "127.0.0.1"
assert app.conf_port == 8000
assert app.conf_log_level == "info"
assert app.conf_reload is True


# def test_settings_path_to_save():
# assert app.path_to_save == Path(app.work_directory).joinpath("flows.json")

0 comments on commit d1cc02d

Please sign in to comment.