Created
April 22, 2020 17:51
-
-
Save Daniel1984/d0a8215c4b1f772a8e5641c20517be76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pkg/server/server.go | |
package server | |
import ( | |
"errors" | |
"log" | |
"net/http" | |
"github.com/julienschmidt/httprouter" | |
) | |
type Server struct { | |
srv *http.Server | |
} | |
func Get() *Server { | |
return &Server{ | |
srv: &http.Server{}, | |
} | |
} | |
func (s *Server) WithAddr(addr string) *Server { | |
s.srv.Addr = addr | |
return s | |
} | |
func (s *Server) WithErrLogger(l *log.Logger) *Server { | |
s.srv.ErrorLog = l | |
return s | |
} | |
func (s *Server) WithRouter(router *httprouter.Router) *Server { | |
s.srv.Handler = router | |
return s | |
} | |
func (s *Server) Start() error { | |
if len(s.srv.Addr) == 0 { | |
return errors.New("Server missing address") | |
} | |
if s.srv.Handler == nil { | |
return errors.New("Server missing handler") | |
} | |
return s.srv.ListenAndServe() | |
} | |
func (s *Server) Close() error { | |
return s.srv.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment