prouter

package module
v1.1.14 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: MIT Imports: 27 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DebugMode = iota
	ReleaseMode
)

Variables

View Source
var (
	SessionNotInitialized = fmt.Errorf("Session not initialized")
	SessionKeyNotExists   = fmt.Errorf("Session key not exist")
)

Functions

func CheckForJSON

func CheckForJSON(r *http.Request) error

CheckForJSON makes sure that the request's Content-Type is application/json.

func NewErr added in v1.1.5

func NewErr(code int, err error, megs ...string) *prouterError

func ReadJSON

func ReadJSON(r *http.Request, out interface{}) error

func SetMapCodeToStatusFunc added in v1.1.5

func SetMapCodeToStatusFunc(fn func(code int) (statusCode int))

func SetMode

func SetMode(value int64)

func SetResponseTmpl added in v1.0.4

func SetResponseTmpl(tmpl ResponseTmpl)

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, v interface{}) error

WriteJSON writes the value v to the http response stream as json with standard json encoding.

Types

type Context added in v1.0.9

type Context struct {
	context.Context

	Request  *http.Request
	Writer   *ResponseWriter
	Path     string
	ClientIp string
	Method   string
	// contains filtered or unexported fields
}

func (*Context) Ctx added in v1.0.9

func (c *Context) Ctx() context.Context

func (*Context) ExecuteTemplateFS added in v1.0.10

func (c *Context) ExecuteTemplateFS(fs embed.FS, resource string, data any) (Response, error)

func (*Context) Redirect added in v1.1.1

func (c *Context) Redirect(code int, location string) (Response, error)

func (*Context) Session added in v1.0.10

func (c *Context) Session() *Session

func (*Context) Value added in v1.1.11

func (c *Context) Value(key any) any

func (*Context) Var added in v1.1.4

func (c *Context) Var(key string) string

func (*Context) WithValue added in v1.1.2

func (c *Context) WithValue(key, val any)

type ContextKeyType added in v1.0.9

type ContextKeyType int
const (
	ContextRequestKey ContextKeyType = iota
	ContextKey
)

type ErrComponent added in v1.1.5

type ErrComponent string
const (
	ErrProuter  ErrComponent = "prouter"
	ErrRecovery ErrComponent = "recovery"
	ErrService  ErrComponent = "service"
	ErrRepo     ErrComponent = "repository"
	ErrLib      ErrComponent = "library"
)

type Error added in v1.1.5

type Error interface {
	error

	Code() int
	Message() string
	Cause() error
	String() string
	Component() ErrComponent
	SetComponent(c ErrComponent) Error
	ResponseErrType() ResponseErrType
	SetResponseType(r ResponseErrType) Error
}

func MsgError added in v1.1.5

func MsgError(code int, msg string) Error

func ResourceAlreadyExists added in v1.1.5

func ResourceAlreadyExists(code int, msg string) Error

func ResourceNotFound added in v1.1.5

func ResourceNotFound(code int, msg string) Error

type HandleFunc

type HandleFunc func(ctx *Context) (Response, error)

func BodyParser added in v1.1.7

func BodyParser[RequestT any, ResponseT any](fn func(*Context, *RequestT) (*ResponseT, error)) HandleFunc

func (HandleFunc) Handle added in v1.0.6

func (f HandleFunc) Handle(ctx *Context) (Response, error)

func (HandleFunc) Name added in v1.0.6

func (f HandleFunc) Name() string

func (HandleFunc) WrapHandler

func (f HandleFunc) WrapHandler(handler handlerFunc) handlerFunc

type LogMiddleware

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

func NewLogMiddleware

func NewLogMiddleware(opts ...LogOption) *LogMiddleware

func (*LogMiddleware) ClientIP

func (lm *LogMiddleware) ClientIP(r *http.Request) string

func (*LogMiddleware) RemoteIP

func (lm *LogMiddleware) RemoteIP(r *http.Request) string

func (*LogMiddleware) WrapHandler

func (lm *LogMiddleware) WrapHandler(handler handlerFunc) handlerFunc

type LogOption

type LogOption func(*LogMiddleware)

func WithLogger

func WithLogger(l plog.Logger) LogOption

type Middleware

type Middleware interface {
	WrapHandler(handler handlerFunc) handlerFunc
}

type OptRoute

type OptRoute interface {
	Route
	Option(*mux.Route) *mux.Route
}

type Prouter

type Prouter struct {
	RouterGroup
	// contains filtered or unexported fields
}

func New

func New(opts ...RouterOption) *Prouter

func NewProuter

func NewProuter(opts ...RouterOption) *Prouter

func (*Prouter) Run

func (v *Prouter) Run(addr string) error

func (*Prouter) ServeHTTP

func (v *Prouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Prouter) ServeHandler

func (v *Prouter) ServeHandler() *mux.Router

type RecoveryMiddleware added in v1.0.4

type RecoveryMiddleware struct{}

func NewRecoveryMiddleware added in v1.0.4

func NewRecoveryMiddleware() *RecoveryMiddleware

func (*RecoveryMiddleware) WrapHandler added in v1.0.4

func (m *RecoveryMiddleware) WrapHandler(handler handlerFunc) handlerFunc

type Response

type Response interface {
	SetCode(int) Response
	SetData(any) Response
	SetMessage(string) Response
	GetCode() int
	GetMessage() string
	GetData() any
}

func ErrorResponse

func ErrorResponse(code int, message string) Response

func SuccessResponse

func SuccessResponse(data any) Response

type ResponseErrType added in v1.1.5

type ResponseErrType string
const (
	BadRequest          ResponseErrType = "BadRequest"
	InternalServerError ResponseErrType = "InternalServerError"
	Forbidden           ResponseErrType = "Forbidden"
	NotFound            ResponseErrType = "NotFound"
	AlreadyExists       ResponseErrType = "AlreadyExists"
)

type ResponseTmpl added in v1.0.4

type ResponseTmpl interface {
	Response
}

func NewResponseTmpl added in v1.0.4

func NewResponseTmpl() ResponseTmpl

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func WrapResponseWriter

func WrapResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) StatusCode

func (w *ResponseWriter) StatusCode() int

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(code int)

type Ret

type Ret struct {
	Code    int    `json:"code"`
	Data    any    `json:"data,omitempty"`
	Message string `json:"message,omitempty"`
}

func (*Ret) GetCode

func (r *Ret) GetCode() int

func (*Ret) GetData

func (r *Ret) GetData() any

func (*Ret) GetMessage

func (r *Ret) GetMessage() string

func (*Ret) SetCode added in v1.0.4

func (r *Ret) SetCode(i int) Response

func (*Ret) SetData added in v1.0.4

func (r *Ret) SetData(a any) Response

func (*Ret) SetMessage added in v1.0.4

func (r *Ret) SetMessage(s string) Response

type Route

type Route interface {
	Handler() handlerFunc
	Path() string
	Method() string
}

func NewRoute

func NewRoute(method, path string, handler HandleFunc, opts ...RouteOption) Route

type RouteOption

type RouteOption func(*mux.Route) *mux.Route

type Router

type Router interface {
	Routes() []Route
}

type RouterGroup

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

func (*RouterGroup) Any

func (rg *RouterGroup) Any(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) DELETE

func (rg *RouterGroup) DELETE(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) GET

func (rg *RouterGroup) GET(path string, handler HandleFunc, opt ...RouteOption)

func (*RouterGroup) Group

func (rg *RouterGroup) Group(prefix string, middlewares ...HandleFunc) *RouterGroup

func (*RouterGroup) HEAD

func (rg *RouterGroup) HEAD(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) HandleRoute

func (rg *RouterGroup) HandleRoute(method, path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) HandleRouter added in v1.1.10

func (rg *RouterGroup) HandleRouter(routers ...Router)

func (*RouterGroup) OPTIONS

func (rg *RouterGroup) OPTIONS(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) PATCH

func (rg *RouterGroup) PATCH(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) POST

func (rg *RouterGroup) POST(path string, handler HandleFunc, opt ...RouteOption)

func (*RouterGroup) PUT

func (rg *RouterGroup) PUT(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) Static

func (rg *RouterGroup) Static(path, root string, opts ...RouteOption)

func (*RouterGroup) StaticFS

func (rg *RouterGroup) StaticFS(relativePath string, fs http.FileSystem, opts ...RouteOption)

func (*RouterGroup) StaticFsEmbed added in v1.0.11

func (rg *RouterGroup) StaticFsEmbed(path, fileRelativePath string, fsEmbed embed.FS, opts ...RouteOption)

func (*RouterGroup) TRACE

func (rg *RouterGroup) TRACE(path string, handler HandleFunc, opts ...RouteOption)

func (*RouterGroup) Use added in v1.1.3

func (rg *RouterGroup) Use(middlewares ...HandleFunc)

func (*RouterGroup) UseMiddleware

func (rg *RouterGroup) UseMiddleware(m ...Middleware)

type RouterOption

type RouterOption func(v *Prouter)

func WithHost

func WithHost(host string) RouterOption

func WithMethodNotAllowedHandler

func WithMethodNotAllowedHandler(handler http.Handler) RouterOption

func WithNotFoundHandler

func WithNotFoundHandler(handler http.Handler) RouterOption

func WithScheme

func WithScheme(scheme string) RouterOption

type Session added in v1.1.2

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

func SessionGet added in v1.1.2

func SessionGet(ctx context.Context, r *http.Request, w http.ResponseWriter) (*Session, error)

func (*Session) Delete added in v1.1.2

func (s *Session) Delete(key string) error

func (*Session) Get added in v1.1.2

func (s *Session) Get(key string) (interface{}, error)

func (*Session) ID added in v1.1.2

func (s *Session) ID() string

func (*Session) Save added in v1.1.2

func (s *Session) Save() error

func (*Session) Set added in v1.1.2

func (s *Session) Set(key string, value interface{}) error

type SessionMiddleware added in v1.0.10

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

func NewSessionMiddleware added in v1.0.10

func NewSessionMiddleware(key string, stores ...sessions.Store) *SessionMiddleware

func (*SessionMiddleware) WrapHandler added in v1.0.10

func (m *SessionMiddleware) WrapHandler(handler handlerFunc) handlerFunc

Directories

Path Synopsis
example
api

Jump to

Keyboard shortcuts

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