httpmin

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: 0BSD Imports: 21 Imported by: 0

README

httpmin

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

Install

go get -u github.com/Angus-Warman/httpmin

Get started

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()
}

If you have a public folder:

package main

import (
	"embed"

	"github.com/Angus-Warman/httpmin"
)

//go:embed all:public
var publicFiles embed.FS

func main() {
	httpmin.New().ServeEmbedded(publicFiles).Run()
}

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

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

type Chassis struct {
	Mux *http.ServeMux
	// contains filtered or unexported fields
}

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

func (c *Chassis) DefaultEnvVar(key, value string) *Chassis

Only sets if environment variable not already set

func (*Chassis) EnvFile

func (c *Chassis) EnvFile(path string) *Chassis

Read a .env formatted file, setting any environment variables that aren't already set

func (*Chassis) OnIP

func (c *Chassis) OnIP(ip string) *Chassis

Alternatively, use a "IP" env variable

func (*Chassis) OnPort

func (c *Chassis) OnPort(port string) *Chassis

The port used comes from: env variables, .env file, this function, "8080" (in that order)

func (*Chassis) PublicIP

func (c *Chassis) PublicIP() *Chassis

Listen on 0.0.0.0, alternatively use a "IP" env variable

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

func (c *Chassis) RouteHandler(pattern string, handler http.Handler) *Chassis

Registers the handler for the given pattern. Panics if the pattern has already been registered.

func (*Chassis) Run

func (c *Chassis) Run()

Serves, printing any errors and exiting on a failure

func (*Chassis) Serve

func (c *Chassis) Serve() error

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

func (c *Chassis) ServeEmbedded(folder embed.FS) *Chassis

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

func (c *Chassis) ServeFolder(rootPath string) *Chassis

Serves files from directory.

Serves "clean" URLs, /page -> /page.html.

func (*Chassis) Server

func (c *Chassis) Server() (*http.Server, error)

Builds the underlying http.Server and sets up HTTPS as configured

func (*Chassis) Use

func (c *Chassis) Use(middleware func(http.Handler) http.Handler) *Chassis

Applied in registration order

func (*Chassis) UseDefaultMiddleware

func (c *Chassis) UseDefaultMiddleware(use bool) *Chassis

middleware.LogRequests and middleware.RecoverPanics

func (*Chassis) UseHTTPS

func (c *Chassis) UseHTTPS(certFile, keyFile string) *Chassis

func (*Chassis) UseSelfSignedHTTPS

func (c *Chassis) UseSelfSignedHTTPS() *Chassis

func (*Chassis) UseSelfSignedHTTPSFromFolder

func (c *Chassis) UseSelfSignedHTTPSFromFolder(certFolder string) *Chassis

Folder will contain cert.pem and key.pem after generating

func (*Chassis) WithLogger

func (c *Chassis) WithLogger(logger *log.Logger) *Chassis

Set as nil to disable logging

Directories

Path Synopsis
examples
parserequest command
protectRoutes command
recoverPanics command
selfSignedHTTPS command
stream command
theLot command
websocket command

Jump to

Keyboard shortcuts

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