49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
// Package handler implements inbound HTTP contracts.
|
|
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type healthResponse struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// RouterOptions contains optional handlers wired by the application layer.
|
|
type RouterOptions struct {
|
|
Chat http.Handler
|
|
ChatPage http.Handler
|
|
DashScopeChat http.Handler
|
|
MCP http.Handler
|
|
}
|
|
|
|
// NewRouter returns the HTTP handler for the application.
|
|
func NewRouter(options ...RouterOptions) http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /health", health)
|
|
if len(options) > 0 {
|
|
if options[0].Chat != nil {
|
|
mux.Handle("/api/chat", options[0].Chat)
|
|
}
|
|
if options[0].ChatPage != nil {
|
|
mux.Handle("GET /chat", http.RedirectHandler("/chat/", http.StatusPermanentRedirect))
|
|
mux.Handle("GET /chat/", options[0].ChatPage)
|
|
}
|
|
if options[0].DashScopeChat != nil {
|
|
mux.Handle("/api/v1/apps/{app_id}/completion", options[0].DashScopeChat)
|
|
}
|
|
if options[0].MCP != nil {
|
|
mux.Handle("/mcp", options[0].MCP)
|
|
}
|
|
}
|
|
|
|
return mux
|
|
}
|
|
|
|
func health(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(http.StatusOK)
|
|
_ = json.NewEncoder(w).Encode(healthResponse{Status: "ok"})
|
|
}
|