Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct {
Addr string
}
Info holds relevant information about the Server. This can be used in the future to hold information about: - number of requests received - average response time.
type Option ¶
An Option configures an App using the functional options paradigm popularized by Rob Pike. If you're unfamiliar with this style, see nolint: revive // line too long. https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and https://github.com/uber-go/guide/blob/master/style.md#functional-options
func WithAddress ¶
WithAddress will set the address field of the server.
Example ¶
package main import ( "fmt" "github.com/purposeinplay/go-commons/httpserver" ) func main() { opt := httpserver.WithAddress(":8080") fmt.Println(opt) }
Output: server.Address: :8080
func WithBaseContext ¶
WithBaseContext sets a predefined base context for all incoming http requests.
If cancelOnShutdown is set to true it will mark the baseContext as done(close the Done channel) whenever the server.Shutdown() method is called.
This is intended to use with long living tcp connections like Websockets in order to cancel the current open connections and allow the server to shutdown.
Example ¶
nolint: revive // line too long.
package main import ( "context" "fmt" "github.com/purposeinplay/go-commons/httpserver" ) func main() { type key string ctx := context.WithValue(context.Background(), key("server"), "example") opt := httpserver.WithBaseContext(ctx, true) fmt.Println(opt) }
Output: server.BaseContext: context.Background.WithValue(type httpserver_test.key, val example) server.CancelContextOnShutdown: true
func WithServerTimeouts ¶
func WithServerTimeouts( writeTimeout, readTimeout, idleTimeout, readHeaderTimeout time.Duration, ) Option
WithServerTimeouts will set the timeouts for the underlying HTTP server.
Example ¶
package main import ( "fmt" "time" "github.com/purposeinplay/go-commons/httpserver" ) func main() { opt := httpserver.WithServerTimeouts( time.Nanosecond, 2*time.Nanosecond, 3*time.Nanosecond, 4*time.Nanosecond, ) fmt.Println(opt) }
Output: server.WriteTimeout: 1 server.ReadTimeout: 2 server.IdleTimeout: 3 server.ReadHeaderTimeout: 4
func WithShutdownSignalsOption ¶
WithShutdownSignalsOption will attempt to shutdown the server when one of the proved os signals is sent to the application.
Example ¶
package main import ( "fmt" "syscall" "github.com/purposeinplay/go-commons/httpserver" ) func main() { opt := httpserver.WithShutdownSignalsOption( syscall.SIGINT, syscall.SIGTERM) fmt.Println(opt) }
Output: server.ShutdownSignals: [interrupt terminated]
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server handles the setup and shutdown of the http server for an http.Handler.
func New ¶
New will build a server with the defaults in place. You can use Options to override the defaults. Default list: - Address: ":8080".
func (*Server) ListenAndServe ¶
ListenAndServe is a wrapper over http.Server.ListenAndServe() that logs basic information and blocks execution until the Server.Shutdown() method is called.