diff --git a/.vscode/settings.json b/.vscode/settings.json index 01a1a4d..a5d8ba9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,7 @@ "jsmin", "judgements", "KHTML", + "levelname", "minifier", "ndjson", "pdfplumber", diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 0000000..eb018c3 --- /dev/null +++ b/common/__init__.py @@ -0,0 +1 @@ +from . import utils diff --git a/common/utils.py b/common/utils.py new file mode 100644 index 0000000..d1432ba --- /dev/null +++ b/common/utils.py @@ -0,0 +1,18 @@ +import logging + + +logger = logging.getLogger(__name__) + + +def init_logging(): + logger = logging.getLogger() + logger.setLevel(logging.INFO) + + formatter = logging.Formatter( + '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') + + consoleHandler = logging.StreamHandler() + consoleHandler.setFormatter(formatter) + logger.addHandler(consoleHandler) + + return logger diff --git a/poetry.lock b/poetry.lock index 34e020f..03efa05 100644 --- a/poetry.lock +++ b/poetry.lock @@ -783,6 +783,16 @@ chardet = ["chardet (>=2.2)"] genshi = ["genshi"] lxml = ["lxml"] +[[package]] +name = "htmlmin" +version = "0.1.12" +description = "An HTML Minifier" +optional = false +python-versions = "*" +files = [ + {file = "htmlmin-0.1.12.tar.gz", hash = "sha256:50c1ef4630374a5d723900096a961cff426dff46b48f34d194a81bbe14eca178"}, +] + [[package]] name = "idna" version = "3.4" @@ -2220,4 +2230,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "7cead493d1fdd34c88cc5841954fddd4bdc0233dcbd7afdbe63444e7e731ceba" +content-hash = "dae39bd98e83423da301dbee27d895f76a9499229685a3a3cc3682adea4e1bda" diff --git a/pyproject.toml b/pyproject.toml index fb0d3f1..4f1d04f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ bs4 = "^0.0.1" asyncio = "^3.4.3" aiohttp = "^3.10.10" aiodns = "^3.2.0" +htmlmin = "^0.1.12" [tool.poetry.dev-dependencies] pytest = "^7.3.1" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 5089320..0000000 --- a/requirements.txt +++ /dev/null @@ -1,13 +0,0 @@ -autopep8 - -PyYAML -xlwt - -requests -requests-toolbelt -grequests - -bs4 -htmlmin - -locust diff --git a/xcpcio-board-cache/main.py b/xcpcio-board-cache/main.py new file mode 100644 index 0000000..28142eb --- /dev/null +++ b/xcpcio-board-cache/main.py @@ -0,0 +1,69 @@ +import asyncio +from aiohttp import web, ClientSession +import os +import logging +from typing import Dict + +from common import utils + +logger = logging.getLogger(__name__) + +HOST = os.getenv('HOST', '0.0.0.0') +PORT = int(os.getenv('PORT', 8080)) +ORIGIN_DOMAIN = os.getenv('ORIGIN_DOMAIN') + +cache_map: Dict[str, str] = {} + + +async def fetch_data(path: str, query: Dict[str, str]) -> str: + async with ClientSession() as session: + url = f'{ORIGIN_DOMAIN}/{path}' + async with session.get(url, params=query) as response: + if response.status == 200: + return await response.text() + else: + raise web.HTTPException(status=response.status) + + +async def async_update_cache(path: str, query: Dict[str, str]): + try: + data = await fetch_data(path, query) + cache_map[path] = data + logger.info(f'Update cache for {path}') + except Exception as e: + logger.error(f'Failed to update cache for {path}: {e}') + + +async def hello(request: web.Request): + return web.Response(text="Hello, World!") + + +async def cache(request: web.Request): + path = request.path + query = request.query + logger.info(f'Cache request for {path}') + + if path in cache_map: + asyncio.create_task(async_update_cache(path, query)) + return web.Response(text=cache_map[path]) + else: + data = await fetch_data(path, query) + cache_map[path] = data + return web.Response(text=data) + + +async def init_app(): + app = web.Application() + app.router.add_get('/hello', hello) + app.router.add_get('/{path:.*}', cache) + return app + + +def main(): + utils.init_logging() + app = init_app() + web.run_app(asyncio.run(app), host=HOST, port=PORT) + + +if __name__ == '__main__': + main()