forked from mtusi2026/mtusi2026lab4
48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type requestHandler func(http.ResponseWriter, *http.Request) *apiError
|
|
|
|
func (s *Server) route(w http.ResponseWriter, r *http.Request) *apiError {
|
|
var handler requestHandler
|
|
switch r.URL.Path {
|
|
case "/health/live":
|
|
handler = s.live
|
|
case "/health/ready":
|
|
handler = s.readiness
|
|
case "/metrics":
|
|
s.metrics.handler.ServeHTTP(w, r)
|
|
return nil
|
|
case "/api/v1/users":
|
|
handler = s.users
|
|
case "/api/v1/users/me":
|
|
handler = s.me
|
|
case "/api/v1/auth/sessions":
|
|
handler = s.createSession
|
|
case "/api/v1/auth/sessions/current":
|
|
handler = s.revokeSession
|
|
case "/api/v1/chats":
|
|
handler = s.chats
|
|
default:
|
|
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" {
|
|
handler = s.chat
|
|
} else if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" {
|
|
handler = s.messages
|
|
} else if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "attachments" {
|
|
handler = s.uploadAttachment
|
|
} else if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "attachments" {
|
|
handler = s.attachmentMetadata
|
|
} else if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "attachments" && parts[4] == "content" {
|
|
handler = s.attachmentContent
|
|
} else {
|
|
return notFound()
|
|
}
|
|
}
|
|
return handler(w, r)
|
|
}
|