capturing requests
Browse files- .gitignore +3 -0
- Dockerfile +23 -0
- fetch.py +64 -0
- pyproject.toml +12 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__
|
2 |
+
*.json
|
3 |
+
uv.lock
|
Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM debian:latest AS puppy
|
2 |
+
|
3 |
+
# prep
|
4 |
+
RUN apt-get update \
|
5 |
+
&& apt-get install -y curl \
|
6 |
+
&& apt-get clean
|
7 |
+
RUN useradd -m -u 1000 user
|
8 |
+
USER user
|
9 |
+
|
10 |
+
ENV PATH=/home/user/.pixi/bin:$PATH
|
11 |
+
RUN mkdir $HOME/puppy
|
12 |
+
WORKDIR $HOME/puppy
|
13 |
+
|
14 |
+
# install puppy
|
15 |
+
RUN curl -fsSL https://raw.githubusercontent.com/liquidcarbon/puppy/main/pup.sh | bash -s 3.12
|
16 |
+
RUN pup
|
17 |
+
RUN pup add appenv fastapi[standard]
|
18 |
+
WORKDIR appenv
|
19 |
+
|
20 |
+
COPY --chown=user *.py .
|
21 |
+
|
22 |
+
EXPOSE 7860
|
23 |
+
CMD .venv/bin/fastapi run fetch.py --host 0.0.0.0 --port 7860
|
fetch.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
from fastapi.responses import Response, FileResponse
|
4 |
+
from typing import Any
|
5 |
+
|
6 |
+
import json
|
7 |
+
import subprocess
|
8 |
+
|
9 |
+
|
10 |
+
class PrettyJSONResponse(Response):
|
11 |
+
media_type = "application/json"
|
12 |
+
|
13 |
+
def render(self, content: Any) -> bytes:
|
14 |
+
return json.dumps(content, indent=2).encode("utf-8")
|
15 |
+
|
16 |
+
|
17 |
+
app = FastAPI()
|
18 |
+
|
19 |
+
|
20 |
+
@app.middleware("http")
|
21 |
+
async def log_request(request: Request, call_next: Any):
|
22 |
+
ts = datetime.now().strftime("%y%m%d%H%M%S%f")
|
23 |
+
data = {
|
24 |
+
"day": int(ts[:6]),
|
25 |
+
"dt": int(ts[:-3]),
|
26 |
+
"url": request.url,
|
27 |
+
"query_params": request.query_params,
|
28 |
+
"client": request.client.host,
|
29 |
+
"method": request.method,
|
30 |
+
"headers": dict(request.headers),
|
31 |
+
}
|
32 |
+
output = json.dumps(
|
33 |
+
obj=data,
|
34 |
+
default=str,
|
35 |
+
indent=None,
|
36 |
+
separators=(", ", ":"),
|
37 |
+
)
|
38 |
+
with open("a.json", "a") as f:
|
39 |
+
separator = "\n" if f.tell() else ""
|
40 |
+
f.write(separator + output)
|
41 |
+
|
42 |
+
response = await call_next(request)
|
43 |
+
return response
|
44 |
+
|
45 |
+
|
46 |
+
@app.get("/")
|
47 |
+
def read_root(request: Request):
|
48 |
+
return {"Hello": request.client.host}
|
49 |
+
|
50 |
+
|
51 |
+
@app.get("/a", response_class=PrettyJSONResponse)
|
52 |
+
def get_analytics(n: int = 5):
|
53 |
+
if n == 0:
|
54 |
+
cmd = "cat a.json"
|
55 |
+
else:
|
56 |
+
cmd = f"tail -{n} a.json"
|
57 |
+
json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
|
58 |
+
content = json.loads(f"[{json_lines.replace(b"\n", b",").decode()}]")
|
59 |
+
return content
|
60 |
+
|
61 |
+
|
62 |
+
@app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
|
63 |
+
def query_analytics():
|
64 |
+
return "a.json"
|
pyproject.toml
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[project]
|
2 |
+
name = "fetch"
|
3 |
+
version = "0.1.0"
|
4 |
+
description = "Puppy installer"
|
5 |
+
authors = [
|
6 |
+
{ name = "Alex Kislukhin" }
|
7 |
+
]
|
8 |
+
readme = "README.md"
|
9 |
+
requires-python = ">=3.12"
|
10 |
+
dependencies = [
|
11 |
+
"fastapi[standard]>=0.115.6",
|
12 |
+
]
|