-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmain.go
41 lines (31 loc) · 851 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"time"
)
const httpAddr = ":8080"
func main() {
fmt.Println("Server running on", httpAddr)
mux := http.NewServeMux()
healthHandler := http.HandlerFunc(healthHandler)
mux.Handle("/health", recoveryMiddleware(healthHandler))
log.Fatal(http.ListenAndServe(httpAddr, mux))
}
func healthHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("everything is ok!"))
}
func recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("[Middleware] %s panic recovered:\n%s\n",
time.Now().Format("2006/01/02 - 15:04:05"), err)
w.WriteHeader(http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}