feat(seeder): add standardized root response with IP, docs, health

This commit is contained in:
Yamamoto 2026-01-02 20:09:51 -03:00
parent 538c233354
commit ea79835415

View file

@ -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
}