-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
156 lines (134 loc) · 3.97 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/go-redis/redis"
)
type key int
const (
requestIDKey key = 0
)
var (
listenAddr string
redisAddr string
healthy int32
)
// this pushes new items onto a stack on a random cycle
func main() {
flag.StringVar(&listenAddr, "binding", "0.0.0.0:5000", "Server listen address")
flag.StringVar(&redisAddr, "redis", "redis:6379", "Redis address (not required)")
flag.Parse()
cancel := make(chan os.Signal)
signal.Notify(cancel, os.Interrupt, syscall.SIGTERM)
logger := log.New(os.Stdout, "http: ", log.LstdFlags)
logger.Printf("Server is starting on %s...\n", listenAddr)
logger.Printf("Checking Redis on %s...\n", redisAddr)
router := http.NewServeMux()
router.Handle("/style.css", http.FileServer(http.Dir("./static")))
router.Handle("/background.jpg", http.FileServer(http.Dir("./static")))
router.HandleFunc("/", handler)
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
server := &http.Server{
Addr: listenAddr,
Handler: tracing(nextRequestID)(logging(logger)(router)),
ErrorLog: logger,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
logger.Println("Server is shutting down...")
atomic.StoreInt32(&healthy, 0)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
}()
logger.Println("Server is ready to handle requests at", listenAddr)
atomic.StoreInt32(&healthy, 1)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("Could not listen on %s: %v\n", listenAddr, err)
}
<-done
logger.Println("Server stopped")
}
func handler(w http.ResponseWriter, r *http.Request) {
var contentBytes, _ = ioutil.ReadFile("./static/index.html")
var content = string(contentBytes)
var leadContent string
if testRedisConnection(redisAddr) {
leadContent = "This is a simple service application(connected to Redis). Deployed by Cloud 66 ~"
} else {
leadContent = "This is a simple single service application. Deployed by Cloud 66"
}
content = strings.Replace(content, "{{LEAD}}", leadContent, -1)
w.Write([]byte(content))
}
func healthz() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 1 {
w.WriteHeader(http.StatusNoContent)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
})
}
func testRedisConnection(redisAddress string) bool {
client := redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: "", // no password set
DB: 0, // use default DB
})
pong, _ := client.Ping().Result()
if pong == "PONG" {
return true
}
return false
// Output: PONG <nil>
}
func logging(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Println(requestID, r.Method, r.URL.Path, r.RemoteAddr, r.UserAgent())
}()
next.ServeHTTP(w, r)
})
}
}
func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}