2019-05-01 11:44:45 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-03-28 13:57:28 +00:00
|
|
|
// HandlerFn defines the handler signature
|
|
|
|
type HandlerFn func(req Request, res *Response) Error
|
2019-05-01 11:44:45 +00:00
|
|
|
|
|
|
|
// Handler is an API handler ready to be bound
|
|
|
|
type Handler struct {
|
|
|
|
path string
|
|
|
|
method string
|
2020-03-28 13:57:28 +00:00
|
|
|
Fn HandlerFn
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler builds a handler from its http method and path
|
2020-03-28 13:57:28 +00:00
|
|
|
func NewHandler(method, path string, fn HandlerFn) (*Handler, error) {
|
2019-05-01 11:44:45 +00:00
|
|
|
return &Handler{
|
|
|
|
path: path,
|
|
|
|
method: strings.ToUpper(method),
|
2020-03-28 13:57:28 +00:00
|
|
|
Fn: fn,
|
|
|
|
}, nil
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMethod returns the handler's HTTP method
|
|
|
|
func (h *Handler) GetMethod() string {
|
|
|
|
return h.method
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPath returns the handler's path
|
|
|
|
func (h *Handler) GetPath() string {
|
|
|
|
return h.path
|
|
|
|
}
|