Files

119 lines
3.1 KiB
Go

package api
import (
"linkport/internal/buildinfo"
"net/http"
"strings"
)
// dispatch keeps route precedence independent from authentication and domain logic.
func (s *Server) dispatch(w http.ResponseWriter, r *http.Request, path string, identity requestIdentity) {
if path == "session" && r.Method == "GET" {
respond(w, 200, map[string]any{"username": identity.session.User, "csrfToken": identity.session.CSRF, "authEnabled": s.authEnabled})
return
}
if strings.HasPrefix(path, "database/") {
s.databaseSnapshot(w, r, path)
return
}
if path == "listen-preflight" {
s.listenPreflight(w, r)
return
}
if path == "tools/navicat/decrypt" {
s.decryptNavicat(w, r)
return
}
if path == "tools/decrypt" {
s.decryptCustom(w, r)
return
}
if path == "version" {
if r.Method != "GET" {
w.Header().Set("Allow", "GET")
fail(w, 405, "method not allowed")
return
}
respond(w, 200, buildinfo.Current())
return
}
if path == "metrics" && r.Method == "GET" {
s.prometheus(w, r)
return
}
if path == "logging/settings" {
s.loggingSettings(w, r)
return
}
if path == "templates" || strings.HasPrefix(path, "templates/") {
s.templates(w, r, path)
return
}
if path == "logout" && r.Method == "POST" {
s.logout(w, identity.sessionID)
return
}
if path == "events" && r.Method == "GET" {
s.events(w, r)
return
}
if path == "commands" && r.Method == "GET" {
s.commands(w, r)
return
}
if path == "connections" || strings.HasPrefix(path, "connections/") {
s.connectionRoutes(w, r, path)
return
}
if path == "alerts" || strings.HasPrefix(path, "alerts/") {
s.alertRoutes(w, r, path)
return
}
if path == "monitoring" || strings.HasPrefix(path, "monitoring/") {
s.monitoring(w, r, path)
return
}
if path == "proxies" && r.Method == "GET" {
s.listProxies(w, r)
return
}
if strings.HasPrefix(path, "configuration/webdav/") {
s.webdavSync(w, r, path)
return
}
s.dispatchMutation(w, r, path, identity)
}
// The operations lock remains at the domain mutation boundary, including reads
// that must be consistent with edits (such as asset impact).
func (s *Server) dispatchMutation(w http.ResponseWriter, r *http.Request, path string, identity requestIdentity) {
s.ops.Lock()
defer s.ops.Unlock()
impactParts := strings.Split(path, "/")
if len(impactParts) == 3 && (impactParts[0] == "assets" || impactParts[0] == "outbounds") && impactParts[2] == "impact" {
s.assetImpact(w, r, impactParts[0], impactParts[1])
return
}
if path == "tokens" || strings.HasPrefix(path, "tokens/") {
s.tokens(w, r, path, identity.session)
return
}
if strings.HasPrefix(path, "configuration/") {
s.configuration(w, r, path)
return
}
if path == "outbounds" || strings.HasPrefix(path, "outbounds/") {
s.outbounds(w, r, path, identity.session, identity.sessionID)
return
}
if path == "assets" || strings.HasPrefix(path, "assets/") {
s.assets(w, r, path)
return
}
if path == "targets" || strings.HasPrefix(path, "targets/") {
s.targets(w, r, path, identity.session, identity.sessionID)
return
}
s.proxyRoutes(w, r, path, identity.session, identity.sessionID)
}