forked from openzipkin-contrib/zipkin-go-opentracing
/
httpserver.go
84 lines (69 loc) · 2.11 KB
/
httpserver.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
// +build go1.7
package svc1
import (
"fmt"
"net/http"
"strconv"
opentracing "github.com/opentracing/opentracing-go"
"github.com/openzipkin/zipkin-go-opentracing/examples/middleware"
)
type httpService struct {
service Service
}
// concatHandler is our HTTP HandlerFunc for a Concat request.
func (s *httpService) concatHandler(w http.ResponseWriter, req *http.Request) {
// parse query parameters
v := req.URL.Query()
result, err := s.service.Concat(req.Context(), v.Get("a"), v.Get("b"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// return the result
w.Write([]byte(result))
}
// sumHandler is our HTTP Handlerfunc for a Sum request.
func (s *httpService) sumHandler(w http.ResponseWriter, req *http.Request) {
// parse query parameters
v := req.URL.Query()
a, err := strconv.ParseInt(v.Get("a"), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
b, err := strconv.ParseInt(v.Get("b"), 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// call our Sum binding
result, err := s.service.Sum(req.Context(), a, b)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// return the result
w.Write([]byte(fmt.Sprintf("%d", result)))
}
// NewHTTPHandler returns a new HTTP handler our svc1.
func NewHTTPHandler(tracer opentracing.Tracer, service Service) http.Handler {
// Create our HTTP Service.
svc := &httpService{service: service}
// Create the mux.
mux := http.NewServeMux()
// Create the Concat handler.
var concatHandler http.Handler
concatHandler = http.HandlerFunc(svc.concatHandler)
// Wrap the Concat handler with our tracing middleware.
concatHandler = middleware.FromHTTPRequest(tracer, "Concat")(concatHandler)
// Create the Sum handler.
var sumHandler http.Handler
sumHandler = http.HandlerFunc(svc.sumHandler)
// Wrap the Sum handler with our tracing middleware.
sumHandler = middleware.FromHTTPRequest(tracer, "Sum")(sumHandler)
// Wire up the mux.
mux.Handle("/concat/", concatHandler)
mux.Handle("/sum/", sumHandler)
// Return the mux.
return mux
}