-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_logger.py
70 lines (50 loc) · 1.62 KB
/
middleware_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# API to records & serve logs from sniffer
# API libs
import json
from fastapi import Body, FastAPI
from fastapi.responses import FileResponse
from uvicorn import run
# Object Serialization
from pydantic import BaseModel
# Database Lib
from redis import Redis
# Fake hash generation
import string
import random
import sys
class Log(BaseModel):
class Config:
arbitrary_types_allowed = True
timestamp : float
pkt : str
info : dict
#fastapi init
app : FastAPI = FastAPI()
# Deliver shards id to API client as list
# @app.get("/shards")
# async def serve_shards_id():
# shards : dict = db.json().jsonmget(["shard:"])
# return {"logs":logs}
# @app.get("/shard/{shard_id}")
# async def serve_shard(shard_id:str):
# Deliver logs to json for API client
@app.get("/logs")
async def serve_logs():
logs : dict[int,Log] = db.json().get("shard:WQZBK7TFKFLB60E9CYCQXA08I1")
return {"logs":logs}
# Get 64 (or more) packets from logger
@app.post("/rlog")
async def download_logs(payload: dict = Body(...)):
data_list : list[str] = payload["logs_stack"]
shards_list : list[dict] = []
for value in data_list:
shards_list.append(json.loads(value))
chars = string.ascii_uppercase + string.digits
db.json().set(name="shard:%s"%(''.join(random.choices(chars, k=26))), path='$', obj={"shard":shards_list})
return {"API":"added %s elements"%(payload.__len__())}
if __name__ == "__main__":
db = Redis(host='localhost', port=6379, db=1)
#redis join test
try: db.ping()
except: print("REDIS CAN'T BE JOINED !"); sys.exit(1)
run(app=app,host="localhost",port=8080)