From ea79835415569aa6cec59ed56831c47abf3ec8d1 Mon Sep 17 00:00:00 2001 From: Yamamoto Date: Fri, 2 Jan 2026 20:09:51 -0300 Subject: [PATCH] feat(seeder): add standardized root response with IP, docs, health --- seeder-api/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/seeder-api/main.go b/seeder-api/main.go index 8910662..b5e3510 100644 --- a/seeder-api/main.go +++ b/seeder-api/main.go @@ -40,6 +40,14 @@ type seedResponse struct { Type string `json:"type,omitempty"` } +type rootResponse struct { + Docs string `json:"docs"` + Health string `json:"health"` + IP string `json:"ip"` + Message string `json:"message"` + Version string `json:"version"` +} + func main() { _ = godotenv.Load() @@ -59,6 +67,7 @@ func main() { srv := &server{pool: pool} mux := http.NewServeMux() + mux.HandleFunc("/", srv.rootHandler) mux.HandleFunc("/health", srv.healthHandler) mux.HandleFunc("/seed", srv.seedHandler) mux.HandleFunc("/reset", srv.resetHandler) @@ -74,6 +83,22 @@ func main() { } } +func (s *server) rootHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + + ip := getClientIP(r) + writeJSON(w, http.StatusOK, rootResponse{ + Docs: "/docs", + Health: "/health", + IP: ip, + Message: "🌱 GoHorseJobs Seeder API is running!", + Version: "1.0.0", + }) +} + func (s *server) healthHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { writeJSON(w, http.StatusMethodNotAllowed, healthResponse{Status: "error", Database: "disconnected", Error: "Method not allowed"}) @@ -281,3 +306,18 @@ func getenv(key, fallback string) string { } return value } + +func getClientIP(r *http.Request) string { + if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" { + parts := strings.Split(forwarded, ",") + return strings.TrimSpace(parts[0]) + } + if realIP := r.Header.Get("X-Real-IP"); realIP != "" { + return realIP + } + host := r.RemoteAddr + if colonPos := strings.LastIndex(host, ":"); colonPos != -1 { + return host[:colonPos] + } + return host +}