ToHTTPServer now returns the exported field http.Handler instead of an unexported type

This commit is contained in:
Adrien Marquès 2020-04-04 10:39:02 +02:00
parent b1498e59c1
commit b0e25b431c
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 11 additions and 15 deletions

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
} }