#!/usr/bin/python3
"""inventory-api — answers /health from the config config-sync prepares in /run."""
import http.server
import json

CONFIG = "/run/inventory/config.json"
with open(CONFIG) as f:  # fails at boot unless config-sync ran first
    config = json.load(f)


class Handler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        body = json.dumps({"status": "ok", "items": len(config.get("items", []))}).encode()
        self.send_response(200 if self.path == "/health" else 404)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, fmt, *args):
        pass


http.server.HTTPServer(("0.0.0.0", 8081), Handler).serve_forever()
