web

package
v0.0.85-test Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package inject provides utilities for mapping and injecting dependencies in various ways.

Package macaron is a high productive and modular web framework in Go.

Index

Constants

View Source
const (
	DEV  = "development"
	PROD = "production"
)

Variables

View Source
var (
	// Env is the environment that Macaron is executing in.
	// The MACARON_ENV is read on initialization to set this variable.
	Env = DEV
)
View Source
var MaxMemory = int64(1024 * 1024 * 10)

MaxMemory is the maximum amount of memory to use when parsing a multipart form. Set this to whatever value you prefer; default is 10 MB.

Functions

func Bind

func Bind(req *http.Request, v interface{}) error

Bind deserializes JSON payload from the request

func InterfaceOf

func InterfaceOf(value interface{}) reflect.Type

InterfaceOf dereferences a pointer to an Interface type. It panics if value is not an pointer to an interface.

func IsFastInvoker

func IsFastInvoker(h interface{}) bool

IsFastInvoker check interface is FastInvoker

func MatchTest

func MatchTest(pattern, url string) bool

MatchTest returns true if given URL is matched by given pattern.

func Params

func Params(r *http.Request) map[string]string

Params returns the named route parameters for the current request, if any.

func Renderer

func Renderer(dir, leftDelim, rightDelim string) func(http.Handler) http.Handler

Renderer is a Middleware that injects a template renderer into the macaron context, enabling ctx.HTML calls in the handlers. If MACARON_ENV is set to "development" then templates will be recompiled on every request. For more performance, set the MACARON_ENV environment variable to "production".

func SetURLParams

func SetURLParams(r *http.Request, vars map[string]string) *http.Request

SetURLParams sets the named URL parameters for the given request. This should only be used for testing purposes.

func Version

func Version() string

Types

type BeforeFunc

type BeforeFunc func(ResponseWriter)

BeforeFunc is a function that is called before the ResponseWriter has been written to.

type BeforeHandler

type BeforeHandler func(rw http.ResponseWriter, req *http.Request) bool

BeforeHandler represents a handler executes at beginning of every request. Macaron stops future process when it returns true.

type Context

type Context struct {
	Injector

	*Router
	Req  *http.Request
	Resp ResponseWriter
	// contains filtered or unexported fields
}

Context represents the runtime context of current request of Macaron instance. It is the integration of most frequently used middlewares and helper methods.

func FromContext

func FromContext(c context.Context) *Context

FromContext returns the macaron context stored in a context.Context, if any.

func (*Context) GetCookie

func (ctx *Context) GetCookie(name string) string

GetCookie returns given cookie value from request header.

func (*Context) HTML

func (ctx *Context) HTML(status int, name string, data interface{})

HTML renders the HTML with default template set.

func (*Context) JSON

func (ctx *Context) JSON(status int, data interface{})

func (*Context) Next

func (ctx *Context) Next()

Next runs the next handler in the context chain

func (*Context) Query

func (ctx *Context) Query(name string) string

Query querys form parameter.

func (*Context) QueryBool

func (ctx *Context) QueryBool(name string) bool

QueryBool returns query result in bool type.

func (*Context) QueryInt

func (ctx *Context) QueryInt(name string) int

QueryInt returns query result in int type.

func (*Context) QueryInt64

func (ctx *Context) QueryInt64(name string) int64

QueryInt64 returns query result in int64 type.

func (*Context) QueryStrings

func (ctx *Context) QueryStrings(name string) []string

QueryStrings returns a list of results by given query name.

func (*Context) Redirect

func (ctx *Context) Redirect(location string, status ...int)

Redirect sends a redirect response

func (*Context) RemoteAddr

func (ctx *Context) RemoteAddr() string

RemoteAddr returns more real IP address.

type ContextInvoker

type ContextInvoker func(ctx *Context)

ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).

func (ContextInvoker) Invoke

func (invoke ContextInvoker) Invoke(params []interface{}) ([]reflect.Value, error)

Invoke implements inject.FastInvoker which simplifies calls of `func(ctx *Context)` function.

type FastInvoker

type FastInvoker interface {
	// Invoke attempts to call the ordinary functions. If f is a function
	// with the appropriate signature, f.Invoke([]interface{}) is a Call that calls f.
	// Returns a slice of reflect.Value representing the returned values of the function.
	// Returns an error if the injection fails.
	Invoke([]interface{}) ([]reflect.Value, error)
}

FastInvoker represents an interface in order to avoid the calling function via reflection.

example:

type handlerFuncHandler func(http.ResponseWriter, *http.Request) error
func (f handlerFuncHandler)Invoke([]interface{}) ([]reflect.Value, error){
	ret := f(p[0].(http.ResponseWriter), p[1].(*http.Request))
	return []reflect.Value{reflect.ValueOf(ret)}, nil
}

type funcHandler func(int, string)
func (f funcHandler)Invoke([]interface{}) ([]reflect.Value, error){
	f(p[0].(int), p[1].(string))
	return nil, nil
}

type Handle

type Handle func(http.ResponseWriter, *http.Request, map[string]string)

Handle is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but has a third parameter for the values of wildcards (variables).

type Handler

type Handler interface{}

Handler can be any callable function. Macaron attempts to inject services into the handler's argument list, and panics if an argument could not be fulfilled via dependency injection.

type Injector

type Injector interface {
	Invoker
	TypeMapper
}

Injector represents an interface for mapping and injecting dependencies into structs and function arguments.

func NewInjector

func NewInjector() Injector

New returns a new Injector.

type Invoker

type Invoker interface {
	// Invoke attempts to call the interface{} provided as a function,
	// providing dependencies for function arguments based on Type. Returns
	// a slice of reflect.Value representing the returned values of the function.
	// Returns an error if the injection fails.
	Invoke(interface{}) ([]reflect.Value, error)
}

Invoker represents an interface for calling functions via reflection.

type Leaf

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

Leaf represents a leaf route information.

func NewLeaf

func NewLeaf(parent *Tree, pattern string, handle Handle) *Leaf

func (*Leaf) URLPath

func (l *Leaf) URLPath(pairs ...string) string

URLPath build path part of URL by given pair values.

type Macaron

type Macaron struct {
	*Router
	// contains filtered or unexported fields
}

Macaron represents the top level web application. Injector methods can be invoked to map services on a global level.

func New

func New() *Macaron

New creates a bare bones Macaron instance. Use this method if you want to have full control over the middleware that is used.

func (*Macaron) ServeHTTP

func (m *Macaron) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP is the HTTP Entry point for a Macaron instance. Useful if you want to control your own HTTP server. Be aware that none of middleware will run without registering any router.

func (*Macaron) SetURLPrefix

func (m *Macaron) SetURLPrefix(prefix string)

SetURLPrefix sets URL prefix of router layer, so that it support suburl.

func (*Macaron) Use

func (m *Macaron) Use(handler Handler)

Use adds a middleware Handler to the stack, and panics if the handler is not a callable func. Middleware Handlers are invoked in the order that they are added.

func (*Macaron) UseMiddleware

func (m *Macaron) UseMiddleware(middleware func(http.Handler) http.Handler)

UseMiddleware is a traditional approach to writing middleware in Go. A middleware is a function that has a reference to the next handler in the chain and returns the actual middleware handler, that may do its job and optionally call next. Due to how Macaron handles/injects requests and responses we patch the web.Context to use the new ResponseWriter and http.Request here. The caller may only call `next.ServeHTTP(rw, req)` to pass a modified response writer and/or a request to the further middlewares in the chain.

type Mux

type Mux = Macaron

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has not been written.
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(BeforeFunc)
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(method string, rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type Router

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

Router represents a Macaron router layer.

func NewRouter

func NewRouter() *Router

func (*Router) Any

func (r *Router) Any(pattern string, h ...Handler)

Any is a shortcut for r.Handle("*", pattern, handlers)

func (*Router) Delete

func (r *Router) Delete(pattern string, h ...Handler)

Delete is a shortcut for r.Handle("DELETE", pattern, handlers)

func (*Router) Get

func (r *Router) Get(pattern string, h ...Handler)

Get is a shortcut for r.Handle("GET", pattern, handlers)

func (*Router) Group

func (r *Router) Group(pattern string, fn func(), h ...Handler)

func (*Router) Handle

func (r *Router) Handle(method string, pattern string, handlers []Handler)

Handle registers a new request handle with the given pattern, method and handlers.

func (*Router) Head

func (r *Router) Head(pattern string, h ...Handler)

Head is a shortcut for r.Handle("HEAD", pattern, handlers)

func (*Router) NotFound

func (r *Router) NotFound(handlers ...Handler)

NotFound configurates http.HandlerFunc which is called when no matching route is found. If it is not set, http.NotFound is used. Be sure to set 404 response code in your handler.

func (*Router) Options

func (r *Router) Options(pattern string, h ...Handler)

Options is a shortcut for r.Handle("OPTIONS", pattern, handlers)

func (*Router) Patch

func (r *Router) Patch(pattern string, h ...Handler)

Patch is a shortcut for r.Handle("PATCH", pattern, handlers)

func (*Router) Post

func (r *Router) Post(pattern string, h ...Handler)

Post is a shortcut for r.Handle("POST", pattern, handlers)

func (*Router) Put

func (r *Router) Put(pattern string, h ...Handler)

Put is a shortcut for r.Handle("PUT", pattern, handlers)

func (*Router) ServeHTTP

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

type Tree

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

Tree represents a router tree in Macaron.

func NewSubtree

func NewSubtree(parent *Tree, pattern string) *Tree

func NewTree

func NewTree() *Tree

func (*Tree) Add

func (t *Tree) Add(pattern string, handle Handle) *Leaf

func (*Tree) Match

func (t *Tree) Match(url string) (Handle, map[string]string, bool)

type TypeMapper

type TypeMapper interface {
	// Maps the interface{} value based on its immediate type from reflect.TypeOf.
	Map(interface{}) TypeMapper
	// Maps the interface{} value based on the pointer of an Interface provided.
	// This is really only useful for mapping a value as an interface, as interfaces
	// cannot at this time be referenced directly without a pointer.
	MapTo(interface{}, interface{}) TypeMapper
	// Returns the Value that is mapped to the current type. Returns a zeroed Value if
	// the Type has not been mapped.
	GetVal(reflect.Type) reflect.Value
}

TypeMapper represents an interface for mapping interface{} values based on type.

type Validator

type Validator interface {
	Validate() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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