Documentation
¶
Index ¶
- Constants
- func NewError(statusCode int, err string) error
- type BodyCtx
- type Bolt
- type BoltHook
- type CompleteRouter
- type Config
- type CookieCtx
- type Ctx
- type EasyFastHandlerFunc
- type Error
- type HandlerFunc
- type HeaderCtx
- type Map
- type MemoryStore
- func (s *MemoryStore) Close() error
- func (s *MemoryStore) Del(key string) error
- func (s *MemoryStore) Exists(key string) bool
- func (s *MemoryStore) Get(key string) ([]byte, error)
- func (s *MemoryStore) Set(key string, value []byte) error
- func (s *MemoryStore) SetEx(key string, value []byte, expiry time.Duration) error
- type MiddlewareFunc
- type Mode
- type Route
- type RouteParamValidatorFunc
- type RoutePart
- type Router
- type RouterParamValidator
- type SessionConfig
- type SessionCtx
- type SessionStore
- type ViewConfig
- type WSConfig
- type WSConn
- type WSHandlerFunc
- type WSMessageHandler
- type WSServer
Constants ¶
const ( ContentTypeJSON = "application/json" ContentTypeText = "text/plain" ContentTypeHTML = "text/html" ContentTypeXML = "application/xml" ContentTypeForm = "application/x-www-form-urlencoded" ContentTypeMultipart = "multipart/form-data" )
const Version = "1.0.1"
Version is the current version of Bolt
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BodyCtx ¶
type BodyCtx interface {
// Parse the request body to any by the Content-Type header
Parse(v any) error
// ParseJSON parses the request body as JSON
ParseJSON(v any) error
// ParseXML parses the request body as XML
ParseXML(v any) error
// ParseForm parses the request body as form
ParseForm(v any) error
// File returns a file from the request
File(name string, maxSize ...int) (multipart.File, *multipart.FileHeader, error)
}
BodyCtx is the context of the request body
type Bolt ¶
type Bolt struct {
CompleteRouter
// contains filtered or unexported fields
}
func (*Bolt) Hook ¶
Hook registers a hook for the given hook type Note: Hooks are methods that are executed before or after a request is processed
type CompleteRouter ¶
type CompleteRouter interface {
Router
RouterParamValidator
// Dump writes the routes to the console.
Dump()
// contains filtered or unexported methods
}
CompleteRouter is an interface that combines the Router and RouterParamValidator interfaces.
type Config ¶
type Config struct {
// ErrorHandler handles the request errors
ErrorHandler func(c Ctx, err error) error
// NotFoundHandler handles the not found requests
NotFoundHandler func(c Ctx) error
// Mode is the application mode
// default is development
Mode Mode
Views *ViewConfig
Session *SessionConfig
Websocket *WSConfig
}
Config is the configuration of the Bolt application
type CookieCtx ¶
type CookieCtx interface {
// Get returns a cookie by name
Get(name string) (*http.Cookie, error)
// Set sets a cookie
Set(cookie *http.Cookie)
}
CookieCtx is the context of the request cookies
type Ctx ¶
type Ctx interface {
ID() string
// Method returns the request method
Method() string
// URL returns the request URL
URL() *url.URL
// Path returns the request path
Path() string
// Params returns all route params
Params() map[string]string
// Param returns a route param by name
Param(name string, defaultValue ...string) string
// ParamInt returns a route param by name as int
ParamInt(name string, defaultValue ...int) (int, error)
// ResponseWriter returns the http.ResponseWriter
ResponseWriter() http.ResponseWriter
// Request returns the http.Request
Request() *http.Request
// Context returns the Request Context
Context() context.Context
// App returns the Bolt application
App() *Bolt
// IP returns the client IP
IP() net.IP
// Headers
// Header returns a HeaderCtx to add response header and get request header
Header() HeaderCtx
// Cookie returns a CookieCtx to get and set cookies
Cookie() CookieCtx
// Session returns a SessionCtx to get and set session data (if session is enabled in the configuration)
Session() SessionCtx
// ContentType sets the response content type
ContentType(t string) Ctx
// Status sets the response status code
Status(code int) Ctx
// Send sends the output as a byte slice
Send([]byte) error
// SendString sends the output as a string
SendString(s string) error
// JSON sends the output as a JSON
JSON(data any) error
// XML sends the output as a XML
XML(data any) error
// SendFile sends a file as a response
SendFile(path string) error
// Pipe sends the output as a stream
Pipe(pipe func(pw *io.PipeWriter)) error
// Format sends the output in the format specified in the Accept header
Format(data any) error
// Redirect redirects the request to the specified URL
Redirect(to string) error
Spark(component spark.Component) error
// Body returns a BodyCtx to parse the request body
Body() BodyCtx
// Get returns a stored value by key
Get(key string) any
// Set stores a value by key in the context (useful for middleware)
// note: the value only exists in the current request
Set(key string, value any)
// Locals returns all stored values
Locals() map[string]any
}
Ctx is the context of the request
type EasyFastHandlerFunc ¶
type HandlerFunc ¶
HandlerFunc is a function that handles a request.
func Adaptor ¶
func Adaptor(fn http.HandlerFunc) HandlerFunc
Adaptor converts the standard http.HandlerFunc to a bolt.HandlerFunc
func EasyFastAdaptor ¶
func EasyFastAdaptor(fn EasyFastHandlerFunc) HandlerFunc
EasyFastAdaptor is a fast way to just dump data or error as a response without using the Ctx send methods
type HeaderCtx ¶
type HeaderCtx interface {
// Add adds a header to the response
Add(key, value string)
// Get returns a header from the request
Get(key string) string
}
HeaderCtx is the context of the request headers
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-memory session storage.
func (*MemoryStore) Close ¶
func (s *MemoryStore) Close() error
func (*MemoryStore) Del ¶
func (s *MemoryStore) Del(key string) error
func (*MemoryStore) Exists ¶
func (s *MemoryStore) Exists(key string) bool
type MiddlewareFunc ¶
MiddlewareFunc is a function that is executed before the handler.
type Route ¶ added in v1.0.1
type Route interface {
Name(name string)
GetName() string
Method() string
Path() string
NormalizedPaths() []string
Handler() HandlerFunc
Middlewares() []MiddlewareFunc
// contains filtered or unexported methods
}
type RouteParamValidatorFunc ¶
RouteParamValidatorFunc is a function that validates a route parameter.
type Router ¶
type Router interface {
// Get registers a new GET route for a path with matching handler and optional middlewares.
Get(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Post registers a new POST route for a path with matching handler and optional middlewares.
Post(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Put registers a new PUT route for a path with matching handler and optional middlewares.
Put(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Delete registers a new DELETE route for a path with matching handler and optional middlewares.
Delete(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Patch registers a new PATCH route for a path with matching handler and optional middlewares.
Patch(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Options registers a new OPTIONS route for a path with matching handler and optional middlewares.
Options(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// All registers a new route for a path with matching handler and optional middlewares.
All(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// Add registers a new route for a path with matching handler and optional middlewares.
Add(method, path string, handler HandlerFunc, middlewares ...MiddlewareFunc) Route
// WS registers a new WebSocket route for a path with matching handler and optional middlewares.
// Note: WebSocket routes should have a different path then GET routes.
WS(path string, handler WSHandlerFunc, middlewares ...MiddlewareFunc) Route
// Group creates a new router group with a common prefix and optional middlewares.
Group(prefix string, middlewares ...MiddlewareFunc) Router
}
Router is an interface that defines the methods for registering routes.
type RouterParamValidator ¶
type RouterParamValidator interface {
// RouterParamValidator is an interface that allows you to register custom route parameter validators.
// default validators: int, bool, uuid, alpha, alphanumeric.
RegisterRouteParamValidator(name string, fn RouteParamValidatorFunc)
}
RouterParamValidator is an interface that allows you to register custom route parameter validators.
type SessionConfig ¶
type SessionConfig struct {
// Enabled is a flag to enable or disable the session
// session is enabled by default
Enabled bool
// TokenFunc is a function to get the session token
// by default it generates a new token if not exists, and stores it in a cookie named "session"
TokenFunc func(c Ctx) (string, error)
// TokenExpire is the session token expiration time
// by default it is 12 hours
// tokens are renewed at each modification
TokenExpire time.Duration
// Store is the session storage
// by default it uses the MemStorage, an in-memory storage
Store SessionStore
}
SessionConfig is the configuration of the session
type SessionCtx ¶
type SessionCtx interface {
// Get returns a session value by key
Get(key string) ([]byte, error)
// Set sets a session value by key
Set(key string, value []byte) error
// Delete deletes a session value by key
Delete(key string) error
// Destroy destroys the session
Destroy() error
// From returns a session from another session id
From(id string) SessionCtx
// ID returns the session id
ID() string
// SetID sets the session id
SetID(s string) SessionCtx
}
SessionCtx is the context of the request session
type SessionStore ¶
type SessionStore interface {
Get(key string) ([]byte, error)
Exists(key string) bool
Set(key string, value []byte) error
SetEx(key string, value []byte, expiry time.Duration) error
Del(key string) error
}
SessionStore is an interface for session storage.
func NewMemStorage ¶
func NewMemStorage(gcInterval ...time.Duration) SessionStore
NewMemStorage creates a new MemoryStore.
type ViewConfig ¶ added in v1.0.2
type WSConfig ¶
type WSConfig struct {
Timeout time.Duration
AcceptOptions *websocket.AcceptOptions
}
WSConfig is the configuration of the websocket
type WSHandlerFunc ¶
type WSHandlerFunc func(conn WSConn)
WSHandlerFunc is a function that handles a WebSocket request.
type WSServer ¶
type WSServer interface {
// AddConnection adds a new connection to the server.
AddConn(conn WSConn)
// RemoveConnection removes a connection from the server.
RemoveConn(conn WSConn)
// RemoveConnection removes a connection from the server.
Broadcast(msg []byte)
// BroadcastFunc sends a message to all connections that satisfy the condition.
BroadcastFunc(msg []byte, fn func(conn WSConn) bool)
// BroadcastTo sends a message to a specific connection.
BroadcastTo(msg []byte, conns ...WSConn)
// OnMessage sets the function to be called when a message is received.
OnMessage(fn WSMessageHandler)
// Close closes the server.
Close() error
// contains filtered or unexported methods
}
WSServer is the interface that wraps the basic methods for a websocket server.