feat(seeder): add standardized root response with IP, docs, health
This commit is contained in:
parent
538c233354
commit
ea79835415
1 changed files with 40 additions and 0 deletions
|
|
@ -40,6 +40,14 @@ type seedResponse struct {
|
||||||
Type string `json:"type,omitempty"`
|
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() {
|
func main() {
|
||||||
_ = godotenv.Load()
|
_ = godotenv.Load()
|
||||||
|
|
||||||
|
|
@ -59,6 +67,7 @@ func main() {
|
||||||
srv := &server{pool: pool}
|
srv := &server{pool: pool}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", srv.rootHandler)
|
||||||
mux.HandleFunc("/health", srv.healthHandler)
|
mux.HandleFunc("/health", srv.healthHandler)
|
||||||
mux.HandleFunc("/seed", srv.seedHandler)
|
mux.HandleFunc("/seed", srv.seedHandler)
|
||||||
mux.HandleFunc("/reset", srv.resetHandler)
|
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) {
|
func (s *server) healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
writeJSON(w, http.StatusMethodNotAllowed, healthResponse{Status: "error", Database: "disconnected", Error: "Method not allowed"})
|
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
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue