package handlers import ( "net/http" "strings" ) // APIRouter routes /map/api/* requests to the appropriate handler. func (h *Handlers) APIRouter(rw http.ResponseWriter, req *http.Request) { path := strings.TrimPrefix(req.URL.Path, "/map/api") path = strings.TrimPrefix(path, "/") switch path { case "config": h.APIConfig(rw, req) return case "v1/characters": h.APIGetChars(rw, req) return case "v1/markers": h.APIGetMarkers(rw, req) return case "maps": h.APIGetMaps(rw, req) return case "setup": h.APISetup(rw, req) return case "login": h.APILogin(rw, req) return case "logout": h.APILogout(rw, req) return case "me": switch req.Method { case http.MethodGet: h.APIMe(rw, req) case http.MethodPatch: h.APIMeUpdate(rw, req) default: JSONError(rw, http.StatusMethodNotAllowed, "method not allowed", "METHOD_NOT_ALLOWED") } return case "me/tokens": h.APIMeTokens(rw, req) return case "me/password": h.APIMePassword(rw, req) return } switch { case path == "oauth/providers": h.APIOAuthProviders(rw, req) return case strings.HasPrefix(path, "oauth/"): rest := strings.TrimPrefix(path, "oauth/") parts := strings.SplitN(rest, "/", 2) if len(parts) != 2 { JSONError(rw, http.StatusNotFound, "not found", "NOT_FOUND") return } provider := parts[0] action := parts[1] switch action { case "login": h.APIOAuthLogin(rw, req, provider) case "callback": h.APIOAuthCallback(rw, req, provider) default: JSONError(rw, http.StatusNotFound, "not found", "NOT_FOUND") } return case strings.HasPrefix(path, "admin/"): adminPath := strings.TrimPrefix(path, "admin/") h.APIAdminRoute(rw, req, adminPath) return } JSONError(rw, http.StatusNotFound, "not found", "NOT_FOUND") }