Documentation
¶
Overview ¶
Package boilerserv provides a boilerplate implementation for a http server.
It also tries not to be opinionated about your packages (e.g. router, logger), so you can use your own desired dependencies.
Example:
type MyServerConfigurator struct {
listener net.Listener
}
func NewMyServerConfigurator(port int) (*MyServerConfigurator, error) {
listener, err := net.Listen("tcp", fmt.Springf(":%d", port))
if err != nil {
return nil, err
}
return &MyServerConfigurator{
listener: listener,
}, nil
}
func (sc MyServerConfigurator) Listener() net.Listener {
return sc.listener
}
func (MyServerConfigurator) ConfigureRoutes() http.Handler {
mux := http.ServeMux{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOk)
})
return &mux
}
func (sc *MyServerConfigurator) LogError(message string, port int) {
log.Println(message)
}
func (sc *MyServerConfigurator) LogInfo(message string, port int) {
log.Println(message)
}
func main() {
serverConfigurator, err := NewMyServerConfigurator(8080)
if err != nil {
log.Fatal(err)
}
server, err := boilerserv.StartServer(context.Background(), serverConfigurator)
if err != nil {
log.Fatal(err)
}
// Block the routine
_ = server.ListenAndHandleShutdown(context.Background())
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HttpServer ¶
type HttpServer struct {
// contains filtered or unexported fields
}
HttpServer wraps a http.Server and can handle graceful shutdown automatically. It implements the Server interface.
func StartServer ¶
func StartServer(ctx context.Context, configurator ServerConfigurator) (*HttpServer, error)
StartServer starts a new HttpServer by providing an implementation of a ServerConfigurator.
func (*HttpServer) ListenAndHandleShutdown ¶
func (s *HttpServer) ListenAndHandleShutdown(ctx context.Context) error
ListenAndHandleShutdown listens on the provided port of the provided net.Listener. It will handle shutdown when receiving an interruption signal.
func (*HttpServer) ShutdownGracefully ¶
func (s *HttpServer) ShutdownGracefully(ctx context.Context) error
ShutdownGracefully will shutdown the HttpServer gracefully.
Click to show internal directories.
Click to hide internal directories.