Documentation
¶
Overview ¶
The bare minimum required to launch a sensible Go server.
Three goals:
- Minimal boilerplate
- Sensible defaults
- Easy configuration
Features:
- Environment variable handling
- Efficient embedded asset serving
- Graceful shutdown
- Request logging
- Recovers from panics
Optional:
- HTTPS (including self-signed)
- CORS
- JWT authentication
- SSE streaming
Hello World ¶
package main
import (
"net/http"
"github.com/Angus-Warman/httpmin"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
}
func main() {
httpmin.New().Route("/", helloWorld).Run()
}
Serve static files ¶
package main
import (
"net/http"
"embed"
"github.com/Angus-Warman/httpmin"
)
//go:embed all:public
var publicFiles embed.FS
func main() {
httpmin.New().ServeEmbedded(publicFiles).Run()
}
See Chassis for more details.
See github.com/Angus-Warman/httpmin/tree/main/_examples for in-depth demos.
Index ¶
- func LoadEnvFile()
- func LoadEnvFileFrom(path string)
- type Chassis
- func (c *Chassis) DefaultEnvVar(key, value string) *Chassis
- func (c *Chassis) EnvFile(path string) *Chassis
- func (c *Chassis) OnIP(ip string) *Chassis
- func (c *Chassis) OnPort(port string) *Chassis
- func (c *Chassis) PublicIP() *Chassis
- func (c *Chassis) Route(pattern string, handler func(w http.ResponseWriter, r *http.Request)) *Chassis
- func (c *Chassis) RouteHandler(pattern string, handler http.Handler) *Chassis
- func (c *Chassis) Run()
- func (c *Chassis) Serve() error
- func (c *Chassis) ServeEmbedded(folder embed.FS) *Chassis
- func (c *Chassis) ServeFolder(rootPath string) *Chassis
- func (c *Chassis) Server() (*http.Server, error)
- func (c *Chassis) Use(middleware func(http.Handler) http.Handler) *Chassis
- func (c *Chassis) UseDefaultMiddleware(use bool) *Chassis
- func (c *Chassis) UseHTTPS(certFile, keyFile string) *Chassis
- func (c *Chassis) UseSelfSignedHTTPS() *Chassis
- func (c *Chassis) UseSelfSignedHTTPSFromFolder(certFolder string) *Chassis
- func (c *Chassis) WithLogger(logger *log.Logger) *Chassis
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadEnvFile ¶ added in v0.0.9
func LoadEnvFile()
func LoadEnvFileFrom ¶ added in v0.0.9
func LoadEnvFileFrom(path string)
Types ¶
type Chassis ¶
func Blank ¶ added in v0.0.4
func Blank() *Chassis
Plain option, will serve http://localhost:8080 unless env variables specify
func New ¶
func New() *Chassis
Default option, reads .env file, logs incoming requests, handles panics, will serve http://localhost:8080 unless env variables specify
func (*Chassis) DefaultEnvVar ¶
Only sets if environment variable not already set
func (*Chassis) EnvFile ¶
Read a .env formatted file, setting any environment variables that aren't already set
func (*Chassis) OnPort ¶
The port used comes from: env variables, .env file, this function, "8080" (in that order)
func (*Chassis) Route ¶
func (c *Chassis) Route(pattern string, handler func(w http.ResponseWriter, r *http.Request)) *Chassis
Registers the handler function for the given pattern. Panics if the pattern has already been registered.
func (*Chassis) RouteHandler ¶
Registers the handler for the given pattern. Panics if the pattern has already been registered.
func (*Chassis) Serve ¶
Builds the underlying http.Server, sets up HTTPS as configured, and serves
Uses serveWithIntercept to catch signals and perform a graceful shutdown
func (*Chassis) ServeEmbedded ¶
Serves files from embedded directory. Pre-computes gzip data for compressed responses. Serves "clean" URLs, /page -> /page.html
//go:embed all:public var publicFiles embed.FS c.ServeEmbedded(publicFiles)
func (*Chassis) ServeFolder ¶
Serves files from directory.
Serves "clean" URLs, /page -> /page.html.
func (*Chassis) UseDefaultMiddleware ¶
middleware.LogRequests and middleware.RecoverPanics
func (*Chassis) UseSelfSignedHTTPS ¶
func (*Chassis) UseSelfSignedHTTPSFromFolder ¶
Folder will contain cert.pem and key.pem after generating
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
parserequest
command
|
|
|
protectRoutes
command
|
|
|
recoverPanics
command
|
|
|
selfSignedHTTPS
command
|
|
|
stream
command
|
|
|
theLot
command
|
|
|
websocket
command
|
|