Skip to content

Commit

Permalink
add server command
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Mar 13, 2024
1 parent 93f05a8 commit 9953a93
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ COPY --from=ENV /usr/local/bin/_dockerfile_setup_root_prefix.sh /usr/local/bin/_
RUN /usr/local/bin/_dockerfile_initialize_user_accounts.sh && \
/usr/local/bin/_dockerfile_setup_root_prefix.sh

ENV PYTHONUTF8=1 RUN_IN_CONTAINER=1 AEXPY_ENV_PROVIDER=micromamba MAMBA_SKIP_ACTIVATE=1
ENV PYTHONUTF8=1 RUN_IN_CONTAINER=1 AEXPY_ENV_PROVIDER=micromamba AEXPY_SERVER_DATA=/data MAMBA_SKIP_ACTIVATE=1

COPY requirements.txt /tmp/requirements.txt
COPY --from=BUILD /dist /tmp/packages
Expand Down
3 changes: 2 additions & 1 deletion scripts/docker_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@


def frontend():
shutil.rmtree(serverWww)
if serverWww.is_dir():
shutil.rmtree(serverWww)
shutil.copytree(webDist, serverWww)


Expand Down
3 changes: 2 additions & 1 deletion scripts/server_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def build():


def copy():
shutil.rmtree(serverWww)
if serverWww.is_dir():
shutil.rmtree(serverWww)
shutil.copytree(webDist, serverWww)


Expand Down
15 changes: 11 additions & 4 deletions src/servers/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from pathlib import Path
from flask import Blueprint, Response, jsonify, request, send_file, send_from_directory

wwwdata = Path(__file__).parent.parent.joinpath("wwwdata")
WWW_DATA = Path(__file__).parent.parent.joinpath("wwwdata")

if not wwwdata.is_dir():
os.makedirs(wwwdata, exist_ok=True)

api = Blueprint("api", __name__)


@api.route("/data/<path:path>", methods=["GET"])
def wwwrootFiles(path: str = "index.html"):
return send_from_directory(wwwdata, path)
if not WWW_DATA.is_dir():
os.makedirs(WWW_DATA, exist_ok=True)
return send_from_directory(WWW_DATA, path)


@api.route("/tasks/extract", methods=["POST"])
Expand All @@ -35,3 +35,10 @@ def info():
"buildDate": BUILD_DATE.isoformat(),
}
)


def build(wwwdata: Path | None = None):
if wwwdata:
global WWW_DATA
WWW_DATA = wwwdata
return api
18 changes: 16 additions & 2 deletions src/servers/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import click
from pathlib import Path


@click.command()
@click.option(
"--data",
type=click.Path(
exists=True,
file_okay=False,
resolve_path=True,
dir_okay=True,
path_type=Path,
),
default=None,
envvar="AEXPY_SERVER_DATA",
help="Path to data storage directory.",
)
@click.option("-d", "--debug", is_flag=True, help="Debug mode.")
@click.option("-p", "--port", type=int, default=8008, help="Port to listen on.")
def serve(debug: "bool" = False, port: "int" = 8008):
def serve(data: Path | None = None, debug: bool = False, port: int = 8008):
"""Serve web server."""
from .entrypoint import serve as inner, buildApp

inner(buildApp(), debug, port)
inner(buildApp(data), debug, port)
8 changes: 4 additions & 4 deletions src/servers/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pathlib
from pathlib import Path
import shutil

import click
Expand All @@ -9,12 +9,12 @@
from flask import Flask


def buildApp():
def buildApp(wwwdata: Path | None = None):
from . import app
from .api import api
from .api import build
from .frontend import frontend

app.register_blueprint(api, url_prefix="/api")
app.register_blueprint(build(wwwdata), url_prefix="/api")
app.register_blueprint(frontend, url_prefix="/")

return app
Expand Down
4 changes: 2 additions & 2 deletions src/servers/frontend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pathlib
from pathlib import Path

from flask import Blueprint, send_file, send_from_directory

wwwroot = pathlib.Path(__file__).parent.joinpath("wwwroot")
wwwroot = Path(__file__).parent.joinpath("wwwroot")

frontend = Blueprint("frontend", __name__)

Expand Down

0 comments on commit 9953a93

Please sign in to comment.