]> git.mxchange.org Git - fba.git/commitdiff
added frontend
authorEnju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com>
Fri, 22 Apr 2022 17:49:23 +0000 (19:49 +0200)
committerEnju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com>
Fri, 22 Apr 2022 17:49:23 +0000 (19:49 +0200)
README.md
api.py
index.html [new file with mode: 0644]
requirements.txt

index 00b01223a51fd7370373aace109b29d711f21b7f..03fff23d0e7f4b1459b3199641b363fc06ac520d 100644 (file)
--- 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 713e0413f85adda64f390530a830cdbab4d8cc1a..1f29693e0b2693b607bdbb247e447a6061247a43 100644 (file)
--- 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 (file)
index 0000000..607ca73
--- /dev/null
@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<head>
+    <title>fedi-block-api {{domain}}</title>
+    <style>
+        body {
+            background-color: #000022;
+            color: #ffffff;
+            text-align: center;
+        }
+        .block_level {
+            background-color: #1c1c3c;
+            width: 750px;
+            padding: 5px;
+            margin: auto;
+            margin-top: 10px;
+        }
+        .block {
+            background-color: #2d2d4d;
+            padding: 5px;
+            margin: 5px;
+        }
+        a {
+            color: #ffffff;
+        }
+        .info {
+            margin-top: 50px;
+        }
+    </style>
+</head>
+<body>
+    {% if domain %}
+        <h1>Instances that block {{domain}}</h1>
+        {% for block_level in blocks.blocks %}
+            <div class="block_level">
+                <h2>{{block_level}} ({{blocks.blocks[block_level]|length}})</h2>
+                {% for block in blocks.blocks[block_level] %}
+                    <div class="block">
+                        <img src="https://chizu.love/fedi-block-api/favicons/{{block}}.png" width=16/>
+                        <b><a href="https://{{block}}">{{block}}</a></b><br/>
+                        {% if block_level in blocks.reasons %}
+                            {{blocks.reasons[block_level][block]}}
+                        {% endif %}
+                    </div>
+                {% endfor %}
+            </div>
+        {% endfor %}
+    {% else %}
+        <h1>Enter a Domain</h1>
+        <form>
+            <input type="text" name="domain" placeholder="example.com" />
+            <input type="submit" value="Submit" />
+        </form>
+        <div class="info">
+            known instances: {{info.known_instances}}<br/>
+            indexed instances: {{info.indexed_instances}}<br/>
+            blocks recorded: {{info.blocks_recorded}}<br/>
+            source code: <a href="{{info.source_code}}">{{info.source_code}}</a>
+        </div>
+    {% endif %}
+</body>
+</html>
\ No newline at end of file
index fd95781d5e7df07dabb51746e9d61d14f5396553..519ef47e345207aad6e0e70ebcbd6603e58dad2a 100644 (file)
@@ -2,4 +2,4 @@ beautifulsoup4
 fastapi
 uvicorn
 requests
-
+jinja2