generated from Luis/nextjs-python-web-template
Fixed incorrect imports
This commit is contained in:
@@ -5,7 +5,7 @@ from types import ModuleType
|
||||
from typing import Optional
|
||||
|
||||
from . import webui
|
||||
from .custom_logger import register
|
||||
from .custom_logger import setup_logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -30,10 +30,6 @@ def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser:
|
||||
parser_webui = subparsers.add_parser("webui", help="start webui")
|
||||
webui.register_parser(parser_webui)
|
||||
|
||||
# if args.debug:
|
||||
register(logging.DEBUG)
|
||||
log.debug("Debug log activated")
|
||||
|
||||
if argcomplete:
|
||||
argcomplete.autocomplete(parser)
|
||||
|
||||
@@ -47,6 +43,12 @@ def main() -> None:
|
||||
parser = create_parser()
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
setup_logging(logging.DEBUG)
|
||||
log.debug("Debug log activated")
|
||||
else:
|
||||
setup_logging(logging.INFO)
|
||||
|
||||
if not hasattr(args, "func"):
|
||||
return
|
||||
|
||||
|
||||
12
pkgs/clan-cli/clan_cli/config.py
Normal file
12
pkgs/clan-cli/clan_cli/config.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import dataclasses
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Config:
|
||||
host: str
|
||||
port_dlg: int
|
||||
port_ap: int
|
||||
port_client_base: int
|
||||
|
||||
|
||||
config = Config(host="127.0.0.1", port_dlg=6000, port_ap=6600, port_client_base=7000)
|
||||
@@ -61,10 +61,19 @@ def get_caller() -> str:
|
||||
return ret
|
||||
|
||||
|
||||
def register(level: Any) -> None:
|
||||
handler = logging.StreamHandler()
|
||||
handler.setLevel(level)
|
||||
handler.setFormatter(CustomFormatter())
|
||||
logger = logging.getLogger("registerHandler")
|
||||
logger.addHandler(handler)
|
||||
# logging.basicConfig(level=level, handlers=[handler])
|
||||
def setup_logging(level: Any) -> None:
|
||||
# Get the root logger and set its level
|
||||
main_logger = logging.getLogger("clan_cli")
|
||||
main_logger.setLevel(level)
|
||||
|
||||
# Create and add the default handler
|
||||
default_handler = logging.StreamHandler()
|
||||
|
||||
# Create and add your custom handler
|
||||
default_handler.setLevel(level)
|
||||
default_handler.setFormatter(CustomFormatter())
|
||||
main_logger.addHandler(default_handler)
|
||||
|
||||
# Set logging level for other modules used by this module
|
||||
logging.getLogger("asyncio").setLevel(logging.INFO)
|
||||
logging.getLogger("httpx").setLevel(level=logging.WARNING)
|
||||
|
||||
@@ -5,11 +5,20 @@ import urllib
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
from .config import config
|
||||
|
||||
app_dlg = FastAPI()
|
||||
app_ap = FastAPI()
|
||||
app_c1 = FastAPI()
|
||||
app_c2 = FastAPI()
|
||||
|
||||
apps = [
|
||||
(app_dlg, config.port_dlg),
|
||||
(app_ap, config.port_ap),
|
||||
(app_c1, config.port_client_base),
|
||||
(app_c2, config.port_client_base + 1),
|
||||
]
|
||||
|
||||
# bash tests: curl localhost:6600/ap_list_of_services
|
||||
# curl localhost:7001/consume_service_from_other_entity
|
||||
|
||||
@@ -40,9 +49,9 @@ async def healthcheck_ap() -> str:
|
||||
def get_health(*, url: str, max_retries: int = 20, delay: float = 0.2) -> str | None:
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
with urllib.request.urlopen(url) as response:
|
||||
with urllib.request.urlopen(url) as response: # type: ignore
|
||||
return response.read()
|
||||
except urllib.error.URLError as e:
|
||||
except urllib.error.URLError as e: # type: ignore
|
||||
print(f"Attempt {attempt + 1} failed: {e.reason}", file=sys.stderr)
|
||||
time.sleep(delay)
|
||||
return None
|
||||
@@ -1,11 +1,15 @@
|
||||
import argparse
|
||||
import logging
|
||||
from typing import Callable, NoReturn, Optional
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
start_server: Optional[Callable] = None
|
||||
ServerImportError: Optional[ImportError] = None
|
||||
try:
|
||||
from .server import start_server
|
||||
except ImportError as e:
|
||||
log.exception(e)
|
||||
ServerImportError = e
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import argparse
|
||||
import logging
|
||||
import multiprocessing as mp
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
@@ -14,6 +15,7 @@ import uvicorn
|
||||
from pydantic import AnyUrl, IPvAnyAddress
|
||||
from pydantic.tools import parse_obj_as
|
||||
|
||||
from clan_cli.emulate_fastapi import apps, get_health
|
||||
from clan_cli.errors import ClanError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -127,28 +129,17 @@ def start_server(args: argparse.Namespace) -> None:
|
||||
subprocess.run(cmd, check=True)
|
||||
|
||||
if args.emulate:
|
||||
import multiprocessing as mp
|
||||
|
||||
from config import host, port_ap, port_client_base, port_dlg
|
||||
from emulate_fastapi import app_ap, app_c1, app_c2, app_dlg, get_health
|
||||
|
||||
app_ports = [
|
||||
(app_dlg, port_dlg),
|
||||
(app_ap, port_ap),
|
||||
(app_c1, port_client_base),
|
||||
(app_c2, port_client_base + 1),
|
||||
]
|
||||
urls = list()
|
||||
# start servers as processes (dlg, ap, c1 and c2 for tests)
|
||||
for app, port in app_ports:
|
||||
for app, port in apps:
|
||||
proc = mp.Process(
|
||||
target=uvicorn.run,
|
||||
args=(app,),
|
||||
kwargs={"host": host, "port": port, "log_level": "info"},
|
||||
kwargs={"host": args.host, "port": port, "log_level": "info"},
|
||||
daemon=True,
|
||||
)
|
||||
proc.start()
|
||||
urls.append(f"http://{host}:{port}")
|
||||
urls.append(f"http://{args.host}:{port}")
|
||||
# check server health
|
||||
for url in urls:
|
||||
res = get_health(url=url + "/health")
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
host = "127.0.0.1"
|
||||
port_dlg = 6000
|
||||
port_ap = 6600
|
||||
port_client_base = 7000
|
||||
@@ -11,7 +11,7 @@ from fastapi.testclient import TestClient
|
||||
from openapi_client import ApiClient, Configuration
|
||||
from ports import PortFunction
|
||||
|
||||
import config
|
||||
from clan_cli.config import config
|
||||
from clan_cli.webui.app import app
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ from openapi_client.models import (
|
||||
Status,
|
||||
)
|
||||
|
||||
import config
|
||||
from clan_cli.config import config
|
||||
|
||||
random.seed(42)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user