-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.go
81 lines (68 loc) · 1.65 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
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/moov-io/base/log"
"github.com/moov-io/watchman"
"github.com/gorilla/mux"
)
var (
httpAddr = flag.String("http.addr", ":10101", "HTTP listen address")
)
func main() {
flag.Parse()
logger := log.NewDefaultLogger()
logger.Logf("Starting moov/watchman-webhook-example:%s", watchman.Version)
// Listen for application termination.
errs := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
// Setup HTTP handler
handler := mux.NewRouter()
addPingRoute(handler)
addWebhookRoute(logger, handler)
// Create main HTTP server
serve := &http.Server{
Addr: *httpAddr,
Handler: handler,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
shutdownServer := func() {
if err := serve.Shutdown(context.TODO()); err != nil {
logger.LogError(err)
}
}
defer shutdownServer()
// Start main HTTP server
go func() {
logger.Logf("listening on %s", *httpAddr)
if err := serve.ListenAndServe(); err != nil {
logger.LogError(err)
}
}()
// Wait for app shutdown
if err := <-errs; err != nil {
logger.LogError(err)
}
os.Exit(0)
}
func addPingRoute(r *mux.Router) {
r.Methods("GET").Path("/ping").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("PONG"))
})
}