a simple framework base gin that you can easy to code your web server
when I use gin to codding my web server,i always need to bind request and write response.I think,if we can codding web api like codding proto server,that will be easy and distinct.
In order to make codding web api easy, I write this repo.when I use it,I just need to define a struct and some method,the framework will auto bind to router use POST with JSON.
method format like: func (ctx *gin.Context,req {struct}) (resp {struct ptr},err error)
OR
autoBind will router the method that param start with *gin.Context and params less than 2
go get github.com/MikeLINGxZ/rome-routerapiRouter := []simple_server_runner.Router{
{
Path: "/api",
Method: "POST",
HandlerFunc: &ApiServer{},
ChildRouter: []simple_server_runner.Router{
{
Path: "/Login",
Method: "POST",
HandlerFunc: Login,
ChildRouter: []simple_server_runner.Router{
{
Path: "/Username",
Method: "GET",
HandlerFunc: func (ctx *gin.Context) error {
return nil
},
ChildRouter: nil,
Middlewares: []gin.HandlerFunc{Auth()},
},
}
Middlewares: nil,
},
},
Middlewares: nil,
}
}server := simple_server_runner.NewServer()
server.AddRouter(router)
err := server.Run(":8787")
if err != nil {
panic(err)
}you will see
[GIN-debug] GET / --> github.com/MikeLINGxZ/simple-server-runner.(*Server).bindRouter.func1 (3 handlers)
[GIN-debug] POST /api/LoginOut --> github.com/MikeLINGxZ/simple-server-runner.(*Server).bindGroupRouter.func1 (3 handlers)
[GIN-debug] POST /api/Login --> github.com/MikeLINGxZ/simple-server-runner.(*Server).bindRouter.func1 (3 handlers)
[GIN-debug] GET /api/Login/Username --> github.com/MikeLINGxZ/simple-server-runner.(*Server).bindRouter.func1 (4 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8787
runner.SetResponse(func(ctx *gin.Context, data interface{}, errInterface interface{}) {
resp := CustomResponse{}
if errInterface != nil {
err, ok := errInterface.(*CustomResponse)
if ok {
resp.Error = err.Error
} else {
resp.Error = "gg"
}
ctx.JSON(http.StatusBadGateway, resp)
return
}
resp.Data = data
ctx.JSON(http.StatusOK, resp)
return
})you don't have to define an empty struct yourself.
func GetAge(ctx *gin.Context) error {
// todo
return nil
}visible errors will show error to client
func CustomErrorWithAuto(ctx *gin.Context) error {
log.Println("NothingToDo")
return simple_server_runner.NewCustomError("test error")
}custom error will show show:
{
"code": 500,
"msg": "your custom error",
"data": null
}other error will show:
{
"code": 500,
"msg": "internal error",
"data": null
}