-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.go
42 lines (34 loc) · 988 Bytes
/
server.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
package napodate
import (
"context"
"net/http"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
)
// NewHTTPServer is a good little server
func NewHTTPServer(ctx context.Context, endpoints Endpoints) http.Handler {
r := mux.NewRouter()
r.Use(commonMiddleware) // @see https://stackoverflow.com/a/51456342
r.Methods("GET").Path("/status").Handler(httptransport.NewServer(
endpoints.StatusEndpoint,
decodeStatusRequest,
encodeResponse,
))
r.Methods("GET").Path("/get").Handler(httptransport.NewServer(
endpoints.GetEndpoint,
decodeGetRequest,
encodeResponse,
))
r.Methods("POST").Path("/validate").Handler(httptransport.NewServer(
endpoints.ValidateEndpoint,
decodeValidateRequest,
encodeResponse,
))
return r
}
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}