From 2ee48560b62f5648822c05ad87fc5ab49788e87e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 21 Mar 2020 16:53:19 +0100 Subject: [PATCH] make ToHTTPServer() check for missing handlers - check for missing handlers in ToHTTPServer() - rename HTTP() to ToHTTPServer() - remove logs --- server.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/server.go b/server.go index cacc0e4..bc11038 100644 --- a/server.go +++ b/server.go @@ -1,8 +1,8 @@ package aicra import ( + "fmt" "io" - "log" "os" "git.xdrm.io/go/aicra/api" @@ -42,12 +42,6 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) { return nil, err } - // 4. log configuration services - log.Printf("🔧 Reading configuration '%s'\n", configPath) - for _, service := range i.config.Services { - log.Printf(" ->\t%s\t'%s'\n", service.Method, service.Pattern) - } - return i, nil } @@ -63,15 +57,24 @@ func (s *Server) Handle(handler *api.Handler) { s.handlers = append(s.handlers, handler) } -// HTTP converts the server to a http server -func (s Server) HTTP() httpServer { +// ToHTTPServer converts the server to a http server +func (s Server) ToHTTPServer() (*httpServer, error) { - // 1. log available handlers - log.Printf("🔗 Mapping handlers\n") - for i := 0; i < len(s.handlers); i++ { - log.Printf(" ->\t%s\t'%s'\n", s.handlers[i].GetMethod(), s.handlers[i].GetPath()) + // check if handlers are missing + for _, service := range s.config.Services { + found := false + for _, handler := range s.handlers { + if handler.GetPath() == service.Pattern { + found = true + break + } + } + if !found { + return nil, fmt.Errorf("missing handler for %s '%s'", service.Method, service.Pattern) + } } // 2. cast to http server - return httpServer(s) + httpServer := httpServer(s) + return &httpServer, nil }