Files
fancy-dns/internal/console/console.go
T
matsandClaude Sonnet 5.5 01202f9785 Console: replace the dashboard, logs and cache pages with a status page and links
With the resolver no longer storing traffic history or a query log, the pages
built on them have nothing to show. The Dashboard becomes a Status page: health
and the reasons for a degraded state, uptime, what is compiled (blocklist rules,
local zone records), the cache with a flush button, per-pod upstream health and
the listeners. Logs and Cache are removed along with the SSE client, the chart
code and the recharts dependency.

api.links (grafana, logs, metrics) names where the charts and the query log
live. GET /api/links serves them and the Status page shows buttons for the ones
that are set, or says how to set them. They are rendered as hrefs, so config
validation refuses anything but http(s) and the page checks again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 09:56:26 +02:00

122 lines
3.7 KiB
Go

// Package console is the editing and viewing half of fancy-dns, split from the
// resolver so that it can run somewhere else. It owns authentication and the
// web UI, writes ClientGroup, BlockList and DNSZone objects to the cluster, and
// forwards everything the resolver owns (statistics, the query log, the cache)
// to the resolver's admin API.
//
// The resolver never calls the console and the console is never on the DNS
// path: stopping it cannot affect a query.
package console
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/api"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/config"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/kubeedit"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/web"
)
// Options configure a Console.
type Options struct {
Config *config.Store
Runtime *Runtime
Editor *kubeedit.Editor
Logger *slog.Logger
Version string
}
// Console serves the UI and the edit API.
type Console struct {
opt Options
log *slog.Logger
api *api.Server
started time.Time
}
// New builds a Console.
func New(opt Options) (*Console, error) {
if opt.Config == nil || opt.Runtime == nil || opt.Editor == nil {
return nil, errors.New("console: config, runtime and editor are required")
}
if opt.Logger == nil {
opt.Logger = slog.Default()
}
c := &Console{opt: opt, log: opt.Logger.With("component", "console"), started: time.Now()}
srv, err := api.New(c.hooks())
if err != nil {
return nil, err
}
c.api = srv
return c, nil
}
// actor names who is making a request, for the object annotation.
func actor(ctx context.Context) string {
if p, ok := api.PrincipalFrom(ctx); ok && p.Name != "" {
return p.Name
}
return ""
}
// Handler is the whole HTTP surface: the console's own routes, the resolver's
// API behind the same authentication, and the single-page app.
func (c *Console) Handler() (http.Handler, error) {
cfg := c.opt.Config.Get()
base := strings.TrimSuffix(cfg.API.BasePath, "/")
own := c.api.Handler()
mux := http.NewServeMux()
// Handled by the shared API server: authentication, the identity of the
// caller, and the edits whose hooks write to the cluster.
for _, p := range []string{
base + "/auth/",
"GET " + base + "/whoami",
"GET " + base + "/links",
"GET " + base + "/healthz",
"PUT " + base + "/clients",
"POST " + base + "/zones/records",
"PUT " + base + "/zones/records",
"DELETE " + base + "/zones/records",
"POST " + base + "/zones/delegations",
"DELETE " + base + "/zones/delegations",
"DELETE " + base + "/zones",
"POST " + base + "/blocklists/toggle",
} {
mux.Handle(p, own)
}
// Reads that combine the cluster's objects with the resolver's view.
mux.Handle("GET "+base+"/zones", c.api.Protect(http.HandlerFunc(c.handleZones), false))
mux.Handle("GET "+base+"/clients", c.api.Protect(http.HandlerFunc(c.handleClients), false))
mux.Handle("GET "+base+"/blocklist-sources", c.api.Protect(http.HandlerFunc(c.handleListSources), false))
mux.Handle("PUT "+base+"/blocklist-sources/{name}", c.api.Protect(http.HandlerFunc(c.handleListSourcePut), true))
mux.Handle("DELETE "+base+"/blocklist-sources/{name}", c.api.Protect(http.HandlerFunc(c.handleListSourceDelete), true))
// Everything else under the API belongs to the resolver.
proxy := c.opt.Runtime.Proxy()
read, write := c.api.Protect(proxy, false), c.api.Protect(proxy, true)
mux.Handle(base+"/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet || r.Method == http.MethodHead {
read.ServeHTTP(w, r)
return
}
write.ServeHTTP(w, r)
}))
if cfg.API.UI {
ui, err := web.Handler("")
if err != nil {
return nil, fmt.Errorf("web console: %w", err)
}
mux.Handle("/", ui)
}
return mux, nil
}