From 8f96bdec3ddcf32329b358d540b05b64f6b74016 Mon Sep 17 00:00:00 2001 From: Enju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com> Date: Fri, 22 Apr 2022 19:49:23 +0200 Subject: [PATCH] added frontend --- README.md | 4 +--- api.py | 23 +++++++++++++++--- index.html | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 +- 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 index.html diff --git a/README.md b/README.md index 00b0122..03fff23 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,7 @@ systemctl enable --now fedi_block_api ## Try it out -https://chizu.love/fedi-block-api/info - -https://chizu.love/fedi-block-api/domain/ {domain} +https://chizu.love/fedi-block-api ## License diff --git a/api.py b/api.py index 713e041..1f29693 100644 --- a/api.py +++ b/api.py @@ -1,9 +1,12 @@ -from fastapi import FastAPI +from fastapi import FastAPI, Request, HTTPException import sqlite3 from hashlib import sha256 +from fastapi.templating import Jinja2Templates +from requests import get base_url = "" app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc") +templates = Jinja2Templates(directory=".") def get_hash(domain: str) -> str: return sha256(domain.encode("utf-8")).hexdigest() @@ -22,8 +25,10 @@ def info(): "source_code": "https://gitlab.com/EnjuAihara/fedi-block-api", } -@app.get(base_url+"/domain/{domain}") -def blocked(domain: str): +@app.get(base_url+"/api") +def blocked(domain: str = None): + if domain == None: + raise HTTPException(status_code=400, detail="No domain specified") conn = sqlite3.connect("blocks.db") c = conn.cursor() wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) @@ -48,3 +53,15 @@ def blocked(domain: str): return {"blocks": result, "reasons": reasons} +@app.get(base_url+"/") +def index(request: Request, domain: str = None): + blocks = get(f"http://127.0.0.1:8069/api?domain={domain}") + info = None + if domain == None: + info = get(f"http://127.0.0.1:8069/info") + if not info.ok: + raise HTTPException(status_code=info.status_code, detail=info.text) + info = info.json() + if not blocks.ok: + raise HTTPException(status_code=blocks.status_code, detail=blocks.text) + return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks.json(), "info": info}) diff --git a/index.html b/index.html new file mode 100644 index 0000000..607ca73 --- /dev/null +++ b/index.html @@ -0,0 +1,61 @@ + + + fedi-block-api {{domain}} + + + + {% if domain %} +

Instances that block {{domain}}

+ {% for block_level in blocks.blocks %} +
+

{{block_level}} ({{blocks.blocks[block_level]|length}})

+ {% for block in blocks.blocks[block_level] %} +
+ + {{block}}
+ {% if block_level in blocks.reasons %} + {{blocks.reasons[block_level][block]}} + {% endif %} +
+ {% endfor %} +
+ {% endfor %} + {% else %} +

Enter a Domain

+
+ + +
+
+ known instances: {{info.known_instances}}
+ indexed instances: {{info.indexed_instances}}
+ blocks recorded: {{info.blocks_recorded}}
+ source code: {{info.source_code}} +
+ {% endif %} + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fd95781..519ef47 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ beautifulsoup4 fastapi uvicorn requests - +jinja2