Documentation ¶
Overview ¶
Package httputil provides utilities for configuring an HTTPs server.
Index ¶
- func ChainFrom(h http.Handler, m ...Middleware) http.Handler
- func ListenAndServe(opts ...Option) error
- type BasicAuth
- type Middleware
- type Option
- func Simple(configPath string, handler http.Handler, httpAddr string, ...) []Option
- func WithACMEHosts(hosts []string) Option
- func WithAddress(addr string) Option
- func WithAutocertCache(cache autocert.Cache) Option
- func WithCertCache(dir string) Option
- func WithDisableRedirect(disable bool) Option
- func WithHTTPHandler(h http.Handler) Option
- func WithKeyPair(cert, key string) Option
- func WithLogger(logger log.Logger) Option
- func WithMiddlewareChain(outer Middleware, others ...Middleware) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChainFrom ¶
func ChainFrom(h http.Handler, m ...Middleware) http.Handler
ChainFrom wraps an HTTP Handler with the provided Middlewares.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/micromdm/go4/httputil" ) func main() { h := httputil.ChainFrom(myHandler(), annotate("one"), annotate("two"), annotate("three"), ) srv := httptest.NewServer(h) defer srv.Close() if _, err := http.Get(srv.URL); err != nil { panic(err) } } func annotate(s string) httputil.Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println("annotate: ", s) next.ServeHTTP(w, r) }) } } func myHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { }) }
Output: annotate: one annotate: two annotate: three
func ListenAndServe ¶
ListenAndServe starts an HTTP server and runs until it receives an Interrupt signal or an error.
With a default config, the server will bind to port 443 and will try to use Let's Encrypt to manage the server certificate.
Types ¶
type BasicAuth ¶
type BasicAuth struct {
Username, Password string
// Use to write a custom response to the client. If nil, a default WWW-Authenticate response is sent.
FailedAuthResponseFunc func(w http.ResponseWriter)
}
BasicAuth implements Middleware for HTTP Basic Auth.
func (*BasicAuth) Middleware ¶
func (auth *BasicAuth) Middleware() Middleware
Middleware is an HTTP Middleware that checks for Basic Auth credentials.
type Middleware ¶
Middleware is a chainable decorator for HTTP Handlers.
func Chain ¶
func Chain(outer Middleware, others ...Middleware) Middleware
Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.
Chain is identical to the go-kit helper for Endpoint Middleware.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/micromdm/go4/httputil" ) func main() { h := httputil.Chain( annotate("one"), annotate("two"), annotate("three"), )(myHandler()) srv := httptest.NewServer(h) defer srv.Close() if _, err := http.Get(srv.URL); err != nil { panic(err) } } func annotate(s string) httputil.Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println("annotate: ", s) next.ServeHTTP(w, r) }) } } func myHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { }) }
Output: annotate: one annotate: two annotate: three
func HTTPDebugMiddleware ¶
func HTTPDebugMiddleware(out io.Writer, printBody bool, logger func(...interface{}) error) Middleware
HTTPDebugMiddleware is a Middleware which prints the HTTP request and response to out. Use os.Stdout to print to standard out. If printBody is false, only the HTTP headers are printed. The Middleware requires a logger in case the request fails.
Example: handler = HTTPDebugMiddleware(debugOut, true, nopLogger)(handler)
type Option ¶
type Option func(*serverConfig)
Option configures the ListenAndServe function.
func Simple ¶
func Simple( configPath string, handler http.Handler, httpAddr string, certPath, keyPath string, useTLS bool, logger log.Logger, whitelistHosts ...string, ) []Option
Simple returns a slice of ListenAndServe options that are most common for a micromdm server project's main.go
func WithACMEHosts ¶
WithACMEHosts configures a list of domains to whitelist for Let's Encrypt. If unspecified, the server will whitelist the first successful ServerName for which it is able to get a certificate.
func WithAddress ¶
WithAddress configures the server listening port and address. If left unspecified, :https will be used by default.
func WithAutocertCache ¶
WithAutocertCache configures a custom autocert.Cache for Let's Encrypt Certificates.
func WithCertCache ¶
WithCertCache configures a directory to store Let's Encrypt certificates.
func WithDisableRedirect ¶
WithDisableRedirect configures whether a listener will be started on :80 to redirect requests to :443
func WithHTTPHandler ¶
WithHTTPHandler configures the server to use a custom HTTP Handler. If unspecified, http.DefaultServeMux will be used.
func WithKeyPair ¶
WithKeyPair configures a TLS certificate to be used in the server TLS Config.
func WithLogger ¶
WithLogger provides a logger for ListenAndServe. Not to be confused with an HTTP Logging Middleware for the HTTP Handler itself.
func WithMiddlewareChain ¶
func WithMiddlewareChain(outer Middleware, others ...Middleware) Option
WithMiddlewareChain chains Middleware for http.DefaultServeMux. If using WithHTTPHandler, the handler must already be wrapped with appropriate Middleware.