rox

package
v0.3.37 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const NoPrintStackBodyString = "500 Internal Server Error"

Variables

View Source
var (
	ErrMethodMismatch = errors.New("method is not allowed")

	ErrNotFound = errors.New("no matching route was found")
)
View Source
var ErrHandler = CreateMiddleWare(func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler) {
	rec := NewRecovery()
	rec.ServeHTTP(w.(ResponseWriter), r, next)
})
View Source
var FormParser = CreateMiddleWare(func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler) {
	err := r.ParseForm()
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
	}
	err = r.ParseMultipartForm(1024)
	if err != nil && err != http.ErrNotMultipart {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
	}
	if next != nil {
		next.ServeHTTP(w, r)
	}
})
View Source
var Pretty = true

Pretty response formatting.

View Source
var RequestLogger = CreateMiddleWare(func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler) {
	start := time.Now()
	log.Info("->"+r.Method, zap.String("path", r.Host+r.URL.Path), zap.Any("form", r.Form), zap.Int64("size",
		r.ContentLength), zap.String("ip", r.RemoteAddr), zap.String("time", util.FormatTimeToString(start)))
	next.ServeHTTP(w, r)
	resp := w.(ResponseWriter)
	log.Info("<-"+r.Method, zap.String("path", r.Host+r.URL.Path), zap.Int("status", resp.Status()), zap.Any("resp", resp.GetResponse()), zap.Int("size", resp.Size()), zap.Int64("ms", time.Since(start).Milliseconds()))
})
View Source
var SkipRouter = errors.New("skip this router")

Functions

func Accepted

func Accepted(w http.ResponseWriter, msg ...interface{})

func AddContext

func AddContext(this *http.Request, k interface{}, v interface{}) *http.Request

func BadGateway

func BadGateway(w http.ResponseWriter, msg ...interface{})

func BadRequest

func BadRequest(w http.ResponseWriter, msg ...interface{})

func Bytes added in v0.2.11

func Bytes(w ResponseWriter, fileName string, data []byte, code ...int)

bytes response with optional status code.

func Conflict

func Conflict(w http.ResponseWriter, msg ...interface{})

func Continue

func Continue(w http.ResponseWriter, msg ...interface{})

func CreateMiddleWare

func CreateMiddleWare(f func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler)) func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler)

func Created

func Created(w http.ResponseWriter, msg ...interface{})

func Error

func Error(w http.ResponseWriter, code int)

Error responds with a generic status code response.

func ExpectationFailed

func ExpectationFailed(w http.ResponseWriter, msg ...interface{})

func Forbidden

func Forbidden(w http.ResponseWriter, msg ...interface{})

func Found

func Found(w http.ResponseWriter, msg ...interface{})

func GatewayTimeout

func GatewayTimeout(w http.ResponseWriter, msg ...interface{})

func Gone

func Gone(w http.ResponseWriter, msg ...interface{})

func HTTPVersionNotSupported

func HTTPVersionNotSupported(w http.ResponseWriter, msg ...interface{})

func InternalServerError

func InternalServerError(w http.ResponseWriter, msg ...interface{})

func JSON

func JSON(w ResponseWriter, val interface{}, code ...int)

JSON response with optional status code.

func LengthRequired

func LengthRequired(w http.ResponseWriter, msg ...interface{})

func MethodNotAllowed

func MethodNotAllowed(w http.ResponseWriter, msg ...interface{})

func MovedPermanently

func MovedPermanently(w http.ResponseWriter, msg ...interface{})

func MultipleChoices

func MultipleChoices(w http.ResponseWriter, msg ...interface{})

func NoContent

func NoContent(w http.ResponseWriter, msg ...interface{})

func NonAuthoritativeInfo

func NonAuthoritativeInfo(w http.ResponseWriter, msg ...interface{})

func NotAcceptable

func NotAcceptable(w http.ResponseWriter, msg ...interface{})

func NotFound

func NotFound(w http.ResponseWriter, msg ...interface{})

func NotImplemented

func NotImplemented(w http.ResponseWriter, msg ...interface{})

func NotModified

func NotModified(w http.ResponseWriter, msg ...interface{})

func OK

func OK(w http.ResponseWriter, msg ...interface{})

func PartialContent

func PartialContent(w http.ResponseWriter, msg ...interface{})

func PaymentRequired

func PaymentRequired(w http.ResponseWriter, msg ...interface{})

func PreconditionFailed

func PreconditionFailed(w http.ResponseWriter, msg ...interface{})

func ProxyAuthRequired

func ProxyAuthRequired(w http.ResponseWriter, msg ...interface{})

func RequestEntityTooLarge

func RequestEntityTooLarge(w http.ResponseWriter, msg ...interface{})

func RequestTimeout

func RequestTimeout(w http.ResponseWriter, msg ...interface{})

func RequestURITooLong

func RequestURITooLong(w http.ResponseWriter, msg ...interface{})

func RequestedRangeNotSatisfiable

func RequestedRangeNotSatisfiable(w http.ResponseWriter, msg ...interface{})

func ResetContent

func ResetContent(w http.ResponseWriter, msg ...interface{})

func SeeOther

func SeeOther(w http.ResponseWriter, msg ...interface{})

func ServiceUnavailable

func ServiceUnavailable(w http.ResponseWriter, msg ...interface{})

func SwitchingProtocols

func SwitchingProtocols(w http.ResponseWriter, msg ...interface{})

func Teapot

func Teapot(w http.ResponseWriter, msg ...interface{})

func TemporaryRedirect

func TemporaryRedirect(w http.ResponseWriter, msg ...interface{})

func Unauthorized

func Unauthorized(w http.ResponseWriter, msg ...interface{})

func UnsupportedMediaType

func UnsupportedMediaType(w http.ResponseWriter, msg ...interface{})

func UseProxy

func UseProxy(w http.ResponseWriter, msg ...interface{})

func Vars

func Vars(this *http.Request) map[string]string

func XML

func XML(w ResponseWriter, val interface{}, code ...int)

Types

type BuildVarsFunc

type BuildVarsFunc func(map[string]string) map[string]string

type Cors

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

func CorsAllowAll

func CorsAllowAll() *Cors

func NewCors

func NewCors(options CorsOptions) *Cors

func (*Cors) MiddleWare

func (this *Cors) MiddleWare(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler)

type CorsOptions

type CorsOptions struct {
	AllowedOrigins []string

	AllowOriginFunc func(r *http.Request, origin string) bool

	AllowedMethods []string

	AllowedHeaders []string

	ExposedHeaders []string

	AllowCredentials bool

	MaxAge int

	OptionsPassthrough bool

	Debug bool
}

type Handler

type Handler func(w ResponseWriter, r *http.Request, a lokas.IProcess)

func CreateHandler

func CreateHandler(h Handler) Handler

type MatcherFunc

type MatcherFunc func(*http.Request, *RouteMatch) bool

func (MatcherFunc) Match

func (m MatcherFunc) Match(this *http.Request, match *RouteMatch) bool

type MiddleWare

type MiddleWare func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler)

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc is a function which receives an http.Handler and returns another http.Handlethis. Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc.

func CORSMethodMiddleware

func CORSMethodMiddleware(this *Router) MiddlewareFunc

CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header on requests for routes that have an OPTIONS method matcher to all the method matchers on the route. Routes that do not explicitly handle OPTIONS requests will not be processed by the middleware. See examples for usage.

func InitMiddleWare

func InitMiddleWare(f func(w ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler), a lokas.IProcess) MiddlewareFunc

func (MiddlewareFunc) Middleware

func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler

Middleware allows MiddlewareFunc to implement the middleware interface.

type MiddlewareMatcher

type MiddlewareMatcher func(req *http.Request) bool

type PanicFormatter

type PanicFormatter interface {
	FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *PanicInformation)
}

type PanicInformation

type PanicInformation struct {
	RecoveredPanic interface{}
	Stack          []byte
	Request        *http.Request
}

func (*PanicInformation) RequestDescription

func (p *PanicInformation) RequestDescription() string

func (*PanicInformation) StackAsString

func (p *PanicInformation) StackAsString() string

type Recovery

type Recovery struct {
	PrintStack       bool
	LogStack         bool
	PanicHandlerFunc func(*PanicInformation)
	StackAll         bool
	StackSize        int
	Formatter        PanicFormatter
}

func NewRecovery

func NewRecovery() *Recovery

NewRecovery returns a new instance of Recovery

func (*Recovery) ServeHTTP

func (this *Recovery) ServeHTTP(writer ResponseWriter, request *http.Request, next http.Handler)

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher

	Status() int

	Written() bool

	Size() int

	Before(func(ResponseWriter))

	GetResponse() map[string]interface{}

	Text() string

	SetText(msg string)

	SetMap(m map[string]interface{})

	AddContent(k string, v interface{})

	AddData(k string, v interface{})

	SetData(v interface{})

	AddContext(k string, v interface{})

	GetContext(k string) interface{}

	WriteContent() error

	Response(code int, msg ...interface{})

	OK(msg ...interface{})

	Failed(code protocol.ErrCode)

	JSON(val interface{}, code ...int)

	XML(val interface{}, code ...int)
}

func NewResponseWriter

func NewResponseWriter(this http.ResponseWriter) ResponseWriter

type Route

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

func CurrentRoute

func CurrentRoute(this *http.Request) *Route

func (*Route) BuildOnly

func (this *Route) BuildOnly() *Route

func (*Route) BuildVarsFunc

func (this *Route) BuildVarsFunc(f BuildVarsFunc) *Route

func (*Route) GetError

func (this *Route) GetError() error

func (*Route) GetHandler

func (this *Route) GetHandler() http.Handler

func (*Route) GetHostTemplate

func (this *Route) GetHostTemplate() (string, error)

func (*Route) GetMethods

func (this *Route) GetMethods() ([]string, error)

func (*Route) GetName

func (this *Route) GetName() string

func (*Route) GetPathRegexp

func (this *Route) GetPathRegexp() (string, error)

func (*Route) GetPathTemplate

func (this *Route) GetPathTemplate() (string, error)

func (*Route) GetQueriesRegexp

func (this *Route) GetQueriesRegexp() ([]string, error)

func (*Route) GetQueriesTemplates

func (this *Route) GetQueriesTemplates() ([]string, error)

func (*Route) Handler

func (this *Route) Handler(handler http.Handler) *Route

func (*Route) HandlerFunc

func (this *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route

func (*Route) Headers

func (this *Route) Headers(pairs ...string) *Route

func (*Route) HeadersRegexp

func (this *Route) HeadersRegexp(pairs ...string) *Route

func (*Route) Host

func (this *Route) Host(tpl string) *Route

func (*Route) Match

func (this *Route) Match(req *http.Request, match *RouteMatch) bool

func (*Route) MatcherFunc

func (this *Route) MatcherFunc(f MatcherFunc) *Route

func (*Route) Methods

func (this *Route) Methods(methods ...string) *Route

func (*Route) Name

func (this *Route) Name(name string) *Route

func (*Route) Path

func (this *Route) Path(tpl string) *Route

func (*Route) PathPrefix

func (this *Route) PathPrefix(tpl string) *Route

func (*Route) Queries

func (this *Route) Queries(pairs ...string) *Route

func (*Route) Schemes

func (this *Route) Schemes(schemes ...string) *Route

func (*Route) SkipClean

func (this *Route) SkipClean() bool

func (*Route) Subrouter

func (this *Route) Subrouter() *Router

func (*Route) URL

func (this *Route) URL(pairs ...string) (*url.URL, error)

func (*Route) URLHost

func (this *Route) URLHost(pairs ...string) (*url.URL, error)

func (*Route) URLPath

func (this *Route) URLPath(pairs ...string) (*url.URL, error)

type RouteMatch

type RouteMatch struct {
	Route   *Route
	Handler http.Handler
	Vars    map[string]string

	MatchErr error
}

type Router

type Router struct {
	NotFoundHandler http.Handler

	MethodNotAllowedHandler http.Handler

	KeepContext bool
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) BuildVarsFunc

func (this *Router) BuildVarsFunc(f BuildVarsFunc) *Route

func (*Router) Get

func (this *Router) Get(name string) *Route

func (*Router) GetRoute

func (this *Router) GetRoute(name string) *Route

func (*Router) Handle

func (this *Router) Handle(path string, handler http.Handler) *Route

func (*Router) HandleFunc

func (this *Router) HandleFunc(path string, f func(http.ResponseWriter,
	*http.Request)) *Route

func (*Router) Headers

func (this *Router) Headers(pairs ...string) *Route

func (*Router) Host

func (this *Router) Host(tpl string) *Route

func (*Router) Match

func (this *Router) Match(req *http.Request, match *RouteMatch) bool

func (*Router) MatcherFunc

func (this *Router) MatcherFunc(f MatcherFunc) *Route

func (*Router) Methods

func (this *Router) Methods(methods ...string) *Route

func (*Router) Name

func (this *Router) Name(name string) *Route

func (*Router) NewRoute

func (this *Router) NewRoute() *Route

func (*Router) Path

func (this *Router) Path(tpl string) *Route

func (*Router) PathExcept

func (this *Router) PathExcept(p string, mwf ...MiddlewareFunc)

func (*Router) PathExcepts

func (this *Router) PathExcepts(p []string, mwf ...MiddlewareFunc)

func (*Router) PathIn

func (this *Router) PathIn(p []string, mwf ...MiddlewareFunc)

func (*Router) PathOnly

func (this *Router) PathOnly(p string, mwf ...MiddlewareFunc)

func (*Router) PathPrefix

func (this *Router) PathPrefix(tpl string) *Route

func (*Router) Queries

func (this *Router) Queries(pairs ...string) *Route

func (*Router) Schemes

func (this *Router) Schemes(schemes ...string) *Route

func (*Router) ServeHTTP

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

func (*Router) SkipClean

func (this *Router) SkipClean(value bool) *Router

func (*Router) StrictSlash

func (this *Router) StrictSlash(value bool) *Router

func (*Router) Use

func (this *Router) Use(mwf ...MiddlewareFunc)

Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Routethis.

func (*Router) UseEncodedPath

func (this *Router) UseEncodedPath() *Router

func (*Router) Walk

func (this *Router) Walk(walkFn WalkFunc) error

func (*Router) When

func (this *Router) When(match MiddlewareMatcher, mwf ...MiddlewareFunc)

type Static

type Static struct {
	Dir http.FileSystem

	Prefix string

	IndexFile string
}

func NewStatic

func NewStatic(directory http.FileSystem) *Static

func (*Static) MiddleWare

func (this *Static) MiddleWare(rw ResponseWriter, r *http.Request, a lokas.IProcess, next http.Handler)

type TextPanicFormatter

type TextPanicFormatter struct{}

func (*TextPanicFormatter) FormatPanicError

func (t *TextPanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *PanicInformation)

type WalkFunc

type WalkFunc func(route *Route, routethis *Router, ancestors []*Route) error

Jump to

Keyboard shortcuts

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