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 or LetsEncryptOpts to get a valid Opts.
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 by internally calling [CreateDevCertKey]
func LetsEncryptOpts ¶
LetsEncryptOpts returns a new Opts that procures certificates from Letsencrypt.
func NewOpts ¶
func NewOpts( port uint16, readHeaderTimeout time.Duration, readTimeout time.Duration, writeTimeout time.Duration, handlerTimeout time.Duration, idleTimeout time.Duration, certFile string, keyFile string, email string, domain string, ) Opts
NewOpts returns a new Opts.
port is the port at which the server should listen on.
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.
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 letsencrypt. domain is the domain name of your website; it can be an exact domain, subdomain or wildcard.
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 letsencrypt.