Documentation
¶
Index ¶
- Constants
- type Config
- type DefaultResponse
- type HandlerFunc
- type Info
- type MediaType
- type MiddlewareFunc
- type OpenAPI
- type Operation
- type Parameter
- type PathItem
- type RateLimitConfig
- type Request
- type RequestBody
- type Response
- type Route
- type RouterGroup
- func (rg *RouterGroup) Any(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) Bind(object ...any) *RouterGroup
- func (rg *RouterGroup) DELETE(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) GET(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) Group(path string, handlers ...RouterGroupOption) *RouterGroup
- func (rg *RouterGroup) HEAD(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) Handle(method, path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) Middleware(middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) OPTIONS(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) PATCH(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) POST(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) PUT(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
- func (rg *RouterGroup) Use(middlewares []MiddlewareFunc, handlers ...RouterGroupOption) *RouterGroup
- type RouterGroupOption
- type RuleFunc
- type Schema
- type Server
- func (s *Server) EnablePProf(pattern ...string)
- func (s *Server) Logger() *mlog.Logger
- func (s *Server) Middleware(middlewares ...MiddlewareFunc)
- func (s *Server) RegisterRuleWithTranslation(rule string, fn RuleFunc, errMessage map[string]string)
- func (s *Server) Routes() []Route
- func (s *Server) Run()
- func (s *Server) SetAddress(addr string)
- func (s *Server) SetConfigWithMap(configMap map[string]any) error
- func (s *Server) SetLogger(logger *mlog.Logger)
- func (s *Server) SetServerName(name string)
- func (s *Server) SetStaticPath(prefix string, directory string)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- func (s *Server) Use(middlewares ...MiddlewareFunc)
Constants ¶
const (
DefaultServerName = "default"
)
const (
ResponseKey contextKey = "MaltoseResponse"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v0.1.2
type Config struct {
// basic config
Address string
ServerName string
ServerRoot string
ServerLocale string
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
// Health check
HealthCheck bool
// TLS config
TLSEnable bool
TLSCertFile string
TLSKeyFile string
TLSServerName string
// graceful shutdown config
GracefulEnable bool
GracefulTimeout time.Duration
GracefulWaitTime time.Duration
// API doc config
OpenapiPath string
SwaggerPath string
SwaggerTemplate string
// log config
Logger *mlog.Logger
}
Config is the server configuration.
func ConfigFromMap ¶ added in v0.1.2
ConfigFromMap creates a new server config from a map.
type DefaultResponse ¶
type DefaultResponse struct {
Code int `json:"code"` // business code
Message string `json:"message"` // prompt information
Data any `json:"data"` // business data
}
DefaultResponse standard response structure
type HandlerFunc ¶
type HandlerFunc func(*Request)
HandlerFunc defines the basic handler function type.
type MiddlewareFunc ¶
type MiddlewareFunc func(*Request)
MiddlewareFunc defines the middleware function type.
func MiddlewareLog ¶
func MiddlewareLog() MiddlewareFunc
MiddlewareLog is a middleware for logging HTTP requests.
func MiddlewareRateLimit ¶
func MiddlewareRateLimit(config RateLimitConfig) MiddlewareFunc
MiddlewareRateLimit creates a middleware that implements rate limiting using a token bucket algorithm
func MiddlewareRateLimitByIP ¶
func MiddlewareRateLimitByIP(config RateLimitConfig) MiddlewareFunc
MiddlewareRateLimitByIP creates a middleware that implements rate limiting per IP address
func MiddlewareResponse ¶
func MiddlewareResponse() MiddlewareFunc
MiddlewareResponse standard response middleware
type OpenAPI ¶
type OpenAPI struct {
Openapi string `json:"openapi"`
Info Info `json:"info"`
Paths map[string]PathItem `json:"paths"`
}
OpenAPI specification object
type Operation ¶
type Operation struct {
Tags []string `json:"tags,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty"`
Responses map[string]Response `json:"responses"`
}
type RateLimitConfig ¶
type RateLimitConfig struct {
// Rate defines the number of requests allowed per second
Rate float64
// Burst defines the maximum number of requests that can be processed at once
Burst int
// SkipFunc is an optional function to determine if rate limiting should be skipped
SkipFunc func(*Request) bool
// ErrorHandler is an optional function to handle rate limit errors
ErrorHandler func(*Request)
}
RateLimitConfig defines the configuration for rate limiting
func DefaultRateLimitConfig ¶
func DefaultRateLimitConfig() RateLimitConfig
DefaultRateLimitConfig returns a default rate limit configuration
type Request ¶
Request is the request wrapper.
func RequestFromCtx ¶
RequestFromCtx gets the Request object from the context.
func (*Request) GetHandlerResponse ¶
GetHandlerResponse gets the handler response.
func (*Request) GetServerName ¶
GetServerName gets the server name.
func (*Request) GetTranslator ¶
func (r *Request) GetTranslator() ut.Translator
GetTranslator gets the translator.
func (*Request) SetHandlerResponse ¶
SetHandlerResponse sets the handler response.
type RequestBody ¶
type Route ¶
type Route struct {
Method string
Path string
HandlerFunc HandlerFunc
Type routeType
Controller any // controller object
ControllerMethod reflect.Method // controller method
ReqType reflect.Type // request parameter type
RespType reflect.Type // response type
}
Route is the route information.
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup is the router group for the server.
func (*RouterGroup) Any ¶
func (rg *RouterGroup) Any(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
Any registers all HTTP methods route.
func (*RouterGroup) Bind ¶
func (rg *RouterGroup) Bind(object ...any) *RouterGroup
Bind binds the controller object.
func (*RouterGroup) DELETE ¶
func (rg *RouterGroup) DELETE(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
DELETE registers DELETE request route.
func (*RouterGroup) GET ¶
func (rg *RouterGroup) GET(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
GET registers GET request route.
func (*RouterGroup) Group ¶
func (rg *RouterGroup) Group(path string, handlers ...RouterGroupOption) *RouterGroup
Group creates a new router group.
func (*RouterGroup) HEAD ¶
func (rg *RouterGroup) HEAD(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
HEAD registers HEAD request route.
func (*RouterGroup) Handle ¶
func (rg *RouterGroup) Handle(method, path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
Handle is a general route registration method.
func (*RouterGroup) Middleware ¶
func (rg *RouterGroup) Middleware(middlewares ...MiddlewareFunc) *RouterGroup
Middleware adds middlewares.
func (*RouterGroup) OPTIONS ¶
func (rg *RouterGroup) OPTIONS(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
OPTIONS registers OPTIONS request route.
func (*RouterGroup) PATCH ¶
func (rg *RouterGroup) PATCH(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
PATCH registers PATCH request route.
func (*RouterGroup) POST ¶
func (rg *RouterGroup) POST(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
POST registers POST request route.
func (*RouterGroup) PUT ¶
func (rg *RouterGroup) PUT(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *RouterGroup
PUT registers PUT request route.
func (*RouterGroup) Use ¶
func (rg *RouterGroup) Use(middlewares []MiddlewareFunc, handlers ...RouterGroupOption) *RouterGroup
Use adds middlewares.
type RouterGroupOption ¶
type RouterGroupOption func(*RouterGroup)
type RuleFunc ¶
type RuleFunc func(fl validator.FieldLevel) bool
RuleFunc is the custom validation rule function.
type Schema ¶
type Schema struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Properties map[string]Schema `json:"properties,omitempty"`
Items *Schema `json:"items,omitempty"`
AdditionalProperties *Schema `json:"additionalProperties,omitempty"`
Description string `json:"description,omitempty"`
Required []string `json:"required,omitempty"`
}
type Server ¶
type Server struct {
RouterGroup
// contains filtered or unexported fields
}
Server HTTP server structure.
func (*Server) EnablePProf ¶
EnablePProf enables PProf functionality for the server
func (*Server) Middleware ¶
func (s *Server) Middleware(middlewares ...MiddlewareFunc)
Middleware alias Use.
func (*Server) RegisterRuleWithTranslation ¶
func (s *Server) RegisterRuleWithTranslation(rule string, fn RuleFunc, errMessage map[string]string)
RegisterRuleWithTranslation registers the custom validation rule and translation for multiple languages.
func (*Server) SetAddress ¶
SetAddress sets the server listening address.
func (*Server) SetConfigWithMap ¶
SetConfigWithMap sets the server config.
func (*Server) SetServerName ¶
SetServerName sets the server name.
func (*Server) SetStaticPath ¶
SetStaticPath enhances the static file service.
func (*Server) Use ¶
func (s *Server) Use(middlewares ...MiddlewareFunc)
Use adds global middleware.
Source Files
¶
- mhttp.go
- mhttp_config.go
- mhttp_doc.go
- mhttp_group.go
- mhttp_handler.go
- mhttp_health.go
- mhttp_middleware.go
- mhttp_middleware_internal.go
- mhttp_middleware_internal_metric.go
- mhttp_middleware_log.go
- mhttp_middleware_ratelimit.go
- mhttp_middleware_response.go
- mhttp_openapi.go
- mhttp_pprof.go
- mhttp_prebind.go
- mhttp_request.go
- mhttp_route.go
- mhttp_server.go
- mhttp_swagger.go
- mhttp_validate.go