forked from improbable-eng/go-httpwares
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (60 loc) · 1.99 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/Solution/go-httpwares"
"github.com/Solution/go-httpwares/logging/logrus"
"github.com/Solution/go-httpwares/tags"
"github.com/Solution/go-httpwares/tracing/debug"
"github.com/Solution/go-httpwares/tracing/opentracing"
"github.com/mwitkow/go-conntrack"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context/ctxhttp"
_ "golang.org/x/net/trace" // import the debug pages
)
var (
port = flag.Int("port", 9090, "whether to use tls or not")
)
func main() {
flag.Parse()
log.SetOutput(os.Stdout)
logInstance := log.NewEntry(log.StandardLogger())
opentracing.SetGlobalTracer(mocktracer.New())
googleClient := httpwares.WrapClient(
http.DefaultClient,
http_ctxtags.Tripperware(),
http_opentracing.Tripperware(),
http_debug.Tripperware(),
)
handlerFunc := func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusOK)
resp.Header().Add("Content-Type", "application/json")
resp.Write([]byte(`{"queried_google": true}`))
callCtx := conntrack.DialNameToContext(req.Context(), "google")
_, err := ctxhttp.Get(callCtx, googleClient, "https://www.google.com")
logInstance.Printf("Google reached with err: %v", err)
}
chainedHandler := chi.Chain(
http_ctxtags.Middleware("google_ping_service"),
http_opentracing.Middleware(),
http_debug.Middleware(),
http_logrus.Middleware(logInstance),
).HandlerFunc(handlerFunc)
http.DefaultServeMux.Handle("/", chainedHandler)
http.DefaultServeMux.Handle("/metrics", prometheus.Handler())
httpServer := http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: http.DefaultServeMux,
ErrorLog: http_logrus.AsHttpLogger(logInstance),
}
log.Printf("Listening on http://localhost:%d/", *port)
if err := httpServer.ListenAndServe(); err != nil {
log.Fatalf("Failed listning: %v", err)
}
}