From 009d7fcab3ad8c7591cf8849464637438932489f Mon Sep 17 00:00:00 2001 From: Enju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com> Date: Fri, 22 Apr 2022 20:56:58 +0200 Subject: [PATCH] added config --- README.md | 2 ++ api.py | 14 +++++++++++--- config.defaults.json | 4 ++++ services/fedi_block_api.service | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 config.defaults.json diff --git a/README.md b/README.md index 03fff23..9cda9e9 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,10 @@ sudo -Hu fba git clone https://gitlab.com/EnjuAihara/fedi-block-api.git /opt/fed cd /opt/fedi-block-api sudo -Hu fba pip3 install -r requirements.txt sudo -Hu fba cp blocks_preloaded.db blocks.db +sudo -Hu fba cp config.defaults.json config.json ``` + ### Install the services ```bash diff --git a/api.py b/api.py index 0090f62..5f0bb53 100644 --- a/api.py +++ b/api.py @@ -1,10 +1,15 @@ +import uvicorn from fastapi import FastAPI, Request, HTTPException, responses import sqlite3 from hashlib import sha256 from fastapi.templating import Jinja2Templates from requests import get +from json import loads -base_url = "" +with open("config.json") as f: + config = loads(f.read()) + base_url = config["base_url"] + port = config["port"] app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc") templates = Jinja2Templates(directory=".") @@ -57,13 +62,16 @@ def blocked(domain: str = None): def index(request: Request, domain: str = None): if domain == "": return responses.RedirectResponse("/") - blocks = get(f"http://127.0.0.1:8069{base_url}/api?domain={domain}") + blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}") info = None if domain == None: - info = get(f"http://127.0.0.1:8069{base_url}/info") + info = get(f"http://127.0.0.1:{port}{base_url}/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}) + +if __name__ == "__main__": + uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info") diff --git a/config.defaults.json b/config.defaults.json new file mode 100644 index 0000000..d0fe2a9 --- /dev/null +++ b/config.defaults.json @@ -0,0 +1,4 @@ +{ + "base_url": "", + "port": 8069 +} diff --git a/services/fedi_block_api.service b/services/fedi_block_api.service index f2e2456..50f29d5 100644 --- a/services/fedi_block_api.service +++ b/services/fedi_block_api.service @@ -7,7 +7,7 @@ Restart=on-failure RestartSec=10 User=fba WorkingDirectory=/opt/fedi-block-api -ExecStart=uvicorn api:app --port 8069 +ExecStart=python3 api.py [Install] WantedBy=multi-user.target -- 2.39.5