File size: 7,267 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | package api
import (
"context"
"net"
"net/http"
"strconv"
"sync/atomic"
"github.com/Resinat/Resin/internal/config"
"github.com/Resinat/Resin/internal/metrics"
"github.com/Resinat/Resin/internal/requestlog"
"github.com/Resinat/Resin/internal/service"
)
// Server wraps the HTTP server and mux for the Resin API.
type Server struct {
httpServer *http.Server
mux *http.ServeMux
}
// NewServer creates a new API server wired with all routes.
// cp may be nil if the control plane is not yet initialized.
func NewServer(
port int,
adminToken string,
systemInfo service.SystemInfo,
runtimeCfg *atomic.Pointer[config.RuntimeConfig],
envCfg *config.EnvConfig,
cp *service.ControlPlaneService,
apiMaxBodyBytes int64,
requestlogRepo *requestlog.Repo,
metricsManager *metrics.Manager,
) *Server {
return NewServerWithAddress(
"",
port,
adminToken,
systemInfo,
runtimeCfg,
envCfg,
cp,
apiMaxBodyBytes,
requestlogRepo,
metricsManager,
)
}
// NewServerWithAddress creates a new API server with an explicit listen address.
func NewServerWithAddress(
listenAddress string,
port int,
adminToken string,
systemInfo service.SystemInfo,
runtimeCfg *atomic.Pointer[config.RuntimeConfig],
envCfg *config.EnvConfig,
cp *service.ControlPlaneService,
apiMaxBodyBytes int64,
requestlogRepo *requestlog.Repo,
metricsManager *metrics.Manager,
) *Server {
mux := http.NewServeMux()
// Public (no auth)
mux.Handle("GET /healthz", HandleHealthz())
// Authenticated routes
authed := http.NewServeMux()
authed.Handle("GET /api/v1/system/info", HandleSystemInfo(systemInfo))
authed.Handle("GET /api/v1/system/config", HandleSystemConfig(runtimeCfg))
authed.Handle("GET /api/v1/system/config/default", HandleSystemDefaultConfig())
authed.Handle("GET /api/v1/system/config/env", HandleSystemEnvConfig(envCfg))
if cp != nil {
// System config mutations.
authed.Handle("PATCH /api/v1/system/config", HandlePatchSystemConfig(cp))
// Platforms.
authed.Handle("GET /api/v1/platforms", HandleListPlatforms(cp))
authed.Handle("POST /api/v1/platforms", HandleCreatePlatform(cp))
authed.Handle("POST /api/v1/platforms/preview-filter", HandlePreviewFilter(cp))
authed.Handle("GET /api/v1/platforms/{id}", HandleGetPlatform(cp))
authed.Handle("PATCH /api/v1/platforms/{id}", HandleUpdatePlatform(cp))
authed.Handle("DELETE /api/v1/platforms/{id}", HandleDeletePlatform(cp))
authed.Handle("POST /api/v1/platforms/{id}/actions/reset-to-default", HandleResetPlatform(cp))
authed.Handle("POST /api/v1/platforms/{id}/actions/rebuild-routable-view", HandleRebuildPlatform(cp))
// Leases (under platforms).
authed.Handle("GET /api/v1/platforms/{id}/leases", HandleListLeases(cp))
authed.Handle("DELETE /api/v1/platforms/{id}/leases", HandleDeleteAllLeases(cp))
authed.Handle("GET /api/v1/platforms/{id}/leases/{account}", HandleGetLease(cp))
authed.Handle("DELETE /api/v1/platforms/{id}/leases/{account}", HandleDeleteLease(cp))
authed.Handle("GET /api/v1/platforms/{id}/ip-load", HandleIPLoad(cp))
// Subscriptions.
authed.Handle("GET /api/v1/subscriptions", HandleListSubscriptions(cp))
authed.Handle("POST /api/v1/subscriptions", HandleCreateSubscription(cp))
authed.Handle("GET /api/v1/subscriptions/{id}", HandleGetSubscription(cp))
authed.Handle("PATCH /api/v1/subscriptions/{id}", HandleUpdateSubscription(cp))
authed.Handle("DELETE /api/v1/subscriptions/{id}", HandleDeleteSubscription(cp))
authed.Handle("POST /api/v1/subscriptions/{id}/actions/refresh", HandleRefreshSubscription(cp))
authed.Handle("POST /api/v1/subscriptions/{id}/actions/cleanup-circuit-open-nodes", HandleCleanupSubscriptionCircuitOpenNodes(cp))
// Account header rules.
authed.Handle("GET /api/v1/account-header-rules", HandleListRules(cp))
// Canonical route (DESIGN.md): url_prefix comes from path parameter only.
authed.Handle("PUT /api/v1/account-header-rules/{prefix...}", HandleUpsertRule(cp))
authed.Handle("POST /api/v1/account-header-rules:resolve", HandleResolveRule(cp))
authed.Handle("DELETE /api/v1/account-header-rules/{prefix...}", HandleDeleteRule(cp))
// Nodes.
authed.Handle("GET /api/v1/nodes", HandleListNodes(cp))
authed.Handle("GET /api/v1/nodes/{hash}", HandleGetNode(cp))
authed.Handle("POST /api/v1/nodes/{hash}/actions/probe-egress", HandleProbeEgress(cp))
authed.Handle("POST /api/v1/nodes/{hash}/actions/probe-latency", HandleProbeLatency(cp))
// GeoIP.
authed.Handle("GET /api/v1/geoip/status", HandleGeoIPStatus(cp))
authed.Handle("GET /api/v1/geoip/lookup", HandleGeoIPLookup(cp))
authed.Handle("POST /api/v1/geoip/lookup", HandleGeoIPLookupPost(cp))
authed.Handle("POST /api/v1/geoip/actions/update-now", HandleGeoIPUpdate(cp))
}
// Request log endpoints (always registered if repo is available).
if requestlogRepo != nil {
authed.Handle("GET /api/v1/request-logs", HandleListRequestLogs(requestlogRepo))
authed.Handle("GET /api/v1/request-logs/{log_id}", HandleGetRequestLog(requestlogRepo))
authed.Handle("GET /api/v1/request-logs/{log_id}/payloads", HandleGetRequestLogPayloads(requestlogRepo))
}
// Metrics endpoints.
if metricsManager != nil {
// Realtime (ring buffer).
authed.Handle("GET /api/v1/metrics/realtime/throughput", HandleRealtimeThroughput(metricsManager))
authed.Handle("GET /api/v1/metrics/realtime/connections", HandleRealtimeConnections(metricsManager))
authed.Handle("GET /api/v1/metrics/realtime/leases", HandleRealtimeLeases(metricsManager))
// History (metrics.db bucket).
authed.Handle("GET /api/v1/metrics/history/traffic", HandleHistoryTraffic(metricsManager))
authed.Handle("GET /api/v1/metrics/history/requests", HandleHistoryRequests(metricsManager))
authed.Handle("GET /api/v1/metrics/history/access-latency", HandleHistoryAccessLatency(metricsManager))
authed.Handle("GET /api/v1/metrics/history/probes", HandleHistoryProbes(metricsManager))
authed.Handle("GET /api/v1/metrics/history/node-pool", HandleHistoryNodePool(metricsManager))
authed.Handle("GET /api/v1/metrics/history/lease-lifetime", HandleHistoryLeaseLifetime(metricsManager))
// Snapshots (realtime computed).
authed.Handle("GET /api/v1/metrics/snapshots/node-pool", HandleSnapshotNodePool(metricsManager))
authed.Handle("GET /api/v1/metrics/snapshots/platform-node-pool", HandleSnapshotPlatformNodePool(metricsManager))
authed.Handle("GET /api/v1/metrics/snapshots/node-latency-distribution", HandleSnapshotNodeLatencyDistribution(metricsManager))
}
limitedAuthed := RequestBodyLimitMiddleware(apiMaxBodyBytes, authed)
mux.Handle("/api/", AuthMiddleware(adminToken, limitedAuthed))
registerEmbeddedWebUI(mux)
srv := &http.Server{
Addr: net.JoinHostPort(listenAddress, strconv.Itoa(port)),
Handler: mux,
}
return &Server{
httpServer: srv,
mux: mux,
}
}
// ListenAndServe starts the HTTP server. It blocks until the server stops.
func (s *Server) ListenAndServe() error {
return s.httpServer.ListenAndServe()
}
// Shutdown gracefully shuts down the server.
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}
// Handler returns the underlying http.Handler for testing.
func (s *Server) Handler() http.Handler {
return s.mux
}
|