router

package
v0.0.0-...-00d5bf9 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: MIT Imports: 11 Imported by: 0

README

gserv/router

an extremely fast, 0-garbage http router

Benchmarks
➜ go test -bench=. -cpu 8 -tags httprouter
PASS
BenchmarkHttpRouter5Params-8    10000000               162 ns/op             160 B/op          1 allocs/op
BenchmarkHttpRouterStatic-8     50000000              24.1 ns/op               0 B/op          0 allocs/op
BenchmarkJumpRouter5Params-8     5000000               376 ns/op               0 B/op          0 allocs/op
BenchmarkJumpRouterStatic-8     20000000              62.0 ns/op               0 B/op          0 allocs/op
TODO
  • Add fasthttp benchmarks and tests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultNotFoundHandler

func DefaultNotFoundHandler(w http.ResponseWriter, req *http.Request, _ Params)

DefaultNotFoundHandler is the default panic handler

func DefaultPanicHandler

func DefaultPanicHandler(w http.ResponseWriter, req *http.Request, v any)

DefaultPanicHandler is the default panic handler

Types

type Handler

type Handler = func(w http.ResponseWriter, req *http.Request, p Params)

Handler is what handler looks like, duh? *note* `p` is NOT safe to be used outside the handler, call p.Copy() if you need to use it.

type OnRequestDone

type OnRequestDone = func(ctx context.Context, group, method, uri string, duration time.Duration)

type Options

type Options struct {
	OnRequestDone

	APIInfo *SwaggerInfo

	NoAutoCleanURL           bool // don't automatically clean URLs, not recommended
	NoDefaultPanicHandler    bool // don't use the default panic handler
	NoPanicOnInvalidAddRoute bool // don't panic on invalid routes, return an error instead
	CatchPanics              bool // don't catch panics
	NoAutoHeadToGet          bool // disable automatically handling HEAD requests
	ProfileLabels            bool
	AutoGenerateSwagger      bool
}

Options passed to the router

type PanicHandler

type PanicHandler = func(w http.ResponseWriter, req *http.Request, v any)

PanicHandler is a special handler that gets called if a panic happens

type Param

type Param struct {
	Name  string
	Value string
}

Param is a key/value pair

type Params

type Params []Param

Params handles the named params in your url, it is *NOT* safe to be used outside of your handler.

func (Params) Copy

func (p Params) Copy() Params

Copy returns a copy of p, required if you want to store it somewhere or use it outside of your handler.

func (Params) Get

func (p Params) Get(name string) string

Get returns a param by name

func (Params) GetExt

func (p Params) GetExt(name string) (val, ext string)

GetExt returns the value split at the last extension available, for example:

if :filename == "report.json", GetExt("filename") returns "report", "json"

type Route

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

func RouteFromRequest

func RouteFromRequest(r *http.Request) *Route

func (*Route) Group

func (r *Route) Group() string

func (*Route) Handler

func (r *Route) Handler() Handler

func (*Route) Path

func (r *Route) Path() string

func (*Route) WithDoc

func (n *Route) WithDoc(desc string, genParams bool) *SwaggerRoute

type Router

type Router struct {
	NotFoundHandler         Handler
	MethodNotAllowedHandler Handler
	PanicHandler            PanicHandler
	// contains filtered or unexported fields
}

Router is an efficient routing library

func New

func New(opts *Options) *Router

New returns a new Router

func (*Router) AddRoute

func (r *Router) AddRoute(group, method, route string, h Handler) *Route

AddRoute adds a Handler to the specific method and route. Calling AddRoute after starting the http server is racy and not supported.

func (*Router) AddRouteWithDesc

func (r *Router) AddRouteWithDesc(group, method, route string, h Handler, desc string) *Route

AddRoute adds a Handler to the specific method and route. Calling AddRoute after starting the http server is racy and not supported.

func (*Router) DisableRoute

func (r *Router) DisableRoute(method, path string, disabled bool) bool

func (*Router) GetRoutes

func (r *Router) GetRoutes() [][3]string

func (*Router) Match

func (r *Router) Match(method, path string) (rn *Route, params Params)

Match matches a method and path to a handler. if METHOD == HEAD and there isn't a specific handler for it, it returns the GET handler for the path.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler

func (*Router) Swagger

func (r *Router) Swagger() *Swagger

type Swagger

type Swagger struct {
	OpenAPI     string                        `json:"openapi,omitempty" yaml:"openapi,omitempty"`
	Server      []SwaggerServer               `json:"server,omitempty" yaml:"server,omitempty"`
	Info        *SwaggerInfo                  `json:"info,omitempty" yaml:"info,omitempty"`
	Paths       SwaggerPath                   `json:"paths,omitempty" yaml:"paths,omitempty"`
	Definitions map[string]*SwaggerDefinition `json:"definitions,omitempty" yaml:"definitions,omitempty"`
}

type SwaggerDefinition

type SwaggerDefinition struct {
	Type       string   `json:"type,omitempty"`
	Required   []string `json:"required,omitempty"`
	Properties any      `json:"properties,omitempty"`
	Items      any      `json:"items,omitempty"`
}

type SwaggerDefinitionField

type SwaggerDefinitionField struct {
	Example any    `json:"example,omitempty"`
	Type    string `json:"type,omitempty"`
}

type SwaggerDesc

type SwaggerDesc struct {
	Summary       string `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description   string `json:"description,omitempty" yaml:"description,omitempty"`
	Value         any    `json:"value,omitempty" yaml:"value,omitempty"`
	ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
}

type SwaggerInfo

type SwaggerInfo struct {
	Title       string `json:"title,omitempty" yaml:"title,omitempty"`
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
	Version     string `json:"version,omitempty" yaml:"version,omitempty"`
}

type SwaggerParam

type SwaggerParam struct {
	Name            string             `json:"name,omitempty" yaml:"name,omitempty"`
	In              string             `json:"in,omitempty" yaml:"in,omitempty"`
	Description     string             `json:"description,omitempty" yaml:"description,omitempty"`
	Type            string             `json:"-" yaml:"-"`
	Schema          *SwaggerDefinition `json:"schema,omitempty" yaml:"schema,omitempty"`
	Required        bool               `json:"required,omitempty" yaml:"required,omitempty"`
	Deprecated      bool               `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	AllowEmptyValue bool               `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"`
}

type SwaggerPath

type SwaggerPath = map[string]map[string]*SwaggerRoute

....................path.......method

type SwaggerRequestBody

type SwaggerRequestBody struct {
	Description string                                `json:"description,omitempty" yaml:"description,omitempty"`
	Content     map[string]*SwaggerRequestBodyContent `json:"content,omitempty" yaml:"value,omitempty"`
	Required    bool                                  `json:"required,omitempty" yaml:"required,omitempty"`
}

type SwaggerRequestBodyContent

type SwaggerRequestBodyContent struct {
	Example  string                  `json:"example,omitempty" yaml:"example,omitempty"`
	Examples map[string]*SwaggerDesc `json:"examples,omitempty" yaml:"examples,omitempty"`
}

type SwaggerRoute

type SwaggerRoute struct {
	OperationID string          `json:"operationID,omitempty" yaml:"operationID,omitempty"`
	Summary     string          `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description string          `json:"description,omitempty" yaml:"description,omitempty"`
	Tags        []string        `json:"tags,omitempty" yaml:"tags,omitempty"`
	Parameters  []*SwaggerParam `json:"parameters,omitempty" yaml:"parameters,omitempty"`

	RequestBody *SwaggerRequestBody     `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
	Responses   map[string]*SwaggerDesc `json:"responses,omitempty" yaml:"responses,omitempty"`
	Examples    map[string]*SwaggerDesc `json:"examples,omitempty" yaml:"examples,omitempty"`
	Public      bool                    `json:"public,omitempty" yaml:"public,omitempty"`
}

func (*SwaggerRoute) AsPublic

func (sr *SwaggerRoute) AsPublic() *SwaggerRoute

AsPublic designates this route as public documentation.

func (*SwaggerRoute) WithBody

func (sr *SwaggerRoute) WithBody(contentType string, example any) *SwaggerRoute

func (*SwaggerRoute) WithDescription

func (sr *SwaggerRoute) WithDescription(v string) *SwaggerRoute

func (*SwaggerRoute) WithExample

func (sr *SwaggerRoute) WithExample(name string, ex *SwaggerDesc) *SwaggerRoute

func (*SwaggerRoute) WithOperationID

func (sr *SwaggerRoute) WithOperationID(v string) *SwaggerRoute

func (*SwaggerRoute) WithParam

func (sr *SwaggerRoute) WithParam(name, desc, in, typ string, required bool, schema *SwaggerDefinition) *SwaggerRoute

func (*SwaggerRoute) WithParams

func (sr *SwaggerRoute) WithParams(params []*SwaggerParam) *SwaggerRoute

func (*SwaggerRoute) WithResponse

func (sr *SwaggerRoute) WithResponse(name string, ex *SwaggerDesc) *SwaggerRoute

func (*SwaggerRoute) WithSummary

func (sr *SwaggerRoute) WithSummary(v string) *SwaggerRoute

func (*SwaggerRoute) WithTags

func (sr *SwaggerRoute) WithTags(v ...string) *SwaggerRoute

type SwaggerServer

type SwaggerServer struct {
	URL         string `json:"url,omitempty" yaml:"url,omitempty"`
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
}

Jump to

Keyboard shortcuts

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