httputil

package
v0.0.0-...-29c6822 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2018 License: MIT, MIT Imports: 20 Imported by: 0

Documentation

Overview

Package httputil provides utilities for configuring an HTTPs server.

Index

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/as/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

func ListenAndServe(opts ...Option) error

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

type Middleware func(http.Handler) http.Handler

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/as/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

func WithACMEHosts(hosts []string) Option

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

func WithAddress(addr string) Option

WithAddress configures the server listening port and address. If left unspecified, :https will be used by default.

func WithAutocertCache

func WithAutocertCache(cache autocert.Cache) Option

WithAutocertCache configures a custom autocert.Cache for Let's Encrypt Certificates.

func WithCertCache

func WithCertCache(dir string) Option

WithCertCache configures a directory to store Let's Encrypt certificates.

func WithHTTPHandler

func WithHTTPHandler(h http.Handler) Option

WithHTTPHandler configures the server to use a custom HTTP Handler. If unspecified, http.DefaultServeMux will be used.

func WithKeyPair

func WithKeyPair(cert, key string) Option

WithKeyPair configures a TLS certificate to be used in the server TLS Config.

func WithLogger

func WithLogger(logger log.Logger) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL