+import logging
+import json
+
from flask import Flask, Response, request
from redis import Redis
-import logging
r = Redis(host="redis", port=6379, decode_responses=True)
@app.route("/stats", methods=["GET"])
def stats():
try:
- stats = r.scan_iter()
+ stats = r.scan_iter(match="hits:*")
if stats is None:
app.logger.debug("Uninitialized stats object")
return {}
@app.route("/api/<path:subpath>", methods=["GET"])
def api(subpath):
app.logger.debug("subpath %s", subpath)
+ path_parts = subpath.split("/")
+ if len(path_parts) < 1 or len(path_parts) > 6:
+ return Response(
+ response=json.dumps(
+ {
+ "error": f"Invalid API path ({subpath}). Path must contain 1 to 6 segments."
+ }
+ ),
+ status=400,
+ )
+ if any([len(part) != 3 for part in path_parts]):
+ return Response(
+ response=json.dumps(
+ {
+ "error": f"Invalid API path ({subpath}). All segments must be 3 characters long."
+ }
+ ),
+ status=400,
+ )
+ # TODO check that the subpath contains no more than 3 unique subpath strings
hits = r.incr(f"hits:{subpath}")
return {"hits": hits}