migrate api.response to const errors; make HandlerFn return an api.Error; rename http HandlFunc to Handle;
This commit is contained in:
parent
5504e4b3ec
commit
af09466013
|
@ -4,28 +4,23 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// HandlerFunc manages an API request
|
||||
type HandlerFunc func(Request, *Response)
|
||||
// HandlerFn defines the handler signature
|
||||
type HandlerFn func(req Request, res *Response) Error
|
||||
|
||||
// Handler is an API handler ready to be bound
|
||||
type Handler struct {
|
||||
path string
|
||||
method string
|
||||
handle HandlerFunc
|
||||
Fn HandlerFn
|
||||
}
|
||||
|
||||
// NewHandler builds a handler from its http method and path
|
||||
func NewHandler(method, path string, handlerFunc HandlerFunc) *Handler {
|
||||
func NewHandler(method, path string, fn HandlerFn) (*Handler, error) {
|
||||
return &Handler{
|
||||
path: path,
|
||||
method: strings.ToUpper(method),
|
||||
handle: handlerFunc,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle fires a handler
|
||||
func (h *Handler) Handle(req Request, res *Response) {
|
||||
h.handle(req, res)
|
||||
Fn: fn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMethod returns the handler's HTTP method
|
||||
|
|
|
@ -16,29 +16,20 @@ type Response struct {
|
|||
err Error
|
||||
}
|
||||
|
||||
// NewResponse creates an empty response. An optional error can be passed as its first argument.
|
||||
func NewResponse(errors ...Error) *Response {
|
||||
res := &Response{
|
||||
// EmptyResponse creates an empty response.
|
||||
func EmptyResponse() *Response {
|
||||
return &Response{
|
||||
Status: http.StatusOK,
|
||||
Data: make(ResponseData),
|
||||
err: ErrorFailure(),
|
||||
err: ErrorFailure,
|
||||
Headers: make(http.Header),
|
||||
}
|
||||
|
||||
// optional error
|
||||
if len(errors) == 1 {
|
||||
res.err = errors[0]
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// SetError sets the error from a base error with error arguments.
|
||||
func (res *Response) SetError(baseError Error, arguments ...interface{}) {
|
||||
if len(arguments) > 0 {
|
||||
baseError.SetArguments(arguments[0], arguments[1:]...)
|
||||
}
|
||||
res.err = baseError
|
||||
// WithError sets the error from a base error with error arguments.
|
||||
func (res *Response) WithError(err Error) *Response {
|
||||
res.err = err
|
||||
return res
|
||||
}
|
||||
|
||||
// Error implements the error interface and dispatches to internal error.
|
||||
|
|
19
http.go
19
http.go
|
@ -18,7 +18,7 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
// 1. find a matching service in the config
|
||||
service := server.config.Find(req)
|
||||
if service == nil {
|
||||
response := api.NewResponse(api.ErrorUnknownService())
|
||||
response := api.EmptyResponse().WithError(api.ErrorUnknownService)
|
||||
response.ServeHTTP(res, req)
|
||||
logError(response)
|
||||
return
|
||||
|
@ -30,7 +30,7 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
// 3. extract URI data
|
||||
err := dataset.ExtractURI(req)
|
||||
if err != nil {
|
||||
response := api.NewResponse(api.ErrorMissingParam())
|
||||
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
||||
response.ServeHTTP(res, req)
|
||||
logError(response)
|
||||
return
|
||||
|
@ -39,7 +39,7 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
// 4. extract query data
|
||||
err = dataset.ExtractQuery(req)
|
||||
if err != nil {
|
||||
response := api.NewResponse(api.ErrorMissingParam())
|
||||
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
||||
response.ServeHTTP(res, req)
|
||||
logError(response)
|
||||
return
|
||||
|
@ -48,7 +48,7 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
// 5. extract form/json data
|
||||
err = dataset.ExtractForm(req)
|
||||
if err != nil {
|
||||
response := api.NewResponse(api.ErrorMissingParam())
|
||||
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
||||
response.ServeHTTP(res, req)
|
||||
logError(response)
|
||||
return
|
||||
|
@ -68,15 +68,13 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
// 7. fail if found no handler
|
||||
if foundHandler == nil {
|
||||
if found {
|
||||
r := api.NewResponse()
|
||||
r.SetError(api.ErrorUncallableService(), service.Method, service.Pattern)
|
||||
r := api.EmptyResponse().WithError(api.ErrorUncallableService)
|
||||
r.ServeHTTP(res, req)
|
||||
logError(r)
|
||||
return
|
||||
}
|
||||
|
||||
r := api.NewResponse()
|
||||
r.SetError(api.ErrorUnknownService(), service.Method, service.Pattern)
|
||||
r := api.EmptyResponse().WithError(api.ErrorUnknownService)
|
||||
r.ServeHTTP(res, req)
|
||||
logError(r)
|
||||
return
|
||||
|
@ -93,8 +91,9 @@ func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
apireq.Param = dataset.Data
|
||||
|
||||
// 10. execute
|
||||
response := api.NewResponse()
|
||||
foundHandler.Handle(*apireq, response)
|
||||
response := api.EmptyResponse()
|
||||
apiErr := foundHandler.Fn(*apireq, response)
|
||||
response.WithError(apiErr)
|
||||
|
||||
// 11. apply headers
|
||||
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
|
12
server.go
12
server.go
|
@ -47,13 +47,11 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) {
|
|||
}
|
||||
|
||||
// HandleFunc sets a new handler for an HTTP method to a path
|
||||
func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc) {
|
||||
handler := api.NewHandler(httpMethod, path, handlerFunc)
|
||||
s.handlers = append(s.handlers, handler)
|
||||
}
|
||||
|
||||
// Handle sets a new handler
|
||||
func (s *Server) Handle(handler *api.Handler) {
|
||||
func (s *Server) Handle(httpMethod, path string, fn api.HandlerFn) {
|
||||
handler, err := api.NewHandler(httpMethod, path, fn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s.handlers = append(s.handlers, handler)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue