pup-py commited on
Commit
7de90bf
·
1 Parent(s): 364dead
Files changed (2) hide show
  1. fetch.py +25 -29
  2. pyproject.toml +1 -1
fetch.py CHANGED
@@ -32,34 +32,6 @@ async def lifespan(app: FastAPI):
32
  app = FastAPI(lifespan=lifespan)
33
 
34
 
35
- @app.middleware("http")
36
- async def log_request(request: Request, call_next: Any):
37
- ts = datetime.now().strftime("%y%m%d%H%M%S%f")
38
- data = {
39
- # "day": int(ts[:6]),
40
- "dt": int(ts[:-3]),
41
- "url": request.url,
42
- "query_params": request.query_params,
43
- "user-agent": request.headers.get("user-agent"),
44
- "client": request.headers.get("x-forwarded-for"),
45
- "private_ip": request.client.host,
46
- "method": request.method,
47
- "headers": str(request.headers),
48
- }
49
- output = json.dumps(
50
- obj=data,
51
- default=str,
52
- indent=None,
53
- separators=(", ", ":"),
54
- )
55
- with open(LOGFILE, "a") as f:
56
- separator = "\n" if f.tell() else ""
57
- f.write(separator + output)
58
-
59
- response = await call_next(request)
60
- return response
61
-
62
-
63
  @app.get("/")
64
  def read_root(request: Request):
65
  """Main URL returning an executable installer script.
@@ -106,6 +78,30 @@ def read_root(request: Request):
106
  return PlainTextResponse("\n".join(script))
107
 
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  @app.get("/a", response_class=PrettyJSONResponse)
110
  def get_analytics(n: int = 5):
111
  if n == 0:
@@ -113,7 +109,7 @@ def get_analytics(n: int = 5):
113
  else:
114
  cmd = f"tail -{n} {LOGFILE.as_posix()} | tac"
115
  json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
116
- content = json.loads(f"[{json_lines.replace(b"\n", b",").decode()}]")
117
  return content
118
 
119
 
 
32
  app = FastAPI(lifespan=lifespan)
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  @app.get("/")
36
  def read_root(request: Request):
37
  """Main URL returning an executable installer script.
 
78
  return PlainTextResponse("\n".join(script))
79
 
80
 
81
+ @app.middleware("http")
82
+ async def log_request(request: Request, call_next: Any):
83
+ ts = datetime.now().strftime("%y%m%d%H%M%S%f")
84
+ data = {
85
+ # "day": int(ts[:6]),
86
+ "dt": int(ts[:-3]),
87
+ "url": request.url,
88
+ "query_params": request.query_params,
89
+ "user-agent": request.headers.get("user-agent"),
90
+ "client": request.headers.get("x-forwarded-for"),
91
+ "private_ip": request.client.host,
92
+ "method": request.method,
93
+ "headers": str(request.headers),
94
+ }
95
+ output = json.dumps(
96
+ obj=data, default=str, indent=None, separators=(", ", ":"), newline="\n"
97
+ )
98
+ with open(LOGFILE, "a") as f:
99
+ f.write(output)
100
+
101
+ response = await call_next(request)
102
+ return response
103
+
104
+
105
  @app.get("/a", response_class=PrettyJSONResponse)
106
  def get_analytics(n: int = 5):
107
  if n == 0:
 
109
  else:
110
  cmd = f"tail -{n} {LOGFILE.as_posix()} | tac"
111
  json_lines = subprocess.run(cmd.split(), capture_output=True).stdout
112
+ content = json.loads(f"[ {json_lines.replace(b"\n", b",").decode()} ]")
113
  return content
114
 
115
 
pyproject.toml CHANGED
@@ -1,6 +1,6 @@
1
  [project]
2
  name = "fetch"
3
- version = "0.4.0"
4
  description = "Puppy Installer"
5
  authors = [
6
  { name = "Alex Kislukhin" }
 
1
  [project]
2
  name = "fetch"
3
+ version = "0.4.1"
4
  description = "Puppy Installer"
5
  authors = [
6
  { name = "Alex Kislukhin" }