summaryrefslogtreecommitdiff
path: root/internal/server/admin.go
diff options
context:
space:
mode:
authorradhitya <alif@radhitya.org>2026-06-21 09:48:42 +0700
committerradhitya <alif@radhitya.org>2026-06-21 09:48:42 +0700
commitb7359e1d45f505171356bcae3c7d5e2341ecc859 (patch)
treef91d4a4b08ce279d488a76e9b7141e69fc844ea9 /internal/server/admin.go
parent2b1f613c42de3861141eb6f93c1740b6937ee183 (diff)
forward mode, cache opt, ACL, rate limit, admin/health, systemd, fix UDP reply
Diffstat (limited to 'internal/server/admin.go')
-rw-r--r--internal/server/admin.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/server/admin.go b/internal/server/admin.go
new file mode 100644
index 0000000..46242d4
--- /dev/null
+++ b/internal/server/admin.go
@@ -0,0 +1,62 @@
+package server
+
+import (
+ "fmt"
+ "log/slog"
+ "net/http"
+)
+
+type Admin struct {
+ server *http.Server
+ s *Server
+}
+
+func NewAdmin(listen string, s *Server) *Admin {
+ if listen == "" {
+ return nil
+ }
+
+ mux := http.NewServeMux()
+ a := &Admin{
+ server: &http.Server{
+ Addr: listen,
+ Handler: mux,
+ },
+ s: s,
+ }
+ mux.HandleFunc("/health", a.healthHandler)
+ return a
+}
+
+func (a *Admin) Start() {
+ if a == nil {
+ return
+ }
+ go func() {
+ slog.Info("admin server listening", "addr", a.server.Addr)
+ if err := a.server.ListenAndServe(); err != nil &&
+ err != http.ErrServerClosed {
+ slog.Error("admin server failed", "error", err)
+ }
+ }()
+}
+
+func (a *Admin) Close() error {
+ if a == nil {
+ return nil
+ }
+ return a.server.Close()
+}
+
+func (a *Admin) healthHandler(w http.ResponseWriter, r *http.Request) {
+ if a.s == nil {
+ http.Error(w, "not ready", http.StatusServiceUnavailable)
+ return
+ }
+ if a.s.Ready() {
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprint(w, "ok\n")
+ return
+ }
+ http.Error(w, "not ready", http.StatusServiceUnavailable)
+}