86 lines
3.1 KiB
Go
86 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"nukumizu-backend/handler"
|
|
"nukumizu-backend/postLog"
|
|
"nukumizu-backend/utils"
|
|
"nukumizu-backend/web"
|
|
)
|
|
|
|
// SetupRouter registers all HTTP routes and returns a configured ServeMux.
|
|
func SetupRouter() *http.ServeMux {
|
|
postLog.Info("Setting up routers...")
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// User endpoints.
|
|
mux.HandleFunc("/api/user/login", handler.UserLoginHandler)
|
|
mux.HandleFunc("/api/user/register", handler.UserRegisterHandler)
|
|
|
|
// Server endpoints (authenticated).
|
|
mux.HandleFunc("/api/server/list", handler.ServerListHandler)
|
|
mux.HandleFunc("/api/server/getInfo", handler.ServerGetInfoHandler)
|
|
mux.HandleFunc("/api/server/getStatus", handler.ServerGetStatusHandler)
|
|
mux.HandleFunc("/api/server/exec", handler.ServerExecHandler)
|
|
|
|
// Settings endpoints (admin only).
|
|
mux.HandleFunc("/api/settings/get", handler.SettingsGetHandler)
|
|
mux.HandleFunc("/api/settings/set", handler.SettingsSetHandler)
|
|
|
|
// Incoming webhook endpoint management (admin only). These configure the
|
|
// endpoints served by SetupWebhookRouter, which runs on its own listener.
|
|
mux.HandleFunc("/api/webhook/add", handler.WebhookAddHandler)
|
|
mux.HandleFunc("/api/webhook/modify", handler.WebhookModifyHandler)
|
|
mux.HandleFunc("/api/webhook/delete", handler.WebhookDeleteHandler)
|
|
mux.HandleFunc("/api/webhook/list", handler.WebhookListHandler)
|
|
|
|
// Health check endpoint.
|
|
mux.HandleFunc("/health", handler.HealthHandler)
|
|
|
|
// WebSocket log streaming endpoint (admin only). The middleware authenticates
|
|
// the upgrade request, so an anonymous or non-admin client is rejected before
|
|
// any log entry leaves the server.
|
|
logBroadcaster := postLog.GetLogBroadcaster()
|
|
if logBroadcaster != nil {
|
|
logSocketHandler := postLog.NewLogSocketHandler(logBroadcaster)
|
|
adminOnly := utils.AuthWS("admin")
|
|
mux.Handle("/api/system/getLogs", adminOnly(http.HandlerFunc(logSocketHandler.Handle)))
|
|
}
|
|
|
|
// Static file serving for the web frontend.
|
|
mux.HandleFunc("/", web.ServeStatic)
|
|
|
|
postLog.Info("Router setup completed")
|
|
return mux
|
|
}
|
|
|
|
// SetupWebhookRouter registers the routes of the incoming webhook API. Unlike
|
|
// SetupRouter it is served on its own listener (webhook.listenAddr/listenPort),
|
|
// so external applications can be given access to the webhook port without
|
|
// reaching the admin API. Every endpoint configured under webhook.endpoints is
|
|
// reachable as /api/webhook/<name>.
|
|
func SetupWebhookRouter() *http.ServeMux {
|
|
postLog.Info("Setting up webhook routers...")
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// The wildcard segment selects the endpoint; requests for a name that is not
|
|
// configured fall through to the handler, which answers with a JSON 404.
|
|
mux.HandleFunc("/api/webhook/post/{name}", handler.WebhookHandler)
|
|
mux.HandleFunc("/", NotFoundHandler)
|
|
|
|
postLog.Info("Webhook router setup completed")
|
|
return mux
|
|
}
|
|
|
|
// NotFoundHandler returns a 404 JSON response for unknown routes.
|
|
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|
postLog.Debug(fmt.Sprintf("Unknown request: %s %s", r.Method, r.URL.Path))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusNotFound)
|
|
fmt.Fprint(w, `{"success":false,"message":"route not found"}`)
|
|
}
|