routerfx

package
v0.0.0-...-3b96a7a Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeApplicationJSON = "application/json"
	ContentTypeTextPlain       = "text/plain"
	ContentTypeFormURLEncoded  = "application/x-www-form-urlencoded"
	ContentTypeMultipart       = "multipart/form-data"

	ContentTypeApplicationJSONUTF8 = "application/json; charset=utf-8"
	ContentTypeTextPlainUTF8       = "text/plain; charset=utf-8"
)

Variables

View Source
var Module = fx.Module(
	"winterfx_core_routefx",
	fx.Provide(
		New,
		flagfx.AsDecoderFunc(DecodeParams),
	),
)

Functions

func AsRouteProvider

func AsRouteProvider[T any](fn func(v T) (pattern string, h HandlerFunc)) any

func Bind

func Bind[T any](c Context) (o T)

Bind a generic version of [Context.Bind]

example:

	func actionValidate(c summer.Context) {
		args := summer.Bind[struct {
       		Tenant       string `json:"header_x_tenant"`
			Username     string `json:"username"`
			Age 		 int    `json:"age,string"`
		}](c)
        _ = args.Tenant
        _ = args.Username
        _ = args.Age
	}

Types

type Context

type Context interface {
	// Context extend the [context.Context] interface by proxying to [http.Request.Context]
	context.Context

	// Inject inject underlying [context.Context]
	Inject(fn func(ctx context.Context) context.Context)

	// Req returns the underlying *http.Request
	Req() *http.Request

	// Header returns the headers of underlying [http.ResponseWriter]
	Header() http.Header

	// Bind unmarshal the request data into any struct with json tags
	//
	// HTTP header is prefixed with "header_"
	//
	// HTTP query is prefixed with "query_"
	//
	// both JSON and Form are supported
	Bind(data interface{})

	// Files returns the multipart file headers
	Files() map[string][]*multipart.FileHeader

	// Code set the response code, can be called multiple times
	Code(code int)

	// Body set the response body with content type, can be called multiple times
	Body(contentType string, buf []byte)

	// Text set the response body to plain text
	Text(s string)

	// JSON set the response body to json
	JSON(data interface{})

	// Perform actually perform the response
	// it is suggested to use in defer, recover() is included to recover from any panics
	Perform()
}

Context context of an incoming request and corresponding response writer

func NewContext

func NewContext(rw http.ResponseWriter, req *http.Request) Context

type HandlerFunc

type HandlerFunc func(c Context)

HandlerFunc handler func with Context as argument

type Options

type Options struct {
	fx.In

	*Params

	Routes []Route `group:"winterfx_core_routerfx_routes"`
}

type Params

type Params struct {
	LoggingResponse bool
	LoggingRequest  bool
	Concurrency     int
}

func DecodeParams

func DecodeParams(fset *flag.FlagSet) *Params

type Route

type Route struct {
	Pattern string
	HandlerFunc
}

Route a route with pattern and handler

func (Route) ToHTTPHandler

func (r Route) ToHTTPHandler(opts RouteOptions) http.Handler

type RouteOptions

type RouteOptions struct {
	LoggingRequest  bool
	LoggingResponse bool
}

type Router

type Router interface {
	http.Handler
}

func New

func New(opts Options) Router

Jump to

Keyboard shortcuts

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