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" "git.xdrm.io/go/aicra/internal/reqdata"
) )
// httpServer wraps the aicra server to allow handling http requests // httpHandler wraps the aicra server to allow handling http requests
type httpServer Server type httpHandler Server
// ServeHTTP implements http.Handler and has to be called on each request // 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() defer req.Body.Close()
// 1. find a matching service in the config // 1. find a matching service in the config

View File

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