Skip to content

Instantly share code, notes, and snippets.

@Daniel1984
Created April 22, 2020 17:51
Show Gist options
  • Save Daniel1984/d0a8215c4b1f772a8e5641c20517be76 to your computer and use it in GitHub Desktop.
Save Daniel1984/d0a8215c4b1f772a8e5641c20517be76 to your computer and use it in GitHub Desktop.
// 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