web

package
v1.21.8 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 12 Imported by: 24

Documentation

Index

Constants

View Source
const MockAfterMiddlewares = "MockAfterMiddlewares"

MockAfterMiddlewares is a general mock point, it's between middlewares and the handler

Variables

This section is empty.

Functions

func Bind

func Bind[T any](_ T) http.HandlerFunc

Bind binding an obj to a handler's context data

func GetForm

func GetForm(dataStore middleware.ContextDataStore) any

GetForm returns the validate form information

func RegisterResponseStatusProvider added in v1.21.0

func RegisterResponseStatusProvider[T any](fn func(req *http.Request) types.ResponseStatusProvider)

func RouteMock added in v1.21.0

func RouteMock(pointName string, h any)

RouteMock uses the registered mock point to mock the route execution, example:

defer web.RouteMockReset()
web.RouteMock(web.MockAfterMiddlewares, func(ctx *context.Context) {
	ctx.WriteResponse(...)
}

Then the mock function will be executed as a middleware at the mock point. It only takes effect in testing mode (setting.IsInTesting == true).

func RouteMockPoint added in v1.21.0

func RouteMockPoint(pointName string) func(next http.Handler) http.Handler

RouteMockPoint registers a mock point as a middleware for testing, example:

r.Use(web.RouteMockPoint("my-mock-point-1"))
r.Get("/foo", middleware2, web.RouteMockPoint("my-mock-point-2"), middleware2, handler)

Then use web.RouteMock to mock the route execution. It only takes effect in testing mode (setting.IsInTesting == true).

func RouteMockReset added in v1.21.0

func RouteMockReset()

RouteMockReset resets all mock points (no mock anymore)

func SetForm

func SetForm(dataStore middleware.ContextDataStore, obj any)

SetForm set the form object

Types

type Combo

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

Combo represents a tiny group routes with same pattern

func (*Combo) Delete

func (c *Combo) Delete(h ...any) *Combo

Delete delegates Delete method

func (*Combo) Get

func (c *Combo) Get(h ...any) *Combo

Get delegates Get method

func (*Combo) Patch

func (c *Combo) Patch(h ...any) *Combo

Patch delegates Patch method

func (*Combo) Post

func (c *Combo) Post(h ...any) *Combo

Post delegates Post method

func (*Combo) Put

func (c *Combo) Put(h ...any) *Combo

Put delegates Put method

type Route

type Route struct {
	R chi.Router
	// contains filtered or unexported fields
}

Route defines a route based on chi's router

func NewRoute

func NewRoute() *Route

NewRoute creates a new route

func (*Route) Any

func (r *Route) Any(pattern string, h ...any)

Any delegate requests for all methods

func (*Route) Combo

func (r *Route) Combo(pattern string, h ...any) *Combo

Combo delegates requests to Combo

func (*Route) Delete

func (r *Route) Delete(pattern string, h ...any)

Delete delegate delete method

func (*Route) Get

func (r *Route) Get(pattern string, h ...any)

Get delegate get method

func (*Route) Group

func (r *Route) Group(pattern string, fn func(), middlewares ...any)

Group mounts a sub-Router along a `pattern` string.

func (*Route) Head

func (r *Route) Head(pattern string, h ...any)

Head delegate head method

func (*Route) Methods added in v1.20.0

func (r *Route) Methods(methods, pattern string, h ...any)

Methods adds the same handlers for multiple http "methods" (separated by ","). If any method is invalid, the lower level router will panic.

func (*Route) Mount

func (r *Route) Mount(pattern string, subR *Route)

Mount attaches another Route along ./pattern/*

func (*Route) NotFound

func (r *Route) NotFound(h http.HandlerFunc)

NotFound defines a handler to respond whenever a route could not be found.

func (*Route) Patch

func (r *Route) Patch(pattern string, h ...any)

Patch delegate patch method

func (*Route) Post

func (r *Route) Post(pattern string, h ...any)

Post delegate post method

func (*Route) Put

func (r *Route) Put(pattern string, h ...any)

Put delegate put method

func (*Route) ServeHTTP

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

ServeHTTP implements http.Handler

func (*Route) Use

func (r *Route) Use(middlewares ...any)

Use supports two middlewares

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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