Documentation ¶
Overview ¶
Package server provides HTTP server implementation. The server provided in here is opinionated and comes with good defaults.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run creates a http server, starts the server on a network address and then calls Serve to handle requests on incoming connections.
It sets up a server with the parameters provided by o. If the Opts supplied include a certificate and key, the server will accept https traffic and also automatically handle http->https redirect. Likewise, if the Opts include an email address, the server will accept https traffic and automatically handle http->https redirect.
The server shuts down cleanly after receiving any termination signal.
Example ¶
package main import ( "context" "fmt" "net/http" "os" "github.com/komuw/ong/log" "github.com/komuw/ong/middleware" "github.com/komuw/ong/mux" "github.com/komuw/ong/server" ) func main() { l := log.New(os.Stdout, 1000)(context.Background()) secretKey := "super-h@rd-pa$$word" mux := mux.New( l, middleware.WithOpts( "localhost", 65081, secretKey, middleware.DirectIpStrategy, l, ), nil, mux.NewRoute( "hello/", mux.MethodGet, hello("hello world"), ), mux.NewRoute( "check/:age/", mux.MethodAll, check(), ), ) opts := server.DevOpts(l) // dev options. // alternatively for production: // opts := server.LetsEncryptOpts("email@email.com", "*.some-domain.com") err := server.Run(mux, opts, l) if err != nil { fmt.Println(err) os.Exit(1) } } func hello(msg string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cspNonce := middleware.GetCspNonce(r.Context()) csrfToken := middleware.GetCsrfToken(r.Context()) fmt.Printf("hello called cspNonce: %s, csrfToken: %s", cspNonce, csrfToken) // use msg, which is a dependency specific to this handler fmt.Fprint(w, msg) } } func check() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { age := mux.Param(r.Context(), "age") _, _ = fmt.Fprintf(w, "Age is %s", age) } }
Output:
Types ¶
type Opts ¶ added in v0.0.14
type Opts struct {
// contains filtered or unexported fields
}
Opts are the various parameters(optionals) that can be used to configure a HTTP server.
Use either NewOpts, DevOpts, CertOpts, AcmeOpts or LetsEncryptOpts to get a valid Opts.
func AcmeOpts ¶ added in v0.0.51
AcmeOpts returns a new Opts that procures certificates from an ACME certificate authority. Also see LetsEncryptOpts
func DevOpts ¶
DevOpts returns a new Opts that has sensible defaults for tls, especially for dev environments. It also automatically creates the dev certifiates/key.
func LetsEncryptOpts ¶
LetsEncryptOpts returns a new Opts that procures certificates from letsencrypt. Also see AcmeOpts
func NewOpts ¶
func NewOpts( port uint16, maxBodyBytes uint64, readHeaderTimeout time.Duration, readTimeout time.Duration, writeTimeout time.Duration, handlerTimeout time.Duration, idleTimeout time.Duration, drainTimeout time.Duration, certFile string, keyFile string, email string, domain string, acmeURL string, ) Opts
NewOpts returns a new Opts.
port is the port at which the server should listen on.
maxBodyBytes is the maximum size in bytes for incoming request bodies. If this is zero, a reasonable default is used.
readHeaderTimeout is the amount of time a server will be allowed to read request headers. readTimeout is the maximum duration a server will use for reading the entire request, including the body. writeTimeout is the maximum duration before a server times out writes of the response. handlerTimeout is the maximum duration that handlers on the server will serve a request before timing out. idleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled. drainTimeout is the duration to wait for after receiving a shutdown signal and actually starting to shutdown the server. This is important especially in applications running in places like kubernetes.
certFile is a path to a tls certificate. keyFile is a path to a tls key.
email is the e-address that will be used if/when procuring certificates from an ACME certificate authority, eg letsencrypt. domain is the domain name of your website; it can be an exact domain, subdomain or wildcard. acmeURL is the URL of the ACME certificate authority's directory endpoint.
If certFile is a non-empty string, this will enable tls using certificates found on disk. If email is a non-empty string, this will enable tls using certificates procured from an ACME certificate authority.