提交 355b9cd9 编写于 作者: J Jason

添加before钩子,用于生成UUID,变量检查等

上级 4c451cec
......@@ -179,7 +179,7 @@ func curdApiListById(model string, mod reflect.Type, field string) Handler {
}
}
func curdApiCreate(model string, mod reflect.Type, after hook) Handler {
func curdApiCreate(model string, mod reflect.Type, before hook, after hook) Handler {
return func(writer http.ResponseWriter, request *http.Request) {
data := reflect.New(mod).Interface()
if err := parseBody(request, data); err != nil {
......@@ -205,7 +205,7 @@ func curdApiCreate(model string, mod reflect.Type, after hook) Handler {
}
}
func curdApiModify(model string, mod reflect.Type, after hook) Handler {
func curdApiModify(model string, mod reflect.Type, before hook, after hook) Handler {
return func(writer http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(mux.Vars(request)["id"])
if err != nil {
......@@ -240,7 +240,7 @@ func curdApiModify(model string, mod reflect.Type, after hook) Handler {
}
}
func curdApiDelete(model string, mod reflect.Type, after hook) Handler {
func curdApiDelete(model string, mod reflect.Type, before hook, after hook) Handler {
return func(writer http.ResponseWriter, request *http.Request) {
id, err := strconv.Atoi(mux.Vars(request)["id"])
if err != nil {
......
package api
import (
"git.zgwit.com/zgwit/iot-admin/db"
"git.zgwit.com/zgwit/iot-admin/models"
"github.com/google/uuid"
)
func elementAfterCreated(data interface{}) error {
//element := data.(*models.Element)
return db.DB("element").UpdateField(data, "UUID", uuid.New().String())
func elementBeforeCreate(data interface{}) error {
element := data.(*models.Element)
element.UUID = uuid.New().String()
return nil
}
func elementBeforeDelete(data interface{}) error {
//TODO 检查是否被引用
return nil
}
package api
import (
"git.zgwit.com/zgwit/iot-admin/db"
"git.zgwit.com/zgwit/iot-admin/models"
"github.com/google/uuid"
"net/http"
)
func projectBeforeCreate(data interface{}) error {
project := data.(*models.Project)
project.UUID = uuid.New().String()
return nil
}
func projectAfterCreate(data interface{}) error {
//project := data.(*models.Project)
return db.DB("project").UpdateField(data, "UUID", uuid.New().String())
//TODO 加载实例
return nil
}
func projectAfterModify(data interface{}) error {
......
......@@ -76,9 +76,9 @@ func RegisterRoutes(app *mux.Router) {
model := "tunnel"
app.HandleFunc("/project/{id}/tunnels", curdApiListById(model, mod, "project_id")).Methods("POST")
app.HandleFunc("/tunnels", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/tunnel", curdApiCreate(model, mod, nil)).Methods("POST") //TODO 启动
app.HandleFunc("/tunnel/{id}", curdApiDelete(model, mod, nil)).Methods("DELETE") //TODO 停止
app.HandleFunc("/tunnel/{id}", curdApiModify(model, mod, nil)).Methods("PUT", "POST") //TODO 重新启动
app.HandleFunc("/tunnel", curdApiCreate(model, mod, nil, nil)).Methods("POST") //TODO 启动
app.HandleFunc("/tunnel/{id}", curdApiDelete(model, mod, nil, nil)).Methods("DELETE") //TODO 停止
app.HandleFunc("/tunnel/{id}", curdApiModify(model, mod, nil, nil)).Methods("PUT", "POST") //TODO 重新启动
app.HandleFunc("/tunnel/{id}", curdApiGet(model, mod)).Methods("GET")
app.HandleFunc("/tunnel/{id}/start", tunnelStart).Methods("GET")
......@@ -91,26 +91,26 @@ func RegisterRoutes(app *mux.Router) {
model = "link"
app.HandleFunc("/tunnel/{id}/links", curdApiListById(model, mod, "tunnel_id")).Methods("POST")
app.HandleFunc("/links", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/link/{id}", curdApiDelete(model, mod, nil)).Methods("DELETE") //TODO 停止
app.HandleFunc("/link/{id}", curdApiModify(model, mod, nil)).Methods("PUT", "POST")
app.HandleFunc("/link/{id}", curdApiDelete(model, mod, nil, nil)).Methods("DELETE") //TODO 停止
app.HandleFunc("/link/{id}", curdApiModify(model, mod, nil, nil)).Methods("PUT", "POST")
app.HandleFunc("/link/{id}", curdApiGet(model, mod)).Methods("GET")
//插件管理
mod = reflect.TypeOf(models.Plugin{})
model = "plugin"
app.HandleFunc("/plugins", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/plugin", curdApiCreate(model, mod, nil)).Methods("POST")
app.HandleFunc("/plugin/{id}", curdApiDelete(model, mod, nil)).Methods("DELETE")
app.HandleFunc("/plugin/{id}", curdApiModify(model, mod, nil)).Methods("PUT", "POST")
app.HandleFunc("/plugin", curdApiCreate(model, mod, nil, nil)).Methods("POST")
app.HandleFunc("/plugin/{id}", curdApiDelete(model, mod, nil, nil)).Methods("DELETE")
app.HandleFunc("/plugin/{id}", curdApiModify(model, mod, nil, nil)).Methods("PUT", "POST")
app.HandleFunc("/plugin/{id}", curdApiGet(model, mod)).Methods("GET")
//项目管理
mod = reflect.TypeOf(models.Project{})
model = "project"
app.HandleFunc("/projects", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/project", curdApiCreate(model, mod, projectAfterCreate)).Methods("POST")
app.HandleFunc("/project/{id}", curdApiDelete(model, mod, projectAfterDelete)).Methods("DELETE")
app.HandleFunc("/project/{id}", curdApiModify(model, mod, projectAfterModify)).Methods("PUT", "POST")
app.HandleFunc("/project", curdApiCreate(model, mod, projectBeforeCreate, projectAfterCreate)).Methods("POST")
app.HandleFunc("/project/{id}", curdApiDelete(model, mod, nil, projectAfterDelete)).Methods("DELETE")
app.HandleFunc("/project/{id}", curdApiModify(model, mod, nil, projectAfterModify)).Methods("PUT", "POST")
app.HandleFunc("/project/{id}", curdApiGet(model, mod)).Methods("GET")
//app.HandleFunc("/project/import", projectImport).Methods("POST")
......@@ -122,20 +122,19 @@ func RegisterRoutes(app *mux.Router) {
mod = reflect.TypeOf(models.ProjectTemplate{})
model = "template"
app.HandleFunc("/templates", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/template", curdApiCreate(model, mod, nil)).Methods("POST")
app.HandleFunc("/template/{id}", curdApiDelete(model, mod, nil)).Methods("DELETE")
app.HandleFunc("/template/{id}", curdApiModify(model, mod, nil)).Methods("PUT", "POST")
app.HandleFunc("/template", curdApiCreate(model, mod, nil, nil)).Methods("POST")
app.HandleFunc("/template/{id}", curdApiDelete(model, mod, nil, nil)).Methods("DELETE")
app.HandleFunc("/template/{id}", curdApiModify(model, mod, nil, nil)).Methods("PUT", "POST")
app.HandleFunc("/template/{id}", curdApiGet(model, mod)).Methods("GET")
//元件管理
mod = reflect.TypeOf(models.Element{})
model = "element"
app.HandleFunc("/elements", curdApiList(model, mod)).Methods("POST")
app.HandleFunc("/element", curdApiCreate(model, mod, elementAfterCreated)).Methods("POST")
app.HandleFunc("/element/{id}", curdApiDelete(model, mod, nil)).Methods("DELETE")
app.HandleFunc("/element/{id}", curdApiModify(model, mod, nil)).Methods("PUT", "POST")
app.HandleFunc("/element", curdApiCreate(model, mod, elementBeforeCreate, nil)).Methods("POST")
app.HandleFunc("/element/{id}", curdApiDelete(model, mod, elementBeforeDelete, nil)).Methods("DELETE")
app.HandleFunc("/element/{id}", curdApiModify(model, mod, nil, nil)).Methods("PUT", "POST")
app.HandleFunc("/element/{id}", curdApiGet(model, mod)).Methods("GET")
}
type Reply struct {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册