Documentation
¶
Overview ¶
Package yethttp provides helpers for working with http.
Example:
type WebApp struct { yethttp.EmbeddableServerWrapper } func NewWebApp(logger yetlog.Logger) WebApp { serverWrapper := yethttp.NewEmbeddableServerWrapper(logger, 8080) return WebApp{ EmbeddableServerWrapper: serverWrapper, } } func main() { webApp := NewWebApp(yetlog.NewNullLogger()) if err := webApp.Serve(context.Background()); err != nil { panic(err) } if err := webApp.WaitForShutdown(context.Background()); err != nil { panic(err) } }
Index ¶
- Variables
- type AfterShutdownFunc
- type EmbeddableServerWrapper
- func (e *EmbeddableServerWrapper) AddAfterShutdownFunc(afterShutdownFunc AfterShutdownFunc)
- func (e *EmbeddableServerWrapper) GracefulShutdown(ctx context.Context) error
- func (e *EmbeddableServerWrapper) Serve(ctx context.Context, routesFunc RoutesFunc) error
- func (e *EmbeddableServerWrapper) SetStartServerFunc(startServerFunc StartServerFunc)
- func (e *EmbeddableServerWrapper) WaitForShutdown(ctx context.Context) error
- type RoutesFunc
- type StartServerFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultRoutesFunc = func() http.Handler { return http.DefaultServeMux }
DefaultRoutesFunc uses the http.DefaultServeMux which shouldn't be used in any productive application. This method is meant to be used for prototyping or testing.
var DefaultStartServerFunc = func(server *http.Server) error {
return server.ListenAndServe()
}
DefaultStartServerFunc provides a default implementation for starting a http.Server which basically calls its ListenAndServe() method.
Functions ¶
This section is empty.
Types ¶
type AfterShutdownFunc ¶ added in v0.6.0
type AfterShutdownFunc func()
AfterShutdownFunc is a function which will be executed after the server shuts down.
type EmbeddableServerWrapper ¶ added in v0.4.0
type EmbeddableServerWrapper struct { HttpServer *http.Server Port int // contains filtered or unexported fields }
EmbeddableServerWrapper wraps a http.Server and can handle server startup, routing and graceful shutdown.
func NewEmbeddableServerWrapper ¶ added in v0.4.0
func NewEmbeddableServerWrapper(logger yetlog.Logger, port int) EmbeddableServerWrapper
NewEmbeddableServerWrapper returns a new EmbeddableServerWrapper.
func (*EmbeddableServerWrapper) AddAfterShutdownFunc ¶ added in v0.6.0
func (e *EmbeddableServerWrapper) AddAfterShutdownFunc(afterShutdownFunc AfterShutdownFunc)
AddAfterShutdownFunc will add an AfterShutdownFunc to the server.
func (*EmbeddableServerWrapper) GracefulShutdown ¶ added in v0.4.0
func (e *EmbeddableServerWrapper) GracefulShutdown(ctx context.Context) error
GracefulShutdown will shutdown the underlying http.Server gracefully. This method can be overwritten to be able to use framework specific function calls.
func (*EmbeddableServerWrapper) Serve ¶ added in v0.4.0
func (e *EmbeddableServerWrapper) Serve(ctx context.Context, routesFunc RoutesFunc) error
Serve starts the server and listens for new connections.
func (*EmbeddableServerWrapper) SetStartServerFunc ¶ added in v0.4.1
func (e *EmbeddableServerWrapper) SetStartServerFunc(startServerFunc StartServerFunc)
SetStartServerFunc is used to overwrite the internal StartServerFunc. It should be called before using the Serve() method.
func (*EmbeddableServerWrapper) WaitForShutdown ¶ added in v0.4.0
func (e *EmbeddableServerWrapper) WaitForShutdown(ctx context.Context) error
WaitForShutdown blocks the go routine and will only continue when it receives a kill signal (SIGINT, SIGTERM, ...).
type RoutesFunc ¶ added in v0.5.1
RoutesFunc is used to define the server routes and handlers.
type StartServerFunc ¶ added in v0.4.1
StartServerFunc is a function which can be used to customize the server start behavior.