forked from ironsmile/nedomi
/
new.go
35 lines (28 loc) · 1.22 KB
/
new.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
// Package handler deals with the main HTTP handler modules for nedomi. It describes the
// RequestHandler interface. Every subpackage *must* have a type which implements it.
// This file contains the function which returns a new RequestHandler
// based on its string name.
//
// New uses the handlerTypes map. This map is generated with
// `go generate` in the types.go file.
//go:generate go run ../tools/module_generator/main.go -template "types.go.template" -output "types.go"
package handler
import (
"fmt"
"github.com/ironsmile/nedomi/config"
"github.com/ironsmile/nedomi/types"
)
// New creates and returns a new RequestHandler identified by its module name.
// Identifier is the module's directory (hence its package name).
// Additionaly it receives handler specific config in the form of *json.RawMessage
// and types.Location representing the location the handler will be used for.
func New(cfg *config.Handler, l *types.Location, next types.RequestHandler) (types.RequestHandler, error) {
if cfg == nil {
return nil, fmt.Errorf("handler.New requires a non nil configuration")
}
fnc, ok := handlerTypes[cfg.Type]
if !ok {
return nil, fmt.Errorf("no such request handler module: %s", cfg.Type)
}
return fnc(cfg, l, next)
}