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"
)
// 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
}