func(*gin.Context) //go-gin 原始接口
func(*api.Context) //自定义的 context 类型
func(*api.Context,req) //自定义的 context 类型,带 request 请求参数
func(*api.Context,*req)
func(*gin.Context,*req) //go-gin context 类型,带 request 请求参数
func(*gin.Context,req)
package main
import (
"fmt"
"net/http"
_ "ginweb/routers" // debug 模式需要添加[mod]/routers 注册注解路由
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc"
"github.com/xxjwxc/ginrpc/api"
)
type ReqTest struct {
Access_token string `json:"access_token"`
UserName string `json:"user_name" binding:"required"` // 带校验方式
Password string `json:"password"`
}
//TestFun4 带自定义 context 跟已解析的 req 参数回调方式
func TestFun4(c *gin.Context, req ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON( http.StatusOK, req)
}
func main() {
base := ginrpc.New()
router := gin.Default()
router.POST("/test4", base.HandlerFunc(TestFun4))
base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun4) // 多种请求方式注册
router.Run(":8080")
}
curl
curl 'http://127.0.0.1:8080/test4' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
``` go mod init ginweb ```
package main
import (
"fmt"
"net/http"
_ "ginweb/routers" // debug 模式需要添加[mod]/routers 注册注解路由
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc"
"github.com/xxjwxc/ginrpc/api"
)
type ReqTest struct {
Access_token string `json:"access_token"`
UserName string `json:"user_name" binding:"required"` // 带校验方式
Password string `json:"password"`
}
// Hello ...
type Hello struct {
}
// Hello 带注解路由(参考 beego 形式)
// @router /block [post,get]
func (s *Hello) Hello(c *api.Context, req *ReqTest) {
fmt.Println(req)
fmt.Println(s.Index)
c.JSON( http.StatusOK, "ok")
}
// Hello2 不带注解路由(参数为 2 默认 post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
fmt.Println(req)
fmt.Println(s.Index)
c.JSON( http.StatusOK, "ok")
}
func main() {
base := ginrpc.New(ginrpc.WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}), ginrpc.WithDebug(true), ginrpc.WithGroup("xxjwxc"))
router := gin.Default()
base.Register(router, new(Hello)) // 对象注册 like(go-micro)
// or base.Register(router, new(Hello))
router.Run(":8080")
}
// @router /block [post,get]
@router 标记
/block 路由
[post,get] method 调用方式
```
_ "[mod]/routers" // debug 模式需要添加[mod]/routers 注册注解路由
```
默认也会在项目根目录生成[gen_router.data]文件(保留此文件,可以不用添加上面代码嵌入)
详细请看 demo [ginweb](/sample/ginweb)
ginrpc.WithCtx: 设置自定义 context
ginrpc.WithDebug(true) : 设置 debug 模式
ginrpc.WithGroup("xxjwxc") : 添加路由前缀 (也可以使用 gin.Group 分组)
ginrpc.WithBigCamel(true) : 设置大驼峰标准(false 为 web 模式,_,小写)
[更多]( https://godoc.org/github.com/xxjwxc/ginrpc)
curl 'http://127.0.0.1:8080/xxjwxc/block' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
curl 'http://127.0.0.1:8080/xxjwxc/hello.hello2' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
1.导出 api 文档
2.导出 postman 测试配置
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.