1
1
package main
2
2
3
3
import (
4
- "context"
5
4
"flag"
6
5
"fmt"
7
6
"log"
8
- "net"
9
- "net/http"
10
7
"os"
11
- "os/signal"
12
- "syscall"
13
8
"time"
14
9
15
10
"github.com/bluewave-labs/capture/internal/config"
16
- "github.com/bluewave-labs/capture/internal/handler"
17
- "github.com/bluewave-labs/capture/internal/middleware"
18
- "github.com/gin-gonic/gin"
11
+ "github.com/bluewave-labs/capture/internal/server"
19
12
)
20
13
21
14
var appConfig * config.Config
22
15
23
16
var Version = "develop" // This will be set during compile time using go build ldflags
24
17
25
- // getLocalIP retrieves the local IP address of the machine.
26
- // It returns the first non-loopback IPv4 address found.
27
- // If no valid address is found, it returns "<ip-address>" as a placeholder.
28
- // This function is used to display the local IP address in the log message.
29
- func getLocalIP () string {
30
- addrs , err := net .InterfaceAddrs ()
31
- if err != nil {
32
- return "<ip-address>"
33
- }
34
- for _ , addr := range addrs {
35
- if ipnet , ok := addr .(* net.IPNet ); ok && ! ipnet .IP .IsLoopback () {
36
- if ipnet .IP .To4 () != nil {
37
- return ipnet .IP .String ()
38
- }
39
- }
40
- }
41
- return "<ip-address>"
42
- }
43
-
44
18
func main () {
45
19
showVersion := flag .Bool ("version" , false , "Display the version of the capture" )
46
20
flag .Parse ()
@@ -56,53 +30,10 @@ func main() {
56
30
os .Getenv ("API_SECRET" ),
57
31
)
58
32
59
- // Initialize the Gin with default middlewares
60
- r := gin .Default ()
61
- apiV1 := r .Group ("/api/v1" )
62
- apiV1 .Use (middleware .AuthRequired (appConfig .APISecret ))
63
-
64
- // Health Check
65
- apiV1 .GET ("/health" , handler .Health )
66
-
67
- // Metrics
68
- apiV1 .GET ("/metrics" , handler .Metrics )
69
- apiV1 .GET ("/metrics/cpu" , handler .MetricsCPU )
70
- apiV1 .GET ("/metrics/memory" , handler .MetricsMemory )
71
- apiV1 .GET ("/metrics/disk" , handler .MetricsDisk )
72
- apiV1 .GET ("/metrics/host" , handler .MetricsHost )
73
- apiV1 .GET ("/metrics/smart" , handler .SmartMetrics )
74
-
75
- log .Println ("WARNING: Remember to add http://" + getLocalIP () + ":" + appConfig .Port + "/api/v1/metrics to your Checkmate Infrastructure Dashboard. Without this endpoint, system metrics will not be displayed." )
76
-
77
- server := & http.Server {
78
- Addr : ":" + appConfig .Port ,
79
- Handler : r .Handler (),
80
- ReadHeaderTimeout : 5 * time .Second ,
81
- }
82
-
83
- go serve (server )
84
-
85
- if err := gracefulShutdown (server , 5 * time .Second ); err != nil {
86
- log .Fatalln ("graceful shutdown error" , err )
87
- }
88
- }
89
-
90
- func serve (srv * http.Server ) {
91
- srvErr := srv .ListenAndServe ()
92
- if srvErr != nil && srvErr != http .ErrServerClosed {
93
- log .Fatalf ("listen error: %s\n " , srvErr )
94
- }
95
- }
96
-
97
- func gracefulShutdown (srv * http.Server , timeout time.Duration ) error {
98
- quit := make (chan os.Signal , 1 )
99
- signal .Notify (quit , syscall .SIGINT , syscall .SIGTERM )
100
-
101
- sig := <- quit
102
- log .Printf ("signal received: %v" , sig )
33
+ srv := server .NewServer (appConfig , nil )
34
+ log .Println ("WARNING: Remember to add http://" + server .GetLocalIP () + ":" + appConfig .Port + "/api/v1/metrics to your Checkmate Infrastructure Dashboard. Without this endpoint, system metrics will not be displayed." )
103
35
104
- ctx , cancel := context .WithTimeout (context .Background (), timeout )
105
- defer cancel ()
36
+ srv .Serve ()
106
37
107
- return srv .Shutdown ( ctx )
38
+ srv .GracefulShutdown ( 5 * time . Second )
108
39
}
0 commit comments