Skip to content

Commit

Permalink
Merge pull request smallnest#36 from flyingtime/master
Browse files Browse the repository at this point in the history
add gin interface
  • Loading branch information
smallnest authored Mar 27, 2020
2 parents abd3134 + d8e990e commit b110a3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
47 changes: 34 additions & 13 deletions template/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ var ControllerTmpl = `package api
import (
"net/http"
"{{.PackageName}}"
"github.com/gin-gonic/gin"
"github.com/julienschmidt/httprouter"
"github.com/smallnest/gen/dbmeta"
"{{.PackageName}}"
)
func config{{pluralize .StructName}}Router(router *httprouter.Router) {
Expand All @@ -18,31 +20,46 @@ func config{{pluralize .StructName}}Router(router *httprouter.Router) {
router.DELETE("/{{pluralize .StructName | toLower}}/:id", Delete{{.StructName}})
}
func configGin{{pluralize .StructName}}Router(router gin.IRoutes) {
router.GET("/{{pluralize .StructName | toLower}}", ConverHttprouterToGin(GetAll{{pluralize .StructName}}))
router.POST("/{{pluralize .StructName | toLower}}", ConverHttprouterToGin(Add{{.StructName}}))
router.GET("/{{pluralize .StructName | toLower}}/:id", ConverHttprouterToGin(Get{{.StructName}}))
router.PUT("/{{pluralize .StructName | toLower}}/:id", ConverHttprouterToGin(Update{{.StructName}}))
router.DELETE("/{{pluralize .StructName | toLower}}/:id", ConverHttprouterToGin(Delete{{.StructName}}))
}
func GetAll{{pluralize .StructName}}(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
page, err := readInt(r, "page", 1)
if err != nil || page < 1 {
page, err := readInt(r, "page", 0)
if err != nil || page < 0 {
http.Error(w, err.Error(), http.StatusBadRequest)
}
pagesize, err := readInt(r, "pagesize", 20)
if err != nil || pagesize <= 0 {
http.Error(w, err.Error(), http.StatusBadRequest)
}
offset := (page - 1) * pagesize
order := r.FormValue("order")
{{pluralize .StructName | toLower}} := []*model.{{.StructName}}{}
{{pluralize .StructName | toLower}}_orm := DB.Model(&model.{{.StructName}}{})
if page > 0 {
pagesize, err := readInt(r, "pagesize", 20)
if err != nil || pagesize <= 0 {
http.Error(w, err.Error(), http.StatusBadRequest)
}
offset := (page - 1) * pagesize
{{pluralize .StructName | toLower}}_orm = {{pluralize .StructName | toLower}}_orm.Offset(offset).Limit(pagesize)
}
if order != "" {
err = DB.Model(&model.{{.StructName}}{}).Order(order).Offset(offset).Limit(pagesize).Find(&{{pluralize .StructName | toLower}}).Error
} else {
err = DB.Model(&model.{{.StructName}}{}).Offset(offset).Limit(pagesize).Find(&{{pluralize .StructName | toLower}}).Error
{{pluralize .StructName | toLower}}_orm = {{pluralize .StructName | toLower}}_orm.Order(order)
}
if err != nil {
if err = {{pluralize .StructName | toLower}}_orm.Find(&{{pluralize .StructName | toLower}}).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, &{{pluralize .StructName | toLower}})
}
Expand All @@ -53,6 +70,7 @@ func Get{{.StructName}}(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
http.NotFound(w, r)
return
}
writeJSON(w, {{.StructName | toLower}})
}
Expand All @@ -67,6 +85,7 @@ func Add{{.StructName}}(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, {{.StructName | toLower}})
}
Expand Down Expand Up @@ -94,6 +113,7 @@ func Update{{.StructName}}(w http.ResponseWriter, r *http.Request, ps httprouter
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, {{.StructName | toLower}})
}
Expand All @@ -109,6 +129,7 @@ func Delete{{.StructName}}(w http.ResponseWriter, r *http.Request, ps httprouter
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
`
25 changes: 25 additions & 0 deletions template/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"io/ioutil"
"net/http"
"strconv"
"unsafe"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/julienschmidt/httprouter"
)
Expand All @@ -30,11 +32,33 @@ func ConfigRouter() http.Handler {
return router
}
func ConfigGinRouter(router gin.IRoutes) {
{{range .}}configGin{{pluralize .}}Router(router)
{{end}}
return
}
func ConverHttprouterToGin(f httprouter.Handle) gin.HandlerFunc {
return func(c *gin.Context) {
var params httprouter.Params
_len := len(c.Params)
if _len == 0 {
params = nil
} else {
params = ((*[1 << 10]httprouter.Param)(unsafe.Pointer(&c.Params[0])))[:_len]
}
f(c.Writer, c.Request, params)
}
}
func readInt(r *http.Request, param string, v int64) (int64, error) {
p := r.FormValue(param)
if p == "" {
return v, nil
}
return strconv.ParseInt(p, 10, 64)
}
Expand All @@ -50,6 +74,7 @@ func readJSON(r *http.Request, v interface{}) error {
if err != nil {
return err
}
return json.Unmarshal(buf, v)
}
`

0 comments on commit b110a3b

Please sign in to comment.