From: Roland Häder <roland@mxchange.org> Date: Mon, 29 May 2023 18:58:44 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ab9778ff76580cb17aa79d3e654466a9b1e06728;p=fba.git Continued: - splitted "scoreboard.html" out of index.html - added route + template for "robots.txt request - renamed "app" to "router" --- diff --git a/api.py b/api.py index fdf5b07..7b18ead 100644 --- a/api.py +++ b/api.py @@ -15,6 +15,7 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. from fastapi import Request, HTTPException, responses, Query +from fastapi.responses import PlainTextResponse from fastapi.templating import Jinja2Templates from datetime import datetime from email import utils @@ -25,10 +26,10 @@ import requests import re import fba -app = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") +router = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") templates = Jinja2Templates(directory="templates") -@app.get(fba.config["base_url"] + "/api/info") +@router.get(fba.config["base_url"] + "/api/info") def info(): fba.cursor.execute("SELECT (SELECT COUNT(domain) FROM instances), (SELECT COUNT(domain) FROM instances WHERE software IN ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica', 'bookwyrm', 'takahe', 'peertube')), (SELECT COUNT(blocker) FROM blocks), (SELECT COUNT(domain) FROM instances WHERE last_status_code IS NOT NULL)") known, indexed, blocks, errorous = fba.cursor.fetchone() @@ -41,7 +42,7 @@ def info(): "slogan" : fba.config["slogan"] } -@app.get(fba.config["base_url"] + "/api/top") +@router.get(fba.config["base_url"] + "/api/top") def top(blocked: int = None, blockers: int = None, reference: int = None, software: int = None): if blocked != None: if blocked > 500: @@ -74,7 +75,7 @@ def top(blocked: int = None, blockers: int = None, reference: int = None, softwa return scoreboard -@app.get(fba.config["base_url"] + "/api") +@router.get(fba.config["base_url"] + "/api") def blocked(domain: str = None, reason: str = None, reverse: str = None): if domain == None and reason == None and reverse == None: raise HTTPException(status_code=400, detail="No filter specified") @@ -112,7 +113,7 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None): return result -@app.get(fba.config["base_url"] + "/scoreboard") +@router.get(fba.config["base_url"] + "/scoreboard") def index(request: Request, blockers: int = None, blocked: int = None, reference: int = None, software: int = None): scores = None @@ -134,6 +135,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference return templates.TemplateResponse("scoreboard.html", { "base_url" : fba.config["base_url"], + "slogan" : fba.config["slogan"], "request" : request, "scoreboard": True, "blockers" : blockers, @@ -143,7 +145,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference "scores" : res.json() }) -@app.get(fba.config["base_url"] + "/") +@router.get(fba.config["base_url"] + "/") def index(request: Request, domain: str = None, reason: str = None, reverse: str = None): if domain == "" or reason == "" or reverse == "": return responses.RedirectResponse("/") @@ -183,7 +185,7 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str "info" : info }) -@app.get(fba.config["base_url"] + "/api/mutual") +@router.get(fba.config["base_url"] + "/api/mutual") def mutual(domains: list[str] = Query()): """Return 200 if federation is open between the two, 4xx otherwise""" fba.cursor.execute( @@ -207,7 +209,7 @@ def mutual(domains: list[str] = Query()): # No known blocks return responses.JSONResponse(status_code=200, content={}) -@app.get(fba.config["base_url"] + "/rss") +@router.get(fba.config["base_url"] + "/rss") def rss(request: Request, domain: str = None): if domain != None: wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) @@ -243,8 +245,17 @@ def rss(request: Request, domain: str = None): "domain" : domain, "blocks" : result }, headers={ - "Content-Type": "application/rss+xml" + "Content-Type": "routerlication/rss+xml" + }) + +@router.get(fba.config["base_url"] + "/robots.txt", response_class=PlainTextResponse) +def robots(request: Request): + return templates.TemplateResponse("robots.txt", { + "request" : request, + "base_url": fba.config["base_url"] + }, headers={ + "Content-Type": "text/plain" }) if __name__ == "__main__": - uvicorn.run("api:app", host=fba.config["host"], port=fba.config["port"], log_level=fba.config["log_level"]) + uvicorn.run("api:router", host=fba.config["host"], port=fba.config["port"], log_level=fba.config["log_level"]) diff --git a/templates/index.html b/templates/index.html index f25d00f..39fcfcc 100644 --- a/templates/index.html +++ b/templates/index.html @@ -21,13 +21,6 @@ margin: auto; margin-top: 10px; } - .scoreboard { - background-color: #1c1c3c; - width: 40em; - padding: 5px; - margin: auto; - margin-top: 10px; - } table { width: 100%; background-color: #2d2d4d; @@ -84,38 +77,7 @@ </style> </head> <body> - {% if scoreboard %} - {% if blockers %} - <h1>Top {{blockers}} defederating instances</h1> - {% elif blocked %} - <h1>Top {{blocked}} defederated instances</h1> - {% elif reference %} - <h1>Top {{reference}} referencing instances</h1> - {% elif software %} - <h1>Top {{software}} used software</h1> - {% endif %} - <div class="scoreboard"> - <table> - <th>â</th> - <th>{% if software %}Software{% else %}Instance{% endif %}</th> - <th>{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}</th> - {% for entry in scores %} - <tr> - <td>{{loop.index}}</td> - <td> - {% if software %} - {{entry['domain']}} - {% else %} - <a href="{{base_url}}/?{% if blockers %}reverse{% elif blocked or reference %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a> - <a class="listlink" href="https://{{entry['domain']}}" rel="external" target="_blank">â</a> - {% endif %} - </td> - <td>{{entry['highscore']}}</td> - </tr> - {% endfor %} - </table> - </div> - {% elif reason or domain or reverse %} + {% if reason or domain or reverse %} {% if reason %} <h1>Instances that use "{{reason}}" in their reason</h1> {% elif reverse %} diff --git a/templates/robots.txt b/templates/robots.txt new file mode 100644 index 0000000..6fdd266 --- /dev/null +++ b/templates/robots.txt @@ -0,0 +1,2 @@ +User-Agent: * +Disallow {{base_url}}/api/ diff --git a/templates/scoreboard.html b/templates/scoreboard.html new file mode 100644 index 0000000..83e1f08 --- /dev/null +++ b/templates/scoreboard.html @@ -0,0 +1,117 @@ +<!DOCTYPE html> +<head> + <title>fedi-block-api</title> + <link rel="alternate" type="application/rss+xml" title="RSS Feed for latest blocked instances" href="{{base_url}}/rss" /> + <style> + body { + background-color: #000022; + color: #ffffff; + text-align: center; + font-family: sans; + } + .block_level { + background-color: #1c1c3c; + width: 80em; + padding: 5px; + margin: auto; + margin-top: 10px; + } + .scoreboard { + background-color: #1c1c3c; + width: 40em; + padding: 5px; + margin: auto; + margin-top: 10px; + } + table { + width: 100%; + background-color: #2d2d4d; + border-spacing: 0px; + } + table tr:nth-of-type(2n) { + background-color: #1c1c3c; + } + table td { + padding: 4px; + } + .block_level table td:nth-of-type(1), .block_level table td:nth-of-type(2), + .block_level table td:nth-of-type(4), .block_level table td:nth-of-type(5) { + white-space: nowrap; + } + .block { + background-color: #2d2d4d; + padding: 5px; + margin: 5px; + } + a { + color: #ffffff; + } + a.listlink { + text-decoration: none; + font-size: 0.8em; + } + .info { + margin-top: 25px; + } + input[type="text"], input[type="submit"] { + padding: 5px; + border-radius: 5px; + color: white; + background: #445; + font-size: 16px; + } + + input[type="text"]:hover { + border-color: #f08; + } + + input[type="submit"] { + cursor: pointer; + } + + input[type="submit"]:hover { + border-color: #f08; + } + + span[title] { + text-decoration: underline dotted; + } + </style> +</head> +<body> + {% if blockers %} + <h1>Top {{blockers}} defederating instances</h1> + {% elif blocked %} + <h1>Top {{blocked}} defederated instances</h1> + {% elif reference %} + <h1>Top {{reference}} referencing instances</h1> + {% elif software %} + <h1>Top {{software}} used software</h1> + {% endif %} + <div class="scoreboard"> + <table> + <th>â</th> + <th>{% if software %}Software{% else %}Instance{% endif %}</th> + <th>{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}</th> + {% for entry in scores %} + <tr> + <td>{{loop.index}}</td> + <td> + {% if software %} + {{entry['domain']}} + {% else %} + <a href="{{base_url}}/?{% if blockers %}reverse{% elif blocked or reference %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a> + <a class="listlink" href="https://{{entry['domain']}}" rel="external" target="_blank">â</a> + {% endif %} + </td> + <td>{{entry['highscore']}}</td> + </tr> + {% endfor %} + </table> + </div> + <div class="info"> + source code: <a href="https://git.mxchange.org/?p=fba.git;a=summary" rel="source" target="_blank">git.mxchange.org</a><br /><br /> + {{slogan}} + </div> +</body> +</html>