Spaces:
Runtime error
Runtime error
| // Package api provides the HTTP server for the GDELT Engine. | |
| package api | |
| import ( | |
| "context" | |
| "net/http" | |
| "time" | |
| ) | |
| // Server wraps the HTTP server. | |
| type Server struct { | |
| server *http.Server | |
| handlers *Handlers | |
| } | |
| // NewServer creates a new API server. | |
| func NewServer(port string, handlers *Handlers) *Server { | |
| mux := http.NewServeMux() | |
| // Register routes | |
| mux.HandleFunc("/health", handlers.HandleHealth) | |
| mux.HandleFunc("/stats", handlers.HandleStats) | |
| mux.HandleFunc("/process", handlers.HandleProcess) | |
| mux.HandleFunc("/timestamps", handlers.HandleListTimestamps) | |
| mux.HandleFunc("/status/", handlers.HandleStatus) | |
| // Root endpoint - API info | |
| mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
| if r.URL.Path != "/" { | |
| http.NotFound(w, r) | |
| return | |
| } | |
| handlers.respondJSON(w, http.StatusOK, map[string]interface{}{ | |
| "name": "GDELT Engine", | |
| "version": "3.0.0", | |
| "endpoints": map[string]string{ | |
| "POST /process": "Submit timestamps for processing", | |
| "GET /status/{ts}": "Check timestamp status", | |
| "GET /timestamps": "List all timestamps", | |
| "GET /stats": "Database statistics", | |
| "GET /health": "Health check", | |
| }, | |
| }) | |
| }) | |
| return &Server{ | |
| server: &http.Server{ | |
| Addr: ":" + port, | |
| Handler: mux, | |
| ReadTimeout: 30 * time.Second, | |
| WriteTimeout: 30 * time.Second, | |
| IdleTimeout: 60 * time.Second, | |
| }, | |
| handlers: handlers, | |
| } | |
| } | |
| // Start starts the HTTP server. | |
| func (s *Server) Start() error { | |
| return s.server.ListenAndServe() | |
| } | |
| // Shutdown gracefully shuts down the server. | |
| func (s *Server) Shutdown(ctx context.Context) error { | |
| return s.server.Shutdown(ctx) | |
| } | |