Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
"github.com/virzz/ginx/code"
"github.com/virzz/ginx/req"
"github.com/virzz/ginx/rsp"
"github.com/virzz/vlog"
"github.com/virzz/webx"
)
const (
id = "com.ctfhub.app.webx.test"
name = "webx-test"
desc = "WebxTest"
)
var (
Version = "latest"
Commit = "unknown"
)
// config.go
type Config struct {
//lint:ignore SA5008 Ignore JSON option "squash"
webx.Config `json:",inline,squash" yaml:",inline"`
// Define custom configuration here
Custom CustomConfig `json:"custom" yaml:"custom"`
}
type CustomConfig struct{}
var Conf = &Config{}
func main() {
app := &webx.App{
ID: id, Name: name, Description: desc,
Version: Version, Commit: Commit,
}
if err := webx.Run(context.Background(), app, Conf); err != nil {
panic(err)
}
}
func init() {
webx.POST("/test", HandleTest)
}
type (
TestReq struct {
Example string `json:"example" form:"example" uri:"example"`
}
TestRsp struct{}
)
func HandleTest(c *gin.Context, req TestReq) (rsp TestRsp, err error) {
return rsp, nil
}
func HandleGinTest(c *gin.Context) {
rid := requestid.Get(c)
// 解析入参
var _req TestReq
if err := req.ShouldBind(c, &_req); err != nil {
vlog.Error("Failed to bind req", "requestid", rid, "err", err.Error())
c.AbortWithStatusJSON(400, rsp.E(code.ParamInvalid, "Error:"+err.Error()))
return
}
// 业务处理
if _req.Example != "test" {
vlog.Error("Failed to bind req", "requestid", rid, "err", "Example is not test")
c.AbortWithStatusJSON(500, rsp.E(code.UnknownErr, "Error: Example is not test"))
return
}
// 返回结果
c.JSON(200, rsp.OK())
}
Index ¶
- func AddCommand(cmd ...*cobra.Command)
- func Any[T, P any](p string, h HandlerFunc[T, P])
- func AppID() string
- func Commit() string
- func CreateCmd(cfg Configer) *cobra.Command
- func Desc() string
- func Description() string
- func Execute(ctx context.Context, cfg Configer) error
- func GET[T, P any](p string, h HandlerFunc[T, P])
- func GinAny(p string, h gin.HandlerFunc)
- func GinGET(p string, h gin.HandlerFunc)
- func GinPOST(p string, h gin.HandlerFunc)
- func ID() string
- func InitCmd(cfg Configer, models ...any) *cobra.Command
- func MigrateCmd(cfg Configer, models ...any) *cobra.Command
- func Name() string
- func POST[T, P any](p string, h HandlerFunc[T, P])
- func Register(f ginx.RegisterFunc)
- func RegisterHandle[T, P any](m, p string, h HandlerFunc[T, P])
- func Run(ctx context.Context, app *App, cfg Configer) error
- func SetPreInit(f PreInitFunc)
- func SetValidate(f ValidateFunc)
- func Version() string
- func Warpper[T, P any](handle HandlerFunc[T, P]) gin.HandlerFunc
- func WarpperRaw(handle HandlerRawFunc) gin.HandlerFunc
- type App
- type Config
- type Configer
- type HandlerFunc
- type HandlerRawFunc
- type PreInitFunc
- type ValidateFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCommand ¶
func Description ¶
func Description() string
func RegisterHandle ¶
func RegisterHandle[T, P any](m, p string, h HandlerFunc[T, P])
RegisterHandle register any method handler
func SetPreInit ¶
func SetPreInit(f PreInitFunc)
func SetValidate ¶
func SetValidate(f ValidateFunc)
func Warpper ¶
func Warpper[T, P any](handle HandlerFunc[T, P]) gin.HandlerFunc
func WarpperRaw ¶
func WarpperRaw(handle HandlerRawFunc) gin.HandlerFunc
Types ¶
type Config ¶
type PreInitFunc ¶
type ValidateFunc ¶
type ValidateFunc func() error
Source Files
¶
Click to show internal directories.
Click to hide internal directories.