Documentation ¶
Index ¶
- Constants
- Variables
- func IsDebugging() bool
- func Mode() string
- func SetMode(value string)
- type Action
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(err error) error
- func (c *Context) Error(err error) *Error
- func (c *Context) Get(key string) (value any, exists bool)
- func (c *Context) GetBool(key string) (b bool)
- func (c *Context) GetDuration(key string) (d time.Duration)
- func (c *Context) GetFloat64(key string) (f64 float64)
- func (c *Context) GetInt(key string) (i int)
- func (c *Context) GetInt64(key string) (i64 int64)
- func (c *Context) GetString(key string) (s string)
- func (c *Context) GetStringMap(key string) (sm map[string]any)
- func (c *Context) GetStringMapString(key string) (sms map[string]string)
- func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)
- func (c *Context) GetStringSlice(key string) (ss []string)
- func (c *Context) GetTime(key string) (t time.Time)
- func (c *Context) GetUint(key string) (ui uint)
- func (c *Context) GetUint64(key string) (ui64 uint64)
- func (c *Context) IsAborted() bool
- func (c *Context) MustGet(key string) any
- func (c *Context) Next() error
- func (c *Context) Set(key string, value any)
- func (c *Context) String(value string) error
- type Engine
- type Error
- type ErrorType
- type HandlerFunc
- func CustomRecovery(handle RecoveryFunc) HandlerFunc
- func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc
- func Logger() HandlerFunc
- func LoggerWithConfig(conf LoggerConfig) HandlerFunc
- func Recovery() HandlerFunc
- func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc
- type HandlersChain
- type IRoutes
- type LogFormatter
- type LogFormatterParams
- type LoggerConfig
- type Options
- type RecoveryFunc
- type Router
Constants ¶
const ( DebugMode = "debug" ReleaseMode = "release" TestMode = "test" )
const AbortIndex int8 = math.MaxInt8 / 2
const EnvNinMode = "NIN_MODE"
Variables ¶
var ( ErrPathInvalid = errors.New("no valid path") ErrPathNotFound = func(path string) error { return fmt.Errorf("%s path not found", path) } )
var DebugPrintRouteFunc func(path, handlerName string, nuHandlers int)
DebugPrintRouteFunc indicates debug log output format.
var DefaultErrorWriter io.Writer = os.Stderr
DefaultErrorWriter is the default io.Writer used by Nin to debug errors
var DefaultWriter io.Writer = os.Stdout
var (
ErrPrivateKeyEmpty = errors.New("privateKey can not be empty")
)
var ( StatusMap = map[sdk.Status]string{ sdk.PublishStatusSent: "sent", sdk.PublishStatusFailed: "failed", sdk.PublishStatusSucceeded: "succeed", } )
Functions ¶
func IsDebugging ¶
func IsDebugging() bool
IsDebugging returns true if the framework is running in debug mode. Use SetMode(nin.ReleaseMode) to disable debug mode.
Types ¶
type Context ¶
type Context struct { Writer *sdk.Relay PublicKey string Path string Handlers HandlersChain // Middleware and final handler functions Action *Action Event *sdk.Event Status sdk.Status // Errors is a list of errors attached to all the handlers/middlewares who used this context. Errors errorMsgs // Keys is a key/value pair exclusively for the context of each request. Keys map[string]any // contains filtered or unexported fields }
func (*Context) AbortWithError ¶
func (*Context) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exist it returns (nil, false)
func (*Context) GetDuration ¶
GetDuration returns the value associated with the key as a duration.
func (*Context) GetFloat64 ¶
GetFloat64 returns the value associated with the key as a float64.
func (*Context) GetStringMap ¶
GetStringMap returns the value associated with the key as a map of interfaces.
func (*Context) GetStringMapString ¶
GetStringMapString returns the value associated with the key as a map of strings.
func (*Context) GetStringMapStringSlice ¶
GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (*Context) GetStringSlice ¶
GetStringSlice returns the value associated with the key as a slice of strings.
func (*Context) GetUint64 ¶
GetUint64 returns the value associated with the key as an unsigned integer.
func (*Context) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
type Error ¶
Error represents a error's specification.
func (*Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface.
type ErrorType ¶
type ErrorType uint64
ErrorType is an unsigned 64-bit error code as defined in the gin spec.
const ( // ErrorTypeBind is used when Context.Bind() fails. ErrorTypeBind ErrorType = 1 << 63 // ErrorTypeRender is used when Context.Render() fails. ErrorTypeRender ErrorType = 1 << 62 // ErrorTypePrivate indicates a private error. ErrorTypePrivate ErrorType = 1 << 0 // ErrorTypePublic indicates a public error. ErrorTypePublic ErrorType = 1 << 1 // ErrorTypeAny indicates any other error. ErrorTypeAny ErrorType = 1<<64 - 1 // ErrorTypeNu indicates any other error. ErrorTypeNu = 2 )
type HandlerFunc ¶
func CustomRecovery ¶
func CustomRecovery(handle RecoveryFunc) HandlerFunc
CustomRecovery returns a middleware that recovers from any panics and calls the provided handle func to handle it.
func CustomRecoveryWithWriter ¶
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc
CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
func Logger ¶
func Logger() HandlerFunc
func LoggerWithConfig ¶
func LoggerWithConfig(conf LoggerConfig) HandlerFunc
LoggerWithConfig instance a Logger middleware with config.
func Recovery ¶
func Recovery() HandlerFunc
Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
func RecoveryWithWriter ¶
func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc
RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one.
type HandlersChain ¶
type HandlersChain []HandlerFunc
func (HandlersChain) Last ¶
func (c HandlersChain) Last() HandlerFunc
Last returns the last handler in the chain. i.e. the last handler is the main one.
type IRoutes ¶
type IRoutes interface { Use(handlers ...HandlerFunc) IRoutes Add(path string, handlers ...HandlerFunc) IRoutes Handlers() map[string]HandlersChain Middles() HandlersChain Close() error }
type LogFormatter ¶
type LogFormatter func(params LogFormatterParams) string
LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
type LogFormatterParams ¶
type LogFormatterParams struct { // TimeStamp shows the time after the server returns a response. TimeStamp time.Time // PublishStatus is relay publish code. PublishStatus sdk.Status // Latency is how much time the server cost to process a certain request. Latency time.Duration // ID equals event ID. ID string // ID equals event PubKey. PubKey string // Path is a path the client requests. Path string // ErrorMessage is set if error has occurred in processing the request. ErrorMessage string // BodySize is the size of the Response Body BodySize int // Keys are the keys set on the request's context. Keys map[string]any // contains filtered or unexported fields }
LogFormatterParams is the structure any formatter will be handed when time to log comes
func (*LogFormatterParams) IsOutputColor ¶
func (p *LogFormatterParams) IsOutputColor() bool
IsOutputColor indicates whether can colors be outputted to the log.
func (*LogFormatterParams) PublishStatusColor ¶
func (p *LogFormatterParams) PublishStatusColor() string
PublishStatusColor is the ANSI color for appropriately logging http status code to a terminal.
func (*LogFormatterParams) ResetColor ¶
func (p *LogFormatterParams) ResetColor() string
ResetColor resets all escape attributes.
type LoggerConfig ¶
type LoggerConfig struct { // Optional. Default value is nin.defaultLogFormatter Formatter LogFormatter // Output is a writer where logs are written. // Optional. Default value is nin.DefaultWriter. Output io.Writer // SkipPaths is an url path array which logs are not written. // Optional. SkipPaths []string }
LoggerConfig defines the config for Logger middleware.
type Options ¶
type RecoveryFunc ¶
RecoveryFunc defines the function passable to CustomRecovery.
type Router ¶
type Router struct { MiddleWares HandlersChain // contains filtered or unexported fields }
func (*Router) Handlers ¶
func (r *Router) Handlers() map[string]HandlersChain
func (*Router) Middles ¶
func (r *Router) Middles() HandlersChain
func (*Router) Use ¶
func (r *Router) Use(middleware ...HandlerFunc) IRoutes