Documentation
¶
Index ¶
- Constants
- func CloseAllRedis()
- func CtxGet[T any](c *HwfCtx, key string) (T, bool)
- func Do(fn func() error) (e error)
- func GetNamedRedis(name string) *redis.Client
- func GetRedis() *redis.Client
- func Go(fn func(), errFn func(error))
- func ListenAndServe(sevCfg *ServerConfig) error
- func Run(app App) error
- func ValidateStruct(dst any) error
- func WrapError(msg string, err error) error
- type App
- type AppJSONFormatter
- type AppPackResourcer
- type AppTLSConfiger
- type ConfigFiler
- type Cron
- type CronJob
- type FieldError
- type H
- type HandlerFunc
- type Hooks
- type HooksFunc
- type HwfCtx
- func (c *HwfCtx) Ctx() context.Context
- func (c *HwfCtx) DB() *gorm.DB
- func (c *HwfCtx) DBNamed(name string) *gorm.DB
- func (c *HwfCtx) GetVal(key string) (any, bool)
- func (c *HwfCtx) Log() *Logger
- func (c *HwfCtx) NamedRedis(name string) *redis.Client
- func (c *HwfCtx) ORM() *ORM
- func (c *HwfCtx) PageFind(db *gorm.DB, page int, pageSize int, ptr any) (int64, error)
- func (c *HwfCtx) PutHook(name string, payload any)
- func (c *HwfCtx) Redis() *redis.Client
- func (c *HwfCtx) Resource() ResourceEngine
- func (c *HwfCtx) SetVal(key string, val any)
- func (c *HwfCtx) StdLog() *log.Loggerdeprecated
- func (c *HwfCtx) Transaction(fn func() error) error
- type LogLevel
- type Logger
- type Module
- type ORM
- type ReqCtx
- func (c *ReqCtx) Abort()
- func (c *ReqCtx) AbortWithStatus(code int)
- func (c *ReqCtx) AbortWithStatusJSON(code int, v any)
- func (c *ReqCtx) Aborted() bool
- func (c *ReqCtx) BindForm(dst any) error
- func (c *ReqCtx) BindHeader(dst any) error
- func (c *ReqCtx) BindJSON(dst any) error
- func (c *ReqCtx) BindQuery(dst any) error
- func (c *ReqCtx) Bytes(code int, b []byte)
- func (c *ReqCtx) ClientIP() string
- func (c *ReqCtx) Cookie(name string) (string, error)
- func (c *ReqCtx) Ctx() *HwfCtx
- func (c *ReqCtx) Form(key string) string
- func (c *ReqCtx) GetHeader(key string) string
- func (c *ReqCtx) GetRawData() ([]byte, error)
- func (c *ReqCtx) HTML(code int, html string)
- func (c *ReqCtx) JSON(code int, v any)
- func (c *ReqCtx) JSONF(v ...any)
- func (c *ReqCtx) Next()
- func (c *ReqCtx) Param(key string, dest any) error
- func (c *ReqCtx) Proxy(target string, setfn func(*httputil.ReverseProxy)) error
- func (c *ReqCtx) Query(key string) string
- func (c *ReqCtx) Redirect(code int, url string)
- func (c *ReqCtx) RenderHtml(code int, templateFile string, data any)
- func (c *ReqCtx) Request() *http.Request
- func (c *ReqCtx) ResponseWriter() http.ResponseWriter
- func (c *ReqCtx) SaveUploadedFile(field, dst string) error
- func (c *ReqCtx) ServeFile(filePath string)
- func (c *ReqCtx) ServeFileDownload(filePath, downloadName string)
- func (c *ReqCtx) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
- func (c *ReqCtx) SetHandlers(handlers []HandlerFunc)
- func (c *ReqCtx) SetHeader(key, value string)
- func (c *ReqCtx) String(code int, s string)
- type ResourceEngine
- type Router
- type RouterEngine
- func (e *RouterEngine) Any(path string, h HandlerFunc)
- func (e *RouterEngine) DELETE(path string, h HandlerFunc)
- func (e *RouterEngine) File(path, file string)
- func (e *RouterEngine) GET(path string, h HandlerFunc)
- func (e *RouterEngine) GetPathMW() map[string][]HandlerFunc
- func (e *RouterEngine) GetRouteMap() map[string]map[string]HandlerFunc
- func (e *RouterEngine) Group(prefix string) Router
- func (e *RouterEngine) POST(path string, h HandlerFunc)
- func (e *RouterEngine) PUT(path string, h HandlerFunc)
- func (e *RouterEngine) Static(prefix, dir string)
- func (e *RouterEngine) Use(mw ...HandlerFunc)
- type ServerConfig
- type ValidationErrors
Constants ¶
const ( MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodDelete = "DELETE" MethodAny = "ANY" )
const Version = "v1.0.0"
Variables ¶
This section is empty.
Functions ¶
func CloseAllRedis ¶
func CloseAllRedis()
closeAllRedis closes all Redis connections (called on shutdown).
func GetNamedRedis ¶
GetNamedRedis returns a named Redis client by name (e.g., "001", "002"). Returns nil if the named Redis is not configured.
func GetRedis ¶
GetRedis returns the default Redis client. Returns nil if Redis is not configured or initialization failed.
func ListenAndServe ¶
func ListenAndServe(sevCfg *ServerConfig) error
Run starts the HTTP server on the given address.
func ValidateStruct ¶
ValidateStruct validates struct fields using `validate:"..."` rules and optional `msg:"rule=message;rule2=message2"`. Supported rules: required, min, max, len, email, eq, gt, lt, ge, le, password, phone, regex, date, datetime, idcard.
Types ¶
type App ¶
type App interface {
// Name returns the project name.
Name() string
// Install runs on first startup when install.lock is missing.
Install(ctx *HwfCtx) error
// Route registers module routes.
Route(r Router)
// Init runs on every startup.
Init(ctx *HwfCtx) error
// Modules returns all modules to register.
Modules() []Module
// Config returns a pointer to the project's configuration struct.
Config() any
}
type AppJSONFormatter ¶
JSONFormatter is the interface to format JSON output.
type AppPackResourcer ¶
PackResourcer is the interface to provide resource filesystem.
type AppTLSConfiger ¶
TLSConfiger is the interface to provide TLS config.
type ConfigFiler ¶
type ConfigFiler interface {
ConfigFile() string
}
ConfigFiler is the interface to provide config file path.
type FieldError ¶
type HandlerFunc ¶
type HandlerFunc func(*ReqCtx)
type Hooks ¶
type Hooks interface {
On(name string, fn HooksFunc)
Put(name string, ctx *HwfCtx, payload any) error
}
Hooks is the interface for hooks.
type HwfCtx ¶
func (*HwfCtx) DBNamed ¶
DBNamed returns a named database handle if configured, otherwise the default.
func (*HwfCtx) GetVal ¶
Get retrieves a value from the request-scoped storage. Returns (nil, false) if key missing.
func (*HwfCtx) NamedRedis ¶
NamedRedis returns the named Redis client if configured.
func (*HwfCtx) Resource ¶
func (c *HwfCtx) Resource() ResourceEngine
Resource returns the resource engine for reading embedded files.
func (*HwfCtx) Transaction ¶
Transaction helper to run fn within a transaction.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
type Module ¶
type Module interface {
// Name returns the module name.
Name() string
// Install runs on first startup when install.lock is missing.
Install(ctx *HwfCtx) error
// Init runs on every startup.
Init(ctx *HwfCtx) error
// Models returns GORM model structs used to auto-create tables.
Models() []any
// Route registers module routes.
Route(r Router)
// Cron returns cron jobs definitions.
Cron(c CronJob)
// Triggers returns hook names module may trigger.
Triggers(h Hooks)
// Config returns a pointer to the module's configuration struct.
Config() any // module specific configuration struct pointer
}
type ORM ¶
type ORM struct {
// contains filtered or unexported fields
}
func GetNamedORM ¶
func (*ORM) Transaction ¶
Transaction 执行一个函数,如果当前不在事务中则开启新事务,否则复用当前事务。 如果函数返回错误,事务会被回滚;否则提交。 注意:如果已经在事务中,函数执行失败不会自动回滚,需要外层事务处理。
type ReqCtx ¶
type ReqCtx struct {
*HwfCtx // framework context
W http.ResponseWriter
R *http.Request
// contains filtered or unexported fields
}
ReqCtx provides request-scoped helpers.
func NewTestReqCtx ¶
func NewTestReqCtx(w http.ResponseWriter, r *http.Request) *ReqCtx
NewTestReqCtx creates a ReqCtx for testing purposes
func (*ReqCtx) Abort ¶
func (c *ReqCtx) Abort()
Abort stops the execution of subsequent handlers in the chain.
func (*ReqCtx) AbortWithStatus ¶
AbortWithStatus aborts the request and sets the HTTP status code.
func (*ReqCtx) AbortWithStatusJSON ¶
AbortWithStatusJSON aborts the request, sets the HTTP status code, and sends a JSON response.
func (*ReqCtx) BindForm ¶
BindForm binds form (for POST with application/x-www-form-urlencoded) with enhanced type conversion and slice support.
func (*ReqCtx) BindHeader ¶
BindHeader binds headers with enhanced type conversion and slice support.
func (*ReqCtx) BindQuery ¶
BindQuery binds URL query parameters with enhanced type conversion and slice support.
func (*ReqCtx) GetRawData ¶
GetRawData reads the raw request body data.
func (*ReqCtx) JSONF ¶
JSONF 使用 App.FormatJSON 对输出进行统一包装;未配置时回退到默认 JSON 输出。 如果应用未实现 AppJSONFormatter 接口,将使用默认的 JSON 格式输出。
func (*ReqCtx) Next ¶
func (c *ReqCtx) Next()
Next executes the next handler in the middleware chain. It should be called within middleware functions to continue processing the request.
func (*ReqCtx) Param ¶
Param gets path parameter by key and assigns it to the provided pointer. Supported types: string, int, int64, uint, uint64, float32, float64, bool Example: c.Param("id", &id)
func (*ReqCtx) Proxy ¶
func (c *ReqCtx) Proxy(target string, setfn func(*httputil.ReverseProxy)) error
Proxy forwards the current request to the given target using ReverseProxy. Example: c.Proxy("http://localhost:9000/") If you need to strip a prefix, adjust c.R.URL.Path before calling.
func (*ReqCtx) RenderHtml ¶
RenderHtml renders the specified HTML template with the given data and status code.
func (*ReqCtx) ResponseWriter ¶
func (c *ReqCtx) ResponseWriter() http.ResponseWriter
ResponseWriter returns the underlying http.ResponseWriter.
func (*ReqCtx) SaveUploadedFile ¶
SaveUploadedFile saves the uploaded file from a multipart form to the specified destination path.
func (*ReqCtx) ServeFileDownload ¶
ServeFileDownload serves a file for download with the specified name.
func (*ReqCtx) SetCookie ¶
func (c *ReqCtx) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
SetCookie sets a cookie with the specified name, value, max age, path, domain, secure, and httpOnly.
func (*ReqCtx) SetHandlers ¶
func (c *ReqCtx) SetHandlers(handlers []HandlerFunc)
SetHandlers sets the handler chain for testing
type ResourceEngine ¶
type Router ¶
type Router interface {
GET(path string, h HandlerFunc)
POST(path string, h HandlerFunc)
PUT(path string, h HandlerFunc)
DELETE(path string, h HandlerFunc)
Any(path string, h HandlerFunc)
Use(mw ...HandlerFunc)
Group(prefix string) Router
Static(prefix, dir string)
File(path, file string)
}
type RouterEngine ¶
type RouterEngine struct {
// contains filtered or unexported fields
}
func (*RouterEngine) Any ¶
func (e *RouterEngine) Any(path string, h HandlerFunc)
Any registers a route handler that matches all HTTP methods.
func (*RouterEngine) DELETE ¶
func (e *RouterEngine) DELETE(path string, h HandlerFunc)
DELETE registers a DELETE route handler.
func (*RouterEngine) File ¶
func (e *RouterEngine) File(path, file string)
File serves a single file at the given path.
func (*RouterEngine) GET ¶
func (e *RouterEngine) GET(path string, h HandlerFunc)
GET registers a GET route handler.
func (*RouterEngine) GetPathMW ¶
func (e *RouterEngine) GetPathMW() map[string][]HandlerFunc
GetPathMW returns the path middleware map for testing purposes
func (*RouterEngine) GetRouteMap ¶
func (e *RouterEngine) GetRouteMap() map[string]map[string]HandlerFunc
GetRouteMap returns the route map for testing purposes
func (*RouterEngine) Group ¶
func (e *RouterEngine) Group(prefix string) Router
Group creates a new router group with the given prefix. All routes registered in the group will have the prefix prepended to their paths. The group inherits middleware from the parent router.
func (*RouterEngine) POST ¶
func (e *RouterEngine) POST(path string, h HandlerFunc)
POST registers a POST route handler.
func (*RouterEngine) PUT ¶
func (e *RouterEngine) PUT(path string, h HandlerFunc)
PUT registers a PUT route handler.
func (*RouterEngine) Static ¶
func (e *RouterEngine) Static(prefix, dir string)
Static serves static files from the given directory at the specified prefix.
func (*RouterEngine) Use ¶
func (e *RouterEngine) Use(mw ...HandlerFunc)
Use adds middleware functions to the router. Middleware will be executed in the order they are added.
type ServerConfig ¶
type ServerConfig struct {
Addr string
TLSConfig *tls.Config
Router *RouterEngine
}
ServerConfig defines the configuration for the HTTP server.
type ValidationErrors ¶
type ValidationErrors []FieldError
func (ValidationErrors) Error ¶
func (ve ValidationErrors) Error() string