generated from sumelms/microservice-template
/
handler.go
45 lines (33 loc) · 1.65 KB
/
handler.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
package http
import (
"net/http"
kittransport "github.com/go-kit/kit/transport"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"github.com/sumelms/microservice-course/internal/subscription/domain"
"github.com/sumelms/microservice-course/internal/subscription/endpoints"
"github.com/sumelms/microservice-course/pkg/errors"
)
func NewHTTPHandler(r *mux.Router, s domain.ServiceInterface, logger log.Logger) {
opts := []kithttp.ServerOption{
kithttp.ServerErrorHandler(kittransport.NewLogErrorHandler(logger)),
kithttp.ServerErrorEncoder(errors.EncodeError),
}
subscriptionRouter := NewSubscriptionRouter(s, opts...)
r.PathPrefix("/subscriptions").Handler(subscriptionRouter)
}
func NewSubscriptionRouter(s domain.ServiceInterface, opts ...kithttp.ServerOption) *mux.Router {
r := mux.NewRouter().PathPrefix("/subscriptions").Subrouter().StrictSlash(true)
listSubscriptionsHandler := endpoints.NewListSubscriptionsHandler(s, opts...)
r.Handle("", listSubscriptionsHandler).Methods(http.MethodGet)
createSubscriptionHandler := endpoints.NewCreateSubscriptionHandler(s, opts...)
r.Handle("", createSubscriptionHandler).Methods(http.MethodPost)
findSubscriptionHandler := endpoints.NewFindSubscriptionHandler(s, opts...)
r.Handle("/{uuid}", findSubscriptionHandler).Methods(http.MethodGet)
deleteSubscriptionHandler := endpoints.NewDeleteSubscriptionHandler(s, opts...)
r.Handle("/{uuid}", deleteSubscriptionHandler).Methods(http.MethodDelete)
updateSubscriptionHandler := endpoints.NewUpdateSubscriptionHandler(s, opts...)
r.Handle("/{uuid}", updateSubscriptionHandler).Methods(http.MethodPut)
return r
}