added frontend

This commit is contained in:
Enju Aihara 2022-04-22 19:49:23 +02:00
parent 8e4ba7958e
commit 8f96bdec3d
4 changed files with 83 additions and 7 deletions

View file

@ -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

23
api.py
View file

@ -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})

61
index.html Normal file
View file

@ -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>

View file

@ -2,4 +2,4 @@ beautifulsoup4
fastapi
uvicorn
requests
jinja2