test dynfunc package; standardize and refactor api #14

Merged
xdrm-brackets merged 19 commits from test/dynamic into 0.3.0 2020-04-04 10:09:20 +00:00
2 changed files with 11 additions and 15 deletions
Showing only changes of commit b0e25b431c - Show all commits

View File

@ -8,11 +8,11 @@ import (
"git.xdrm.io/go/aicra/internal/reqdata"
)
// httpServer wraps the aicra server to allow handling http requests
type httpServer Server
// httpHandler wraps the aicra server to allow handling http requests
type httpHandler Server
// ServeHTTP implements http.Handler and has to be called on each request
func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
func (server httpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
// 1. find a matching service in the config

View File

@ -2,7 +2,7 @@ package aicra
import (
"fmt"
"io"
"net/http"
"os"
"git.xdrm.io/go/aicra/datatype"
@ -81,24 +81,20 @@ func (s *Server) Handle(method, path string, fn interface{}) error {
return nil
}
// ToHTTPServer converts the server to a http server
func (s Server) ToHTTPServer() (*httpServer, error) {
// check if handlers are missing
// ToHTTPServer converts the server to a http.Handler
func (s Server) ToHTTPServer() (http.Handler, error) {
for _, service := range s.config.Services {
found := false
var hasAssociatedHandler bool
for _, handler := range s.handlers {
if handler.Method == service.Method && handler.Path == service.Pattern {
found = true
hasAssociatedHandler = true
break
}
}
if !found {
return nil, fmt.Errorf("%s '%s': %w", service.Method, service.Pattern, ErrNoHandlerForService)
if !hasAssociatedHandler {
return nil, fmt.Errorf("%s '%s': %w", service.Method, service.Pattern, ErrMissingHandler)
}
}
// 2. cast to http server
httpServer := httpServer(s)
return &httpServer, nil
return httpHandler(s), nil
}