-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
54 lines (43 loc) · 1.34 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
package main
import (
"errors"
"flag"
"net/http"
"github.com/ethereum/go-ethereum/log"
"github.com/flashbots/go-utils/envflag"
"github.com/flashbots/go-utils/httplogger"
"github.com/flashbots/go-utils/logutils"
"go.uber.org/zap"
)
var listenAddr = "localhost:8124"
func HelloHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
w.WriteHeader(http.StatusOK)
}
func ErrorHandler(w http.ResponseWriter, r *http.Request) {
l := logutils.ZapFromRequest(r)
l.Error("this is an error", zap.Error(errors.New("testError")))
http.Error(w, "this is an error", http.StatusInternalServerError)
}
func PanicHandler(w http.ResponseWriter, r *http.Request) {
panic("foo!")
}
func main() {
logLevel := envflag.String("log-level", "info", "Log level")
logDev := envflag.MustBool("log-dev", false, "Log in development mode")
flag.Parse()
l := logutils.MustGetZapLogger(
logutils.LogDevMode(*logDev),
logutils.LogLevel(*logLevel),
)
defer logutils.FlushZap(l)
l.Info("Webserver running at " + listenAddr)
mux := http.NewServeMux()
mux.HandleFunc("/hello", HelloHandler)
mux.HandleFunc("/error", ErrorHandler)
mux.HandleFunc("/panic", PanicHandler)
loggedRouter := httplogger.LoggingMiddlewareZap(l, mux)
if err := http.ListenAndServe(listenAddr, loggedRouter); err != nil {
log.Crit("webserver failed", "err", err)
}
}