-
Notifications
You must be signed in to change notification settings - Fork 50
/
handlers_service_validator.go
51 lines (41 loc) · 1.26 KB
/
handlers_service_validator.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 handlers
import (
"net/http"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/server/services"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/api/utils"
)
// serviceValidator is an HTTP filter for validating that the service
// specified as part of the path is valid.
type serviceValidator struct {
handler types.APIFunc
}
// NewServiceValidator returns a new filter for validating that the service
// specified as part of the path is valid.
func NewServiceValidator() types.Middleware {
return &serviceValidator{}
}
func (h *serviceValidator) Name() string {
return "service-validator"
}
func (h *serviceValidator) Handler(m types.APIFunc) types.APIFunc {
return (&serviceValidator{m}).Handle
}
// Handle is the type's Handler function.
func (h *serviceValidator) Handle(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
if !store.IsSet("service") {
return utils.NewStoreKeyErr("service")
}
serviceName := store.GetString("service")
service := services.GetStorageService(ctx, serviceName)
if service == nil {
return utils.NewNotFoundError(serviceName)
}
ctx = context.WithStorageService(ctx, service)
return h.handler(ctx, w, req, store)
}