feat: read codex agent definitions

This commit is contained in:
Yoilun
2026-05-25 16:39:35 +08:00
parent c67d5a33ad
commit fee920a895
9 changed files with 383 additions and 9 deletions

37
internal/server/server.go Normal file
View File

@@ -0,0 +1,37 @@
package server
import (
"encoding/json"
"net/http"
"codex-agent-manager/internal/agents"
"codex-agent-manager/internal/app"
)
func New(cfg app.Config) http.Handler {
mux := http.NewServeMux()
agentStore := agents.Store{CodexHome: cfg.CodexHome}
mux.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
})
mux.HandleFunc("/api/agents", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
return
}
items, err := agentStore.List()
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, map[string]any{"items": items})
})
return mux
}
func writeJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}