https://github.com/bugfan/rest
golang restful frame scaffold gin-xorm gin orm
https://github.com/bugfan/rest
Last synced: 4 months ago
JSON representation
golang restful frame scaffold gin-xorm gin orm
- Host: GitHub
- URL: https://github.com/bugfan/rest
- Owner: bugfan
- Created: 2020-09-08T09:59:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-02-16T06:15:31.000Z (over 4 years ago)
- Last Synced: 2024-06-20T03:55:42.096Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go restful api generator
## Introduce
Only need to define the model, automatically generate add delete modify query interface
## Usage
```
// main func
func main() {
g := gin.Default()
x := xorm.NewEngine(...)
rest.NewAPIBackend(g, x, "/api")
log.Fatal(g.Run("127.0.0.1:9996"))
}
// api
func init(){
rest.RegisterModelAndController(&User{}, &UserController{}, rest.RouteTypeALL, nil, "user") //bind all route
rest.RegisterModelAndController(&Admin{}, &AdminController{}, rest.RouteTypeGet, []string{"Password"}, "admin") // just bind get method
}
type UserController struct{
ID int64
Name string
}
// set Before
func (u *UserController) Before(g *gin.Context, x *xorm.Engine) bool {
if u.Name != "zhao"{
return false
}
return true
}
type AdminController struct{
ID int64
Name string
Password string
}
// overwrite Before
func (u *UserController) New(c *gin.Context) {
err := c.BindJSON(u)
if err! = nil{
c.AbortWithError(http.StatusBadRequest, err)
return
}
if u.ID < 200 {
c.AbortWithError(http.StatusBadRequest, errors.New("ID error"))
return
}
c.JSON(200, nil)
}
type User struct{
ID int64
Name string
}
type Admin struct{
ID int64
Name string
Password string
}
```