forked from k8s-community/step-by-step
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (38 loc) · 1.16 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
package main
import (
"net/http"
"os"
"github.com/Sirupsen/logrus"
common_handlers "github.com/k8s-community/handlers"
"github.com/k8s-community/step-by-step/version"
"github.com/k8s-community/utils/shutdown"
"github.com/takama/router"
)
var log = logrus.New()
// Run server: go build; env SERVICE_PORT=8000 step-by-step
// Try requests: curl http://127.0.0.1:8000/test
func main() {
port := os.Getenv("SERVICE_PORT")
if len(port) == 0 {
log.Fatal("Required parameter service port is not set")
}
r := router.New()
r.Logger = logger
r.GET("/", home)
// Readiness and liveness probes for Kubernetes
r.GET("/info", func(c *router.Control) {
common_handlers.Info(c, version.RELEASE, version.REPO, version.COMMIT)
})
r.GET("/healthz", func(c *router.Control) {
c.Code(http.StatusOK).Body(http.StatusText(http.StatusOK))
})
go r.Listen("0.0.0.0:" + port)
logger := log.WithField("event", "shutdown")
sdHandler := shutdown.NewHandler(logger)
sdHandler.RegisterShutdown(sd)
}
// sd does graceful dhutdown of the service
func sd() (string, error) {
// if service has to finish some tasks before shutting down, these tasks must be finished her
return "Ok", nil
}