This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
51 lines (43 loc) · 1.45 KB
/
http.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
package http
import (
"encoding/json"
"errors"
"net/http"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"golang.org/x/net/context"
"github.com/artefactual-labs/archivematica-workflow/pkg/endpoints"
)
var (
// ErrBadRouting is returned when an expected path variable is missing.
// It always indicates programmer error.
ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)")
)
// NewHandler returns a handler that makes a set of endpoints available on
// predefined paths.
func NewHandler(ctx context.Context, eps endpoints.Endpoints, logger log.Logger) http.Handler {
r := mux.NewRouter()
options := []httptransport.ServerOption{}
r.Methods("GET").Path("/workflows/{id}").Handler(httptransport.NewServer(
ctx,
eps.WorkflowGetEndpoint,
decodeGetWorkflowRequest,
encodeGenericResponse,
options...,
))
return r
}
func decodeGetWorkflowRequest(_ context.Context, r *http.Request) (request interface{}, err error) {
vars := mux.Vars(r)
id, ok := vars["id"]
if !ok {
return nil, ErrBadRouting
}
return endpoints.WorkflowGetRequest{ID: id}, nil
}
// encodeGenericResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer. Primarily useful in a server.
func encodeGenericResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}