File size: 1,666 Bytes
0b5960f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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)
}