www

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: BSD-2-Clause Imports: 12 Imported by: 0

README

Web / HTTP utilities in Go

Features:

  • Static website router (http.Handler)
  • Backend + frontend router (http.Handler)
  • Status & response body size recorder (http.ResponseWriter)
  • RespondJSON / RespondText
  • HTTP router (based on gorilla/mux)
  • Generate XML sitemap from static website FS
  • Panic recovery middleware
  • Logging middleware
  • Rate limiting middleware
  • Timeout middleware
  • CORS middleware
  • Authorization middleware

Documentation

Index

Constants

View Source
const (
	HeaderContentType    = "Content-Type"
	ContentTypeJSON      = "application/json"
	ContentTypePlainText = "text/plain; charset=utf-8"
)

Variables

This section is empty.

Functions

func NewHTTPAccessLogMiddleware added in v0.0.1

func NewHTTPAccessLogMiddleware(cb func(AccessLogRequestData)) func(http.Handler) http.Handler

func RespondJSON

func RespondJSON(w http.ResponseWriter, status int, v any) error

Writes a JSON response.

func RespondText

func RespondText(w http.ResponseWriter, status int, s string) error

Writes a plain text response

Types

type AccessLogRequestData added in v0.0.3

type AccessLogRequestData struct {
	Path                string
	IPAddr              string
	StatusCode          int
	NumBodyBytesWritten int
}

type GorillaRouter

type GorillaRouter struct {
	// contains filtered or unexported fields
}

func NewGorillaRouter

func NewGorillaRouter() *GorillaRouter

func (*GorillaRouter) HandleEndpoint

func (gorillaRouter *GorillaRouter) HandleEndpoint(path, method string, handlerFunc http.HandlerFunc)

func (*GorillaRouter) HandlePrefix

func (gorillaRouter *GorillaRouter) HandlePrefix(path string, handler http.Handler)

func (*GorillaRouter) ParseURIParams

func (gorillaRouter *GorillaRouter) ParseURIParams(r *http.Request) map[string]string

func (*GorillaRouter) ServeHTTP

func (gorillaRouter *GorillaRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

type PrefixRouter

type PrefixRouter struct {
	// contains filtered or unexported fields
}

PrefixRouter handles requests for a website and a backend API.

func NewPrefixRouter

func NewPrefixRouter(config PrefixRouterConfig) (*PrefixRouter, error)

func (*PrefixRouter) ServeHTTP

func (h *PrefixRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

This function routes requests to the appropriate handler depending if they are for the backend API or the file server (= website files).

type PrefixRouterConfig

type PrefixRouterConfig struct {
	PathPrefix           string
	HandlerWithPrefix    http.Handler
	HandlerWithoutPrefix http.Handler
}

func (PrefixRouterConfig) Validate

func (c PrefixRouterConfig) Validate() error

type Recorder

type Recorder struct {
	http.ResponseWriter
	StatusCode          int
	NumBodyBytesWritten int
}

Recorder implements the http.ResponseWriter interface. It stores data about the current request / response and forwards it to the underlying response writer

func (*Recorder) Write

func (w *Recorder) Write(data []byte) (int, error)

func (*Recorder) WriteHeader

func (w *Recorder) WriteHeader(statusCode int)

type Router

type Router interface {
	http.Handler
	HandleEndpoint(method string, path string, handlerFunc http.HandlerFunc)
	HandlePrefix(path string, handler http.Handler)
	ParseURIParams(*http.Request) map[string]string
}

type Sitemap added in v0.0.1

type Sitemap struct {
	URLSet []SitemapURL `xml:"urlset"`
}

func NewSitemap added in v0.0.1

func NewSitemap(baseURL string, fsys fs.FS) (*Sitemap, error)

func (*Sitemap) String added in v0.0.1

func (s *Sitemap) String() (string, error)

type SitemapURL added in v0.0.1

type SitemapURL struct {
	XMLName    xml.Name  `xml:"url"`
	Location   string    `xml:"location"`
	LastMod    time.Time `xml:"lastmod"`
	ChangeFreq string    `xml:"changefreq,omitempty"`
	Priority   float32   `xml:"priority,omitempty"`
}

type StaticWebsite added in v0.0.1

type StaticWebsite struct {
	// contains filtered or unexported fields
}

func NewStaticWebsite added in v0.0.1

func NewStaticWebsite(c StaticWebsiteConfig) (*StaticWebsite, error)

subFS: Set it to "." for current dir

func (*StaticWebsite) HTTPHandler added in v0.0.1

func (sw *StaticWebsite) HTTPHandler() (string, http.Handler)

func (*StaticWebsite) ServeHTTP added in v0.0.1

func (h *StaticWebsite) ServeHTTP(w http.ResponseWriter, r *http.Request)

type StaticWebsiteConfig added in v0.0.1

type StaticWebsiteConfig struct {
	Fsys             fs.FS  // static file system
	SubDir           string // "." for current directory
	FallbackPagePath string // ex: "404.html"
}

func (StaticWebsiteConfig) Validate added in v0.0.1

func (c StaticWebsiteConfig) Validate() error

Jump to

Keyboard shortcuts

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