httpsrv

package module
v0.0.0-...-b013a37 Latest Latest
Warning

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

Go to latest
Published: May 30, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NopMiddleware

func NopMiddleware(method, path string, h http.Handler) http.Handler

func ReadFile

func ReadFile(fs http.FileSystem, name string) ([]byte, error)

ReadFile reads file contents from the file name in fs

func TemplateHandler

func TemplateHandler(t *template.Template, fn func(*http.Request) interface{}) http.Handler

TemplateHandler returns an http.Handler that renders the provided template

Types

type Dir

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

Dir is an http.Filesystem that extends Dir and provides the ability to server arbitrary files

func NewDir

func NewDir(dir http.Dir) *Dir

NewDir returns an initialized *Dir

func (*Dir) AddFile

func (d *Dir) AddFile(name string, fn func() (http.File, error))

AddFile adds a file at name provided by fn()

func (*Dir) Open

func (d *Dir) Open(name string) (http.File, error)

Open satisfies http.Filesystem

type Middleware

type Middleware interface {
	ID() string
	Handler(method, path string, h http.Handler) http.Handler
}

Middleware takes an http route and spits out a new handler

var H2C Middleware = MiddlewareFunc("h2c", func(method, path string, h http.Handler) http.Handler {
	s := &http2.Server{}
	h = h2c.NewHandler(h, s)

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		h.ServeHTTP(w, r)
	})
})

H2C wraps a handler and provides support for upgrading to h2c

func AccessLogger

func AccessLogger(w io.Writer) Middleware

AccessLogger logs to w when requests come in the apache log format

func MiddlewareFilterFunc

func MiddlewareFilterFunc(id string, fn func([]Middleware) []Middleware) Middleware

func MiddlewareFunc

func MiddlewareFunc(id string, fn func(method, path string, h http.Handler) http.Handler) Middleware

MiddlewareFunc is a helper for defining middleware

func SkipMiddleware

func SkipMiddleware(smws ...Middleware) Middleware

SkipMiddleware returns a MiddlewareFilter to skip specific middleware

type MiddlewareFilterer

type MiddlewareFilterer interface {
	Middleware
	Filter([]Middleware) []Middleware
}

type ReqValue

type ReqValue string

ReqValue is a value pulled from the request

func ParamValue

func ParamValue(r *http.Request, key string) ReqValue

ParamValue returns a ReqValue from the url path

func QueryValue

func QueryValue(r *http.Request, key string) ReqValue

QueryValue returns a ReqValue from the query string

func (ReqValue) Bool

func (r ReqValue) Bool() bool

Bool returns r as a bool

func (ReqValue) Float64

func (r ReqValue) Float64() float64

Float64 returns r as a float64

func (ReqValue) Int

func (r ReqValue) Int() int

Int returns r as an int

func (ReqValue) String

func (r ReqValue) String() string

func (ReqValue) UUID

func (r ReqValue) UUID() uuid.UUID

UUID returns r as a UUID

type Route

type Route struct {
	Method     string
	Path       string
	Handler    http.Handler
	Middleware []Middleware
}

A Route is the tuple of (method, path, handler) that is registered with the router

func AssetsRoute

func AssetsRoute(prefix string, fs http.FileSystem, mws ...Middleware) *Route

AssetsRoute returns a Route to handle static assets

type SPAConf

type SPAConf struct {
	Root              string
	IndexTemplate     *template.Template
	IndexTemplateData func(*http.Request, map[string]map[string]interface{}) interface{}
	IndexFilter       func(*http.Request) bool
	IndexMiddleware   []Middleware
	Assets            http.FileSystem
	AssetFile         string
	AssetPrefix       string
	AssetMiddleware   []Middleware
}

SPAConf is a helper for defining routes and asset handling for single page apps

func (SPAConf) Init

func (c SPAConf) Init(s *Server) error

Init initializes all the routes and confs for SPAConf

type Server

type Server struct {
	svc.Service
	// contains filtered or unexported fields
}

Server is the http server

func New

func New(addr string) *Server

New returns an initialized Server

func (*Server) AddListenAddr

func (s *Server) AddListenAddr(addr string)

AddListenAddr adds a new address to listen to when the server starts

func (*Server) AddMiddleware

func (s *Server) AddMiddleware(mw ...Middleware)

AddMiddleware adds global middleware to the server

func (*Server) Handle

func (s *Server) Handle(rts ...*Route)

Handle registers the provided route with the router

func (*Server) HandleNotFound

func (s *Server) HandleNotFound(h http.Handler, mw ...Middleware)

HandleNotFound registers a handler to run when a method is not found

func (*Server) Lookup

func (s *Server) Lookup(method, path string) *Route

Lookup finds the associated route that was registered with the provided method and path

func (*Server) NotFoundHandler

func (s *Server) NotFoundHandler() http.Handler

NotFoundHandler returns the registered not found handler

Directories

Path Synopsis
Package h2c implements the unencrypted "h2c" form of HTTP/2.
Package h2c implements the unencrypted "h2c" form of HTTP/2.

Jump to

Keyboard shortcuts

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