Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Nov 21, 2024
1 parent 4c48536 commit 18d619f
Show file tree
Hide file tree
Showing 107 changed files with 1,381 additions and 548 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
.venv
*.db
.env
*.db
7 changes: 7 additions & 0 deletions amalgam/fastapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Fastapp

# Principle

- Developer should be able to override everything with sane amount of code.
- Simple tasks should only require small amount of code.
- A bit of magic is okay as long as transparent and documented.
17 changes: 17 additions & 0 deletions amalgam/fastapp/_zrb/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import platform

DIR = os.path.dirname(__file__)
APP_DIR = os.path.dirname(DIR)
APP_MODULE_NAME = os.path.basename(APP_DIR)

MICROSERVICES_ENV_VARS = {
"FASTAPP_MODE": "microservices",
"FASTAPP_AUTH_BASE_URL": "http://localhost:3002",
}
MONOLITH_ENV_VARS = {"FASTAPP_MODE": "monolith"}

if platform.system() == "Windows":
ACTIVATE_VENV_SCRIPT = "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser; . .venv\Scripts\Activate" # noqa
else:
ACTIVATE_VENV_SCRIPT = "source .venv/bin/activate"
17 changes: 17 additions & 0 deletions amalgam/fastapp/_zrb/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from zrb import Group, project_group

app_group = project_group.add_group(
Group(name="fastapp", description="🚀 Managing Fastapp")
)

app_run_group = app_group.add_group(
Group(name="run", description="🟢 Run Fastapp")
)

app_migrate_group = app_group.add_group(
Group(name="migrate", description="📦 Run Fastapp DB migration")
)

app_create_group = app_group.add_group(
Group(name="create", description="✨ Create resources for Fastapp")
)
89 changes: 89 additions & 0 deletions amalgam/fastapp/_zrb/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os

from zrb import Cmd, CmdTask, EnvFile, EnvMap, Task, StrInput
from fastapp._zrb.config import (
APP_DIR, MICROSERVICES_ENV_VARS, MONOLITH_ENV_VARS, ACTIVATE_VENV_SCRIPT
)


def create_migration(name: str, module: str) -> Task:
return CmdTask(
name=f"create-fastapp-{name}-migration",
description=f"🧩 Create Fastapp {name.capitalize()} DB migration",
input=StrInput(
name="message",
description="Migration message",
prompt="Migration message",
allow_empty=False,
),
env=[
EnvFile(path=os.path.join(APP_DIR, "template.env")),
EnvMap(
vars={
"APP_DB_URL": f"sqlite:///{APP_DIR}/.migration.{module}.db",
"FASTAPP_MODULES": f"{module}",
}
),
],
cwd=APP_DIR,
cmd=[
ACTIVATE_VENV_SCRIPT,
f"cd {os.path.join(APP_DIR, 'module', module)}",
"alembic upgrade head",
Cmd(
"alembic revision --autogenerate -m {double_quote(ctx.input.message)}",
auto_render=True
),
],
auto_render_cmd=False,
retries=2,
)


def migrate_module(name: str, module: str, as_microservices: bool) -> Task:
env_vars = MICROSERVICES_ENV_VARS if as_microservices else MONOLITH_ENV_VARS
return CmdTask(
name=f"migrate-fastapp-{name}" if as_microservices else f"migrate-{name}-on-monolith",
description=f"🧩 Run Fastapp {name.capitalize()} DB migration",
env=[
EnvFile(path=os.path.join(APP_DIR, "template.env")),
EnvMap(
vars={
**env_vars,
"FASTAPP_MODULES": f"{module}",
}
),
],
cwd=APP_DIR,
cmd=[
ACTIVATE_VENV_SCRIPT,
f"cd {os.path.join(APP_DIR, 'module', module)}",
"alembic upgrade head",
],
auto_render_cmd=False,
retries=2,
)


def run_microservice(name: str, port: int, module: str) -> Task:
return CmdTask(
name=f"run-fastapp-{name}",
description=f"🧩 Run Fastapp {name.capitalize()}",
env=[
EnvFile(path=os.path.join(APP_DIR, "template.env")),
EnvMap(
vars={
**MICROSERVICES_ENV_VARS,
"FASTAPP_PORT": f"{port}",
"FASTAPP_MODULES": f"{module}",
}
),
],
cwd=APP_DIR,
cmd=[
ACTIVATE_VENV_SCRIPT,
'fastapi dev main.py --port "${FASTAPP_PORT}"',
],
auto_render_cmd=False,
retries=2,
)
117 changes: 117 additions & 0 deletions amalgam/fastapp/_zrb/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os

from zrb import CmdTask, Env, EnvFile, Task
from fastapp._zrb.config import APP_DIR, ACTIVATE_VENV_SCRIPT
from fastapp._zrb.group import app_run_group, app_migrate_group, app_create_group
from fastapp._zrb.helper import migrate_module, run_microservice, create_migration
from fastapp._zrb.venv_task import prepare_venv

# 🚀 Run/Migrate All ===========================================================

run_all = app_run_group.add_task(
Task(
name="run-fastapp", description="🟢 Run Fastapp as monolith and microservices"
),
alias="all",
)

migrate_all = app_migrate_group.add_task(
Task(
name="migrate-fastapp",
description="📦 Run Fastapp DB migration for monolith and microservices"
),
alias="all",
)

create_all_migration = app_create_group.add_task(
Task(name="create-fastapp-migration", description="📦 Create Fastapp DB migration"),
alias="migration"
)

# 🗿 Run/Migrate Monolith =====================================================

run_monolith = app_run_group.add_task(
CmdTask(
name="run-monolith-fastapp",
description="🗿 Run Fastapp as a monolith",
env=[
EnvFile(path=os.path.join(APP_DIR, "template.env")),
Env(name="FASTAPP_MODE", default="monolith"),
],
cwd=APP_DIR,
cmd=[
ACTIVATE_VENV_SCRIPT,
'fastapi dev main.py --port "${FASTAPP_PORT}"',
],
auto_render_cmd=False,
retries=2,
),
alias="monolith",
)
prepare_venv >> run_monolith >> run_all

migrate_monolith = app_migrate_group.add_task(
Task(
name="migrate-monolith-fastapp",
description="🗿 Run Fastapp DB migration for monolith"
),
alias="monolith",
)
migrate_monolith >> migrate_all

# 🌐 Run/Migrate Microsevices ==================================================

run_microservices = app_run_group.add_task(
Task(name="run-microservices-fastapp", description="🌐 Run Fastapp as microservices"),
alias="microservices",
)
run_microservices >> run_all

migrate_microservices = app_migrate_group.add_task(
Task(
name="migrate-microservices-fastapp",
description="🌐 Run Fastapp DB migration for microservices"
),
alias="microservices",
)
migrate_microservices >> migrate_all

# 📡 Run/Migrate Gateway =======================================================

run_gateway = app_run_group.add_task(
run_microservice("gateway", 3001, "gateway"), alias="microservices-gateway"
)
prepare_venv >> run_gateway >> run_microservices

create_gateway_migration = app_create_group.add_task(
create_migration("gateway", "gateway"), alias="gateway-migration"
)
prepare_venv >> create_gateway_migration >> create_all_migration

migrate_monolith_gateway = migrate_module("gateway", "gateway", as_microservices=False)
prepare_venv >> migrate_monolith_gateway >> [migrate_monolith, run_monolith]

migrate_microservices_gateway = app_migrate_group.add_task(
migrate_module("gateway", "gateway", as_microservices=True), alias="microservices-gateway"
)
prepare_venv >> migrate_microservices_gateway >> [migrate_microservices, run_gateway]

# 🔐 Run/Migrate Auth ==========================================================

run_auth = app_run_group.add_task(
run_microservice("auth", 3002, "auth"), alias="microservices-auth"
)
prepare_venv >> run_auth >> run_microservices

create_auth_migration = app_create_group.add_task(
create_migration("auth", "auth"), alias="auth-migration"
)
prepare_venv >> create_auth_migration >> create_all_migration

migrate_monolith_auth = migrate_module("auth", "auth", as_microservices=False)
prepare_venv >> migrate_monolith_auth >> [migrate_monolith, run_monolith]

migrate_microservices_auth = app_migrate_group.add_task(
migrate_module("auth", "auth", as_microservices=True), alias="microservices-auth"
)
prepare_venv >> migrate_microservices_auth >> [migrate_microservices, run_auth]
21 changes: 21 additions & 0 deletions amalgam/fastapp/_zrb/venv_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

from zrb import CmdTask
from fastapp._zrb.config import APP_DIR, ACTIVATE_VENV_SCRIPT
from fastapp._zrb.group import app_group

create_venv = CmdTask(
name="create-fastapp-venv",
cwd=APP_DIR,
cmd="python -m venv .venv",
execute_condition=lambda _: not os.path.isdir(os.path.join(APP_DIR, ".venv")),
)

prepare_venv = CmdTask(
name="prepare-fastapp-venv",
cmd=[ACTIVATE_VENV_SCRIPT, "pip install -r requirements.txt"],
cwd=APP_DIR,
)
create_venv >> prepare_venv

app_group.add_task(create_venv, alias="prepare")
4 changes: 2 additions & 2 deletions amalgam/fastapp/common/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from contextlib import asynccontextmanager

from common.db_engine import engine
from config import APP_MODE, APP_MODULES
from fastapp.common.db_engine import engine
from fastapp.config import APP_MODE, APP_MODULES
from fastapi import FastAPI
from sqlmodel import SQLModel

Expand Down
2 changes: 1 addition & 1 deletion amalgam/fastapp/common/db_engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from config import APP_DB_URL
from fastapp.config import APP_DB_URL
from sqlmodel import create_engine

connect_args = {"check_same_thread": False}
Expand Down
Loading

0 comments on commit 18d619f

Please sign in to comment.