-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (74 loc) · 1.77 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
package main
import (
"fmt"
"os"
"os/signal"
"splunk-cf-logdrain/handlers"
"github.com/spf13/viper"
"github.com/labstack/echo/v4"
"net/http"
_ "net/http/pprof"
)
var commit = "deadbeaf"
var release = "v1.2.2"
var buildVersion = release + "-" + commit
func main() {
e := make(chan *echo.Echo, 1)
os.Exit(realMain(e))
}
func realMain(echoChan chan<- *echo.Echo) int {
viper.SetEnvPrefix("splunk-cf-logdrain")
viper.SetDefault("transport_url", "")
viper.SetDefault("syslog_endpoint", "localhost:5140")
viper.AutomaticEnv()
token := os.Getenv("TOKEN")
// Echo framework
e := echo.New()
// Middleware
healthHandler := handlers.HealthHandler{}
e.GET("/health", healthHandler.Handler())
e.GET("/api/version", handlers.VersionHandler(buildVersion))
syslogEndpoint := viper.GetString("syslog_endpoint")
syslogHandler, err := handlers.NewSyslogHandler(token, syslogEndpoint)
if err != nil {
fmt.Printf("syslogHandler: %v\n", err)
return 8
}
e.POST("/syslog/drain/:token", syslogHandler.Handler())
setupPprof()
setupInterrupts()
echoChan <- e
exitCode := 0
if err := e.Start(listenString()); err != nil {
fmt.Printf("error: %v\n", err)
exitCode = 6
}
return exitCode
}
func setupInterrupts() {
// Setup a channel to receive a signal
done := make(chan os.Signal, 1)
// Notify this channel when a SIGINT is received
signal.Notify(done, os.Interrupt)
// Fire off a goroutine to loop until that channel receives a signal.
// When a signal is received simply exit the program
go func() {
for range done {
os.Exit(0)
}
}()
}
func setupPprof() {
go func() {
err := http.ListenAndServe("localhost:6060", nil)
if err != nil {
}
}()
}
func listenString() string {
port := os.Getenv("LISTEN_PORT")
if port == "" {
port = "8080"
}
return ":" + port
}