traceroute_map/app/main.py

68 lines
1.5 KiB
Python
Raw Permalink Normal View History

from fastapi import Request, FastAPI
from fastapi.staticfiles import StaticFiles
from .collector import parse_traceroute_output, store_traceroute
from .db import Database, ensure_table_setup
from pprint import pprint as print
# Setup our SQLite before anything else.
ensure_table_setup()
# Setup web framework thingies
app = FastAPI()
app.mount("/static", StaticFiles(directory="app/static"), name="static")
@app.get("/")
def read_root():
return {
"greeting": "Hello, Kalzu!",
"instructions": [
"Try piping traceroute data directly to POST /trace/{hostname}.",
"{hostname} is for filtering data by sender.",
"For example the following command using HTTPie:",
"",
" $ traceroute peitto.info | http POST localhost:8000/trace/rekku",
"",
"",
"Also take a look at /docs/ and /static/index.html",
"",
"",
"",
"END OF TRANSMISSION",
]
+ [None] * 800,
}
@app.get("/trace/")
def list_traces():
db = Database()
trace = db.list_traces()
db.end()
return trace
@app.post("/trace/{origin}")
async def create_trace(origin: str, request: Request):
raw_data = await request.body()
data = raw_data.decode("utf-8", "ignore")
print(f"Received data from {origin}:")
print(data)
trace = parse_traceroute_output(data, origin)
print("Parsed data:")
print(trace)
db = Database()
db.create_trace(trace)
db.end()
return {"status": "ok"}