This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (89 loc) · 3.11 KB
/
main.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"fmt"
"github.com/gorilla/mux"
"github.com/madjlzz/madprobe/controller"
"github.com/madjlzz/madprobe/internal/alerter"
"github.com/madjlzz/madprobe/internal/persistence"
"github.com/madjlzz/madprobe/internal/prober"
"github.com/madjlzz/madprobe/util"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
configuration := util.NewServerConfiguration()
client := http.DefaultClient
if len(configuration.CaCertificate) > 0 {
client = util.HttpsClient(configuration.CaCertificate)
}
// Event Bus channel to let services communicate.
alertBus := make(chan prober.Probe)
// TODO: should be passed as a property...
persistenceClient, err := persistence.NewBoltDBClient("madprobe.db")
if err != nil {
log.Fatalf("[ERROR] persistence module wasn't able to initialize. got: %v\n", err)
}
probeRunner := prober.NewProbeRunner(client, alertBus)
probeService := prober.NewProbeService(probeRunner, persistenceClient)
probeController := controller.NewProbeController(probeService)
r := mux.NewRouter()
r.HandleFunc("/api/v1/probe/create", probeController.Create).
Methods(http.MethodPost)
r.HandleFunc("/api/v1/probe/{name}", probeController.Read).
Methods(http.MethodGet)
r.HandleFunc("/api/v1/probe", probeController.ReadAll).
Methods(http.MethodGet)
r.HandleFunc("/api/v1/probe/{name}", probeController.Delete).
Methods(http.MethodDelete)
srv := &http.Server{
Addr: "0.0.0.0:" + configuration.Port,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r, // Pass our instance of gorilla/mux in.
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if len(configuration.ServerCertificate) == 0 || len(configuration.ServerKey) == 0 {
log.Printf("Starting HTTP server on port %s...\n", configuration.Port)
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
} else {
log.Printf("Starting HTTPs server on port %s...\n", configuration.Port)
if err := srv.ListenAndServeTLS(configuration.ServerCertificate, configuration.ServerKey); err != nil {
log.Println(err)
}
}
}()
// Alerter start at boot time.
al, err := alerter.NewService(alertBus)
if err != nil {
fmt.Printf("[WARNING] alerter module wasn't able to start. got: %v\n", err)
}
al.Run()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Insert a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), configuration.Wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
_ = srv.Shutdown(ctx)
_ = al.Close()
_ = persistenceClient.Close()
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("shutting down")
os.Exit(0)
}