switch to uvicorn for custom logging
Browse files- Dockerfile +1 -1
- fetch.py +19 -4
- pyproject.toml +1 -1
Dockerfile
CHANGED
@@ -24,4 +24,4 @@ RUN pixi run uv sync --project .
|
|
24 |
|
25 |
# HELLO WORLD
|
26 |
EXPOSE 7860
|
27 |
-
CMD .venv/bin/
|
|
|
24 |
|
25 |
# HELLO WORLD
|
26 |
EXPOSE 7860
|
27 |
+
CMD .venv/bin/python -m fetch
|
fetch.py
CHANGED
@@ -4,12 +4,15 @@ from contextlib import asynccontextmanager
|
|
4 |
from datetime import datetime
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import FileResponse, PlainTextResponse, Response
|
|
|
7 |
from typing import Any
|
8 |
|
9 |
import httpx
|
10 |
import json
|
11 |
import subprocess
|
12 |
|
|
|
|
|
13 |
|
14 |
class PrettyJSONResponse(Response):
|
15 |
media_type = "application/json"
|
@@ -46,7 +49,7 @@ async def log_request(request: Request, call_next: Any):
|
|
46 |
indent=None,
|
47 |
separators=(", ", ":"),
|
48 |
)
|
49 |
-
with open(
|
50 |
separator = "\n" if f.tell() else ""
|
51 |
f.write(separator + output)
|
52 |
|
@@ -93,9 +96,9 @@ def read_root(request: Request):
|
|
93 |
@app.get("/a", response_class=PrettyJSONResponse)
|
94 |
def get_analytics(n: int = 5):
|
95 |
if n == 0:
|
96 |
-
cmd = "cat
|
97 |
else:
|
98 |
-
cmd = f"tail -{n}
|
99 |
json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
|
100 |
content = json.loads(f"[{json_lines.replace(b"\n", b",").decode()}]")
|
101 |
return content
|
@@ -103,7 +106,7 @@ def get_analytics(n: int = 5):
|
|
103 |
|
104 |
@app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
|
105 |
def query_analytics():
|
106 |
-
return
|
107 |
|
108 |
|
109 |
@app.get("/favicon.ico")
|
@@ -123,3 +126,15 @@ async def self_ping():
|
|
123 |
|
124 |
scheduler = AsyncIOScheduler(executors={"default": AsyncIOExecutor()})
|
125 |
scheduler.add_job(self_ping, "interval", minutes=60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from datetime import datetime
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import FileResponse, PlainTextResponse, Response
|
7 |
+
from pathlib import Path
|
8 |
from typing import Any
|
9 |
|
10 |
import httpx
|
11 |
import json
|
12 |
import subprocess
|
13 |
|
14 |
+
LOGFILE = Path.home() / "a.json"
|
15 |
+
|
16 |
|
17 |
class PrettyJSONResponse(Response):
|
18 |
media_type = "application/json"
|
|
|
49 |
indent=None,
|
50 |
separators=(", ", ":"),
|
51 |
)
|
52 |
+
with open(LOGFILE, "a") as f:
|
53 |
separator = "\n" if f.tell() else ""
|
54 |
f.write(separator + output)
|
55 |
|
|
|
96 |
@app.get("/a", response_class=PrettyJSONResponse)
|
97 |
def get_analytics(n: int = 5):
|
98 |
if n == 0:
|
99 |
+
cmd = f"cat {LOGFILE.as_posix()}"
|
100 |
else:
|
101 |
+
cmd = f"tail -{n} {LOGFILE.as_posix()}"
|
102 |
json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
|
103 |
content = json.loads(f"[{json_lines.replace(b"\n", b",").decode()}]")
|
104 |
return content
|
|
|
106 |
|
107 |
@app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
|
108 |
def query_analytics():
|
109 |
+
return LOGFILE.as_posix()
|
110 |
|
111 |
|
112 |
@app.get("/favicon.ico")
|
|
|
126 |
|
127 |
scheduler = AsyncIOScheduler(executors={"default": AsyncIOExecutor()})
|
128 |
scheduler.add_job(self_ping, "interval", minutes=60)
|
129 |
+
|
130 |
+
|
131 |
+
if __name__ == "__main__":
|
132 |
+
import uvicorn
|
133 |
+
|
134 |
+
fmt = "%(asctime)s %(levelprefix)s %(message)s"
|
135 |
+
uvicorn_logging = uvicorn.config.LOGGING_CONFIG
|
136 |
+
uvicorn_logging["formatters"]["access"]["datefmt"] = "%y%m%d @ %T"
|
137 |
+
uvicorn_logging["formatters"]["access"]["fmt"] = fmt
|
138 |
+
uvicorn_logging["formatters"]["default"]["datefmt"] = "%y%m%d @ %T"
|
139 |
+
uvicorn_logging["formatters"]["default"]["fmt"] = fmt
|
140 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
pyproject.toml
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[project]
|
2 |
name = "fetch"
|
3 |
-
version = "0.3.
|
4 |
description = "Puppy Installer"
|
5 |
authors = [
|
6 |
{ name = "Alex Kislukhin" }
|
|
|
1 |
[project]
|
2 |
name = "fetch"
|
3 |
+
version = "0.3.2"
|
4 |
description = "Puppy Installer"
|
5 |
authors = [
|
6 |
{ name = "Alex Kislukhin" }
|