-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
36 lines (29 loc) · 957 Bytes
/
router.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
package service
import (
"net/http"
"github.com/go-chi/chi"
)
// EvaluationHandler is a contract to a notification handler.
type EvaluationHandler interface {
Evaluate() func(w http.ResponseWriter, r *http.Request)
Validate() func(w http.ResponseWriter, r *http.Request)
Errors() func(w http.ResponseWriter, r *http.Request)
}
// NewRouter is a construction function for router that handles evaluation operations.
func NewRouter(nh EvaluationHandler) http.Handler {
router := chi.NewRouter()
api := []struct {
MethodFunc func(pattern string, handlerFn http.HandlerFunc)
Method string
Path string
HandleFunc func() func(w http.ResponseWriter, r *http.Request)
}{
{router.Post, "POST", "/evaluate", nh.Evaluate},
{router.Post, "POST", "/validate", nh.Validate},
{router.Get, "GET", "/errors", nh.Errors},
}
for _, endpoint := range api {
endpoint.MethodFunc(endpoint.Path, endpoint.HandleFunc())
}
return router
}