Skip to content

Commit

Permalink
Use fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Nov 23, 2024
1 parent ac101b2 commit caef3e9
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 306 deletions.
25 changes: 15 additions & 10 deletions src/zrb/runner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from typing import Any

from zrb.config import BANNER, WEB_HTTP_PORT
from zrb.context.any_context import AnyContext
from zrb.context.shared_context import SharedContext
from zrb.group.group import Group
from zrb.runner.web_server import run_web_server
from zrb.session.session import Session
from zrb.task.any_task import AnyTask
from zrb.task.task import Task
from zrb.task.make_task import make_task
from zrb.util.cli.style import (
stylize_bold_yellow,
stylize_faint,
Expand Down Expand Up @@ -148,14 +149,18 @@ def _extract_kwargs_from_args(


cli = Cli(name="zrb", description="Your Automation Powerhouse", banner=BANNER)
server = cli.add_group(Group(name="server", description="🌐 Server related command"))
server.add_task(
Task(
name="start-server",
description="🚀 Start Zrb Web Server",
action=lambda ctx: run_web_server(ctx=ctx, root_group=cli, port=WEB_HTTP_PORT),
cli_only=True,
retries=0,
),
server_group = cli.add_group(
Group(name="server", description="🌐 Server related command")
)


@make_task(
name="start-server",
description="🚀 Start Zrb Web Server",
cli_only=True,
retries=0,
group=server_group,
alias="start",
)
async def run(ctx: AnyContext):
await run_web_server(web_server_ctx=ctx, root_group=cli, port=WEB_HTTP_PORT)
24 changes: 0 additions & 24 deletions src/zrb/runner/web_app/any_request_handler.py

This file was deleted.

9 changes: 4 additions & 5 deletions src/zrb/runner/web_app/group_info_ui/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from fastapi.responses import HTMLResponse

from zrb.group.any_group import AnyGroup
from zrb.runner.web_app.any_request_handler import AnyRequestHandler
from zrb.util.group import get_non_empty_subgroups, get_subtasks
from zrb.util.string.format import fstring_format

Expand All @@ -23,9 +24,7 @@
_TASK_LI_TEMPLATE = f.read()


def handle_group_info_ui(
handler: AnyRequestHandler, root_group: AnyGroup, group: AnyGroup, url: str
):
def handle_group_info_ui(root_group: AnyGroup, group: AnyGroup, url: str):
url_parts = url.split("/")
parent_url_parts = url_parts[:-2] + [""]
parent_url = "/".join(parent_url_parts)
Expand Down Expand Up @@ -75,7 +74,7 @@ def handle_group_info_ui(
},
)
)
handler.send_html_response(
return HTMLResponse(
fstring_format(
_VIEW_TEMPLATE,
{
Expand Down
4 changes: 2 additions & 2 deletions src/zrb/runner/web_app/group_info_ui/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" href="/pico.min.css">
<link rel="icon" href="/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="stylesheet" href="/static/pico.min.css">
<link rel="icon" href="/static/favicon-32x32.png" sizes="32x32" type="image/png">
<title>Zrb</title>
</head>
<body>
Expand Down
7 changes: 4 additions & 3 deletions src/zrb/runner/web_app/home_page/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from fastapi.responses import HTMLResponse

from zrb.group.any_group import AnyGroup
from zrb.runner.web_app.any_request_handler import AnyRequestHandler
from zrb.util.group import get_non_empty_subgroups, get_subtasks
from zrb.util.string.format import fstring_format

Expand All @@ -23,7 +24,7 @@
_TASK_LI_TEMPLATE = f.read()


def handle_home_page(handler: AnyRequestHandler, root_group: AnyGroup):
def handle_home_page(root_group: AnyGroup):
subgroups = get_non_empty_subgroups(root_group, web_only=True)
group_info = (
""
Expand Down Expand Up @@ -62,7 +63,7 @@ def handle_home_page(handler: AnyRequestHandler, root_group: AnyGroup):
},
)
)
handler.send_html_response(
return HTMLResponse(
fstring_format(
_VIEW_TEMPLATE,
{
Expand Down
4 changes: 2 additions & 2 deletions src/zrb/runner/web_app/home_page/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" href="/pico.min.css">
<link rel="icon" href="/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="stylesheet" href="/static/pico.min.css">
<link rel="icon" href="/static/favicon-32x32.png" sizes="32x32" type="image/png">
<title>Zrb</title>
</head>
<body>
Expand Down
12 changes: 4 additions & 8 deletions src/zrb/runner/web_app/task_ui/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from fastapi.responses import HTMLResponse

from zrb.group.any_group import AnyGroup
from zrb.runner.web_app.any_request_handler import AnyRequestHandler
from zrb.session.any_session import AnySession
from zrb.task.any_task import AnyTask
from zrb.util.string.format import fstring_format
Expand All @@ -28,12 +29,7 @@


def handle_task_ui(
handler: AnyRequestHandler,
root_group: AnyGroup,
task: AnyTask,
session: AnySession,
url: str,
args: list[str],
root_group: AnyGroup, task: AnyTask, session: AnySession, url: str, args: list[str]
):
session.register_task(task)
ctx = task.get_ctx(session)
Expand All @@ -56,7 +52,7 @@ def handle_task_ui(
]
)
session_name = args[0] if len(args) > 0 else ""
handler.send_html_response(
return HTMLResponse(
fstring_format(
_VIEW_TEMPLATE,
{
Expand Down
4 changes: 2 additions & 2 deletions src/zrb/runner/web_app/task_ui/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" href="/pico.min.css">
<link rel="icon" href="/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="stylesheet" href="/static/pico.min.css">
<link rel="icon" href="/static/favicon-32x32.png" sizes="32x32" type="image/png">
<title>Zrb</title>
</head>
<body>
Expand Down
Loading

0 comments on commit caef3e9

Please sign in to comment.