router

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BODY_UNKNOWN        = iota
	BODY_JSON           = 1
	BODY_MUTLIPART_FORM = 2
	BODY_FORM           = 3
	BODY_FILE           = 4
	BODY_XML            = 5
	DEFAULT_LOCALE      = "fr_FR"
)
View Source
const (
	TYPE_NONE               = iota
	TYPE_QUERY              = "query"
	TYPE_JSON               = "json"
	TYPE_FORM               = "form"
	TYPE_MULTI_PART         = "multipart"
	TYPE_MULTI_PART_FILE    = "multipart-file"
	TYPE_FILE               = "file"
	ERROR_INVOKE_MIDDLEWARE = "%s middleware method not found"
)
View Source
const (
	ENV_PRODUCTION           = "PROD"
	API_INTERNAL_ERROR       = "API_INTERNAL_ERROR"
	API_INVOKE_ERROR         = "API_INVOKE_ERROR"
	API_BODY_JSON_PARSE_FAIL = "API_BODY_JSON_PARSE_FAIL"
	API_BODY_XML_PARSE_FAIL  = "API_BODY_XML_PARSE_FAIL"
	API_BODY_EOF             = "API_BODY_EOF"
	API_BODY_TOO_LARGE       = "API_BODY_TOO_LARGE"
	API_BODY_FORM_EMPTY      = "API_BODY_FORM_EMPTY"
	API_BODY_FORM_DATA_EMPTY = "API_BODY_FORM_DATA_EMPTY"
	API_BODY_JSON_TYPE_FAIL  = "API_BODY_JSON_TYPE_FAIL"
	API_BODY_XML_TYPE_FAIL   = "API_BODY_XML_TYPE_FAIL"
	API_BODY_TYPE_FAIL       = "API_BODY_TYPE_FAIL"
	API_BODY_VALIDATOR_FAIL  = "API_BODY_VALIDATOR_FAIL"
	API_QUERY_VALIDATOR_FAIL = "API_QUERY_VALIDATOR_FAIL"
	API_FORM_VALIDATOR_FAIL  = "API_FORM_VALIDATOR_FAIL"
	API_EXCEEDED_TIMEOUT     = "API_EXCEEDED_TIMEOUT"
)

noinspection ALL

View Source
const NAMESPACE = "router"
View Source
const (
	// ParamsKey is the key in a request context.
	ParamsKey key = iota
)

Variables

View Source
var (

	// Error for not found.
	ErrNotFound = errors.New("no matching route was found")
	// Error for method not allowed.
	ErrMethodNotAllowed = errors.New("methods is not allowed")
	// Configurable Handler to be used when no route matches.
	NotFoundHandler http.Handler
	// Configurable Handler to be used when the request method does not match the route.
	MethodNotAllowedHandler http.Handler
)

Functions

func Get

func Get() *router

Static function

func New

func New(controller AppController) *router

func NewContext

func NewContext(ctx context.Context, header http.Header) *promise.Promise

func NewHandler

func NewHandler(route route, customRender JSONRender) *handler

func NewLoggingResponseWriter

func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter

func NewRender

func NewRender(ctx *Context, custom JSONRender) *promise.Promise

func NewTree

func NewTree() *tree

NewTree creates a new trie tree.

func SetJsonData

func SetJsonData(v interface{}, w http.ResponseWriter)

func SetStatus

func SetStatus(statusCode int, w http.ResponseWriter)

Types

type AppController

type AppController interface {
	OnCreate() (err error)
	OnFinish() (err error)
}

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

func (*Context) GetBody

func (c *Context) GetBody() *io.ReadCloser

func (*Context) GetContext

func (c *Context) GetContext() *Context

func (*Context) GetContextData

func (c *Context) GetContextData(key string) interface{}

func (*Context) GetFormValue

func (c *Context) GetFormValue(value string) string

func (*Context) GetHeader

func (c *Context) GetHeader() http.Header

func (*Context) GetJSON

func (c *Context) GetJSON() interface{}

func (*Context) GetLocale

func (c *Context) GetLocale() string

func (*Context) GetMatchPath

func (c *Context) GetMatchPath() string

func (*Context) GetMultiFormFormFile

func (c *Context) GetMultiFormFormFile(value string) *multipart.FileHeader

func (*Context) GetMultiFormFormValue

func (c *Context) GetMultiFormFormValue(value string) string

func (*Context) GetParam

func (c *Context) GetParam(name string) string

func (*Context) GetPath

func (c *Context) GetPath() string

func (*Context) GetProfils

func (c *Context) GetProfils() []string

func (*Context) GetQuery

func (c *Context) GetQuery() map[string][]string

func (*Context) GetQueryParams

func (c *Context) GetQueryParams() params

func (*Context) GetRemoteIP

func (c *Context) GetRemoteIP() string

func (*Context) GetRemoteURI

func (c *Context) GetRemoteURI() string

func (*Context) GetResponseWriter

func (c *Context) GetResponseWriter() http.ResponseWriter

func (*Context) GetURL

func (c *Context) GetURL() (url *url.URL)

func (*Context) GetUUID

func (c *Context) GetUUID() string

func (*Context) IsFinish

func (c *Context) IsFinish() bool

func (*Context) JSON

func (c *Context) JSON(statusCode int, data interface{})

func (*Context) ParseBody

func (c *Context) ParseBody(v interface{}) (err error)

func (*Context) RequireBody

func (c *Context) RequireBody(typeBody int) (err error)

func (*Context) Response

func (c *Context) Response() interface{}

func (*Context) SetContextData

func (c *Context) SetContextData(key string, data interface{})

func (*Context) SetForm

func (c *Context) SetForm(form url.Values)

func (*Context) SetHeader

func (c *Context) SetHeader(key string, value string)

func (*Context) SetJSON

func (c *Context) SetJSON(body io.ReadCloser)
func (c *Context) SetFile(body io.ReadCloser) {
	c.file = new(File)
	c.file.data = body

	c._setTypeBody(BODY_FILE)
}

func (*Context) SetLocale

func (c *Context) SetLocale(language string)

func (*Context) SetMatchPath

func (c *Context) SetMatchPath(path string)

func (*Context) SetMultiPartForm

func (c *Context) SetMultiPartForm(multipartForm *multipart.Form)

func (*Context) SetProfils

func (c *Context) SetProfils(profils []string)

func (*Context) SetQuery

func (c *Context) SetQuery(query map[string][]string, params params, path string)

func (*Context) SetRemote

func (c *Context) SetRemote(ip string, uri string)

func (*Context) SetRequestHeader

func (c *Context) SetRequestHeader(values http.Header)

func (*Context) SetURL

func (c *Context) SetURL(url *url.URL)

func (*Context) SetXML

func (c *Context) SetXML(body io.ReadCloser)

func (*Context) StatusCode

func (c *Context) StatusCode() int

type ContextLast

type ContextLast struct {
	File   string
	Line   int
	Caller *runtime.Func
}

type ErrorMsg

type ErrorMsg struct {
	Code    int    `json:"err_code"`
	Message string `json:"err_message"`
}

func SetError

func SetError(statusCode int, message string) ErrorMsg

type ErrorStackTrace

type ErrorStackTrace interface {
	Error() string
	Trace() []string
}

type HTTPError

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

func HTTPBadRequest

func HTTPBadRequest() *HTTPError

func HTTPInternalServerError

func HTTPInternalServerError() *HTTPError

func HTTPRequestTimeout

func HTTPRequestTimeout() *HTTPError

func HTTPUnauthorized

func HTTPUnauthorized() *HTTPError

func NewHTTPError

func NewHTTPError(statusCode int) *HTTPError

func (*HTTPError) Code

func (e *HTTPError) Code() string

func (*HTTPError) Details

func (e *HTTPError) Details() interface{}

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Message

func (e *HTTPError) Message() string

func (*HTTPError) OnDev

func (e *HTTPError) OnDev() bool

func (*HTTPError) SetCode

func (e *HTTPError) SetCode(code string) *HTTPError

func (*HTTPError) SetDetails

func (e *HTTPError) SetDetails(details interface{}) *HTTPError

func (*HTTPError) SetMessage

func (e *HTTPError) SetMessage(message string) *HTTPError

func (*HTTPError) SetOnDev

func (e *HTTPError) SetOnDev(private bool) *HTTPError

func (*HTTPError) SetStatusCode

func (e *HTTPError) SetStatusCode(statusCode int) *HTTPError

func (*HTTPError) SetTrace

func (e *HTTPError) SetTrace(trace string) *HTTPError

func (*HTTPError) StatusCode

func (e *HTTPError) StatusCode() int

func (*HTTPError) Trace

func (e *HTTPError) Trace() []string

type HTTPGenericError

type HTTPGenericError interface {
	Error() string
	Code() string
	StatusCode() int
	Message() string
	Details() interface{}
	Trace() []string
	OnDev() bool
}

type Handler

type Handler interface {
	Next(c *Context) (err error)
}

type HandlerFunc

type HandlerFunc func(c *Context) (err error)

func (HandlerFunc) Next

func (h HandlerFunc) Next(c *Context) (err error)

type JSONRender

type JSONRender interface {
	SetError(err error)
	SetContext(c *Context)
	SetResponseWriter(w http.ResponseWriter) *promise.Promise
	SetRequest(request *http.Request)
	GetContext() *Context
	Send() *promise.Promise
}

type Middleware

type Middleware struct {
}

func (*Middleware) Helmet

func (m *Middleware) Helmet() func(h Handler) Handler

func (*Middleware) ResponseTime

func (m *Middleware) ResponseTime(data struct {
	Render string `json:"render"`
}) func(h Handler) Handler

func (*Middleware) TimeOut

func (m *Middleware) TimeOut(data struct {
	Value time.Duration `json:"value"`
}) func(h Handler) Handler

type NextHandler

type NextHandler struct {
	N func(c *Context) (err error)
}

func (NextHandler) Next

func (n NextHandler) Next(c *Context) (err error)

type R

type R map[string]interface{}

type Render

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

func (*Render) GetContext

func (r *Render) GetContext() *Context

func (*Render) Send

func (r *Render) Send() *promise.Promise

func (*Render) SetContext

func (r *Render) SetContext(c *Context)

func (*Render) SetError

func (r *Render) SetError(err error)

func (*Render) SetRequest

func (r *Render) SetRequest(request *http.Request)

func (*Render) SetResponseWriter

func (r *Render) SetResponseWriter(w http.ResponseWriter) *promise.Promise

type Route

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

route represents the route which has data for a routing.

type Router

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

Router represents the router which handles routing.

func NewRouter

func NewRouter() *Router

NewRouter creates a new router.

func (*Router) Handle

func (r *Router) Handle()

Handle handles a route.

func (*Router) Handler

func (r *Router) Handler(path string, handler *handler)

Handler sets a handler.

func (*Router) Methods

func (r *Router) Methods(methods ...string) *Router

func (*Router) Profils

func (r *Router) Profils(scopes ...string) *Router

func (*Router) ServeHTTP

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

Jump to

Keyboard shortcuts

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