From: Roland Häder Date: Tue, 4 Jul 2023 16:42:21 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=48a897a32ad4af66dd1509d8d945ef9d8cafcd5a;p=fba.git Continued: - rewrote to fetch domain data over internal JSON API (seem to be not cached?) - avoid local variable --- diff --git a/daemon.py b/daemon.py index cdc6ef5..89b288f 100755 --- a/daemon.py +++ b/daemon.py @@ -190,6 +190,23 @@ LIMIT ?", [ return result +@router.get(config.get("base_url") + "/api/domain.json", response_class=JSONResponse) +def api_domain(domain: str): + # Tidy up domain name + domain = tidyup.domain(domain) + + if not utils.is_domain_wanted(domain): + raise HTTPException(status_code=500, detail=f"domain='{domain}' is not wanted") + + # Fetch domain data + database.cursor.execute("SELECT * FROM instances WHERE domain = ? LIMIT 1", [domain]) + domain_data = database.cursor.fetchone() + + if domain_data is None: + raise HTTPException(status_code=404, detail=f"domain='{domain}' not found") + + return domain_data + @router.get(config.get("base_url") + "/api/mutual.json", response_class=JSONResponse) def api_mutual(domains: list[str] = Query()): """Return 200 if federation is open between the two, 4xx otherwise""" @@ -205,9 +222,8 @@ def api_mutual(domains: list[str] = Query()): "bw": "*." + domains[1], }, ) - response = database.cursor.fetchone() - if response is not None: + if database.cursor.fetchone() is not None: # Blocks found return JSONResponse(status_code=418, content={}) @@ -286,7 +302,6 @@ def top(request: Request, mode: str, value: str, amount: int = config.get("api_l raise HTTPException(status_code=500, detail=f"amount='{amount}' is to big") info = response.json() - response = None blocklist = list() if mode == "block_level" and not blocks.is_valid_level(value): @@ -296,7 +311,7 @@ def top(request: Request, mode: str, value: str, amount: int = config.get("api_l response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/top.json?mode={mode}&value={value}&amount={amount}") - if response is not None: + if response is not None and response.ok: blocklist = response.json() found = 0 @@ -325,12 +340,11 @@ def rss(request: Request, domain: str): if not utils.is_domain_wanted(domain): raise HTTPException(status_code=500, detail=f"domain='{domain}' is not wanted") - # Fetch domain data - database.cursor.execute("SELECT * FROM instances WHERE domain = ? LIMIT 1", [domain]) - domain_data = database.cursor.fetchone() + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/domain.json?domain={domain}") - if domain_data is None: - raise HTTPException(status_code=404, detail=f"domain='{domain}' not found") + domain_data = dict() + if response is not None and response.ok: + domain_data = response.json() # Format timestamps format = config.get("timestamp_format") diff --git a/fba/models/instances.py b/fba/models/instances.py index 1294a01..0c39953 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -126,6 +126,7 @@ def update_data(domain: str): fields.append(time.time()) # For WHERE statement + logger.debug("Setting domain='%s' for WHERE statement ...", domain) fields.append(domain) logger.debug("sql_string='%s',fields()=%d", sql_string, len(fields))