test dynfunc package; standardize and refactor api #14
6
http.go
6
http.go
|
@ -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
|
||||
|
|
20
server.go
20
server.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue