refactor: update all API blueprints (certs, dhcp, firewall, logs, network, proxy, wireguard)

This commit is contained in:
2026-06-16 03:36:51 +00:00
parent 4fc0fb3f72
commit 708b8b5d15
7 changed files with 199 additions and 149 deletions
+32 -76
View File
@@ -1,116 +1,72 @@
"""Log viewing API blueprint.
Serves log content to the /logs page via HTMX endpoints through vacuum-walld.
Wraps raw log text in the standard JSON response contract.
"""
import logging
from flask import Blueprint, render_template_string
from flask import Blueprint
from daemon.client import get
from daemon.client import NotFound, get
from daemon.iface import (
GET_LOGS_APP,
GET_LOGS_DNSMASQ,
GET_LOGS_JOURNAL,
GET_LOGS_NGINX_ACCESS,
GET_LOGS_NGINX_ERROR,
)
from webui.api.common import _error, _ok
logger = logging.getLogger(__name__)
bp = Blueprint("logs", __name__)
_LOG_LINE_TEMPLATE = """\
{% for line in lines %}
<div class="log-line{% if 'ERROR' in line %} log-error{% elif 'WARN' in line %} log-warn{% endif %}">{{ line | e }}</div>
{% endfor %}"""
def _render_log_lines(text: str) -> str:
"""Render raw log text into styled HTML log-line divs.
Args:
text: Raw log content with newline-separated lines.
Returns:
HTML string with color-coded log-line elements.
"""
lines = text.rstrip("\n").split("\n") if text.strip() else []
return render_template_string(_LOG_LINE_TEMPLATE, lines=lines)
@bp.route("/journal")
def journal():
"""GET /api/logs/journal — Return systemd journal log lines.
Fetches the daemon's journal log content via vacuum-walld and
renders it as styled HTML log-line elements.
Returns:
HTML string containing rendered journal log lines.
"""
"""GET /api/logs/journal — Return systemd journal log lines."""
try:
text = get("/logs/journal")
return _render_log_lines(text)
return _ok(get(GET_LOGS_JOURNAL))
except RuntimeError:
return _render_log_lines("(error reading journal)\n")
return _error("error reading journal", 500)
@bp.route("/nginx/access")
def nginx_access():
"""GET /api/logs/nginx/access — Return nginx access log lines.
Fetches the nginx access log content via vacuum-walld and
renders it as styled HTML log-line elements.
Returns:
HTML string containing rendered access log lines.
"""
"""GET /api/logs/nginx/access — Return nginx access log lines."""
try:
text = get("/logs/nginx/access")
return _render_log_lines(text)
return _ok(get(GET_LOGS_NGINX_ACCESS))
except NotFound:
return _error("log file not found", 404)
except RuntimeError:
return _render_log_lines("(log file not found)\n")
return _error("error reading log", 500)
@bp.route("/nginx/error")
def nginx_error():
"""GET /api/logs/nginx/error — Return nginx error log lines.
Fetches the nginx error log content via vacuum-walld and
renders it as styled HTML log-line elements.
Returns:
HTML string containing rendered error log lines.
"""
"""GET /api/logs/nginx/error — Return nginx error log lines."""
try:
text = get("/logs/nginx/error")
return _render_log_lines(text)
return _ok(get(GET_LOGS_NGINX_ERROR))
except NotFound:
return _error("log file not found", 404)
except RuntimeError:
return _render_log_lines("(log file not found)\n")
return _error("error reading log", 500)
@bp.route("/dnsmasq")
def dnsmasq():
"""GET /api/logs/dnsmasq — Return dnsmasq log lines.
Fetches the dnsmasq log content via vacuum-walld and
renders it as styled HTML log-line elements.
Returns:
HTML string containing rendered dnsmasq log lines.
"""
"""GET /api/logs/dnsmasq — Return dnsmasq log lines."""
try:
text = get("/logs/dnsmasq")
return _render_log_lines(text)
return _ok(get(GET_LOGS_DNSMASQ))
except RuntimeError:
return _render_log_lines("(error reading journal)\n")
return _error("error reading journal", 500)
@bp.route("/app")
def app_log():
"""GET /api/logs/app — Return application log lines.
Fetches the application log content via vacuum-walld and
renders it as styled HTML log-line elements.
Returns:
HTML string containing rendered application log lines.
"""
"""GET /api/logs/app — Return application log lines."""
try:
text = get("/logs/app")
return _render_log_lines(text)
return _ok(get(GET_LOGS_APP))
except NotFound:
return _error("log file not found", 404)
except RuntimeError:
return _render_log_lines("(log file not found)\n")
return _error("error reading log", 500)