Help us shape the future of Go! Take the 2026 Go Developer Survey

rest

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// AccessControlAllowMethodsHeader - CORS Header that says what HTTP Methods are allowed to specific endpoint
	AccessControlAllowMethodsHeader = "Access-Control-Allow-Methods"
	AccessControlAllowOriginHeader  = "Access-Control-Allow-Origin"
	AccessControlAllowHeadersHeader = "Access-Control-Allow-Headers"
	// Value that allow all headers
	AnyOrigin            = "*"
	AllowAllHeaderValues = "*"
)

Variables

This section is empty.

Functions

func EnableCors

func EnableCors(respWriter *http.ResponseWriter, origin string, methods string)

EnableCors

This function sets CORS headers to specified origin. In the future it should take array with allowed origins (ip addresses and/or hostnames)
* Parameters:
*     - respWriter - gorilla/mux response writer
*     - origin - domain i.e. example.com or http://127.0.0.1:3000
*     - methods - allowed methods i.e. GET, POST, OPTIONS
* Returns nothing

func EnableCorsAllOrigin

func EnableCorsAllOrigin(respWriter *http.ResponseWriter)

EnableCorsAllOrigin

This function sets CORS headers to allow any origin. In the future it should take array with allowed origins (ip addresses and/or hostnames)
* Parameters:
*     - respWriter - gorilla/mux response writer
* Returns nothing

Types

type GinBasedWebApiHandler added in v1.5.0

type GinBasedWebApiHandler struct {
	AllowCors bool
	Origin    string
	Router    *g.Engine
	// contains filtered or unexported fields
}

func NewGinBasedWebApiHandler added in v1.5.0

func NewGinBasedWebApiHandler(allowCors bool, origin string, opts ...g.OptionFunc) *GinBasedWebApiHandler

func (*GinBasedWebApiHandler) DELETE added in v1.5.0

func (handler *GinBasedWebApiHandler) DELETE(routerGroup *g.RouterGroup, path string, f func(ctx *g.Context))

func (*GinBasedWebApiHandler) GET added in v1.5.0

func (handler *GinBasedWebApiHandler) GET(routerGroup *g.RouterGroup, path string, f func(ctx *g.Context))

func (*GinBasedWebApiHandler) PATCH added in v1.5.0

func (handler *GinBasedWebApiHandler) PATCH(routerGroup *g.RouterGroup, path string, f func(ctx *g.Context))

func (*GinBasedWebApiHandler) POST added in v1.5.0

func (handler *GinBasedWebApiHandler) POST(routerGroup *g.RouterGroup, path string, f func(ctx *g.Context))

func (*GinBasedWebApiHandler) PUT added in v1.5.0

func (handler *GinBasedWebApiHandler) PUT(routerGroup *g.RouterGroup, path string, f func(ctx *g.Context))

type MuxBasedWebApiHandler added in v1.5.0

type MuxBasedWebApiHandler struct {
	AllowCors bool
	Origin    string
	Router    *m.Router
	// contains filtered or unexported fields
}

MuxBasedWebApiHandler is a struct that encapsulates Router and CORS settings

func NewMuxBasedWebApiHandler added in v1.5.0

func NewMuxBasedWebApiHandler(allowCors bool, origin string) *MuxBasedWebApiHandler

NewMuxBasedWebApiHandler

This function creates instance of MuxBasedWebApiHandler and inits properties with arguments values
* Parameters:
*    - allowCors - represents should be course configured (true) or not
*    - origin - represents ip/domain or * (AnyOrigin) name which will be used response headers

func (*MuxBasedWebApiHandler) HandleFunc added in v1.5.0

func (handler *MuxBasedWebApiHandler) HandleFunc(router *m.Router, path string, f func(http.ResponseWriter, *http.Request), handlerMethods ...string) *m.Route

HandleFunc

This is a Proxy function that assign handler to handle specific route by url but also simultaneously it configures CORS handler.
* This function is almost equal to mux.Router.HandleFunc except fact that we passing
* We are working here with route names for OPTIONS handler i.e. we have REST resource /api/good and 2 separate handlers for GET and POST
* therefore for proper CORS handle we should respond on OPTIONS /api/good with empty body and header AccessControlAllowMethodsHeader with
* values OPTIONS, GET, POST. Our HandleFunc allow to reduce a complexity of router config because using our HandleFunc we take service on
* handling OPTIONS method by our HandleFunc.
* Parameters:
*     - router - router to which we assign handler func this is implemented for sub routers supports
*     - path - url of route (request)
*     - f - handler function that handles request
* Return created route like router.HandleFunc do

Jump to

Keyboard shortcuts

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