router.go 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package api

import (
	"git.zgwit.com/zgwit/iot-admin/conf"
	"git.zgwit.com/zgwit/iot-admin/models"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
	"reflect"
)

type paramFilter struct {
J
Jason 已提交
13 14
	Key    string        `form:"key"`
	Values []interface{} `form:"value"`
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

type paramSearch struct {
	Offset    int           `form:"offset"`
	Length    int           `form:"length"`
	SortKey   string        `form:"sortKey"`
	SortOrder string        `form:"sortOrder"`
	Filters   []paramFilter `form:"filters"`
	Keyword   string        `form:"keyword"`
}

type paramId struct {
	Id int64 `uri:"id"`
}

type paramId2 struct {
	Id  int64 `uri:"id"`
	Id2 int64 `uri:"id2"`
}

func mustLogin(c *gin.Context) {
	session := sessions.Default(c)
	if user := session.Get("user"); user != nil {
		c.Next()
	} else {
		//TODO 检查OAuth2返回的code,进一步获取用户信息,放置到session中

		c.JSON(http.StatusUnauthorized, gin.H{"ok": false, "error": "Unauthorized"})
		c.Abort()
	}
}

func RegisterRoutes(app *gin.RouterGroup) {

	if conf.Config.SysAdmin.Enable {
		//检查 session,必须登录
		app.Use(mustLogin)
	} else if conf.Config.BaseAuth.Enable {
		//检查HTTP认证
		app.Use(gin.BasicAuth(gin.Accounts(conf.Config.BaseAuth.Users)))
	} else {
		//支持匿名访问
	}

	//TODO 转移至子目录,并使用中间件,检查session及权限
	mod := reflect.TypeOf(models.Tunnel{})
61
	fields := []string{
J
Jason 已提交
62 63 64
		"name", "description", "type", "addr", "timeout",
		"register_enable", "register_regex", "register_min", "register_max",
		"heart_beat_enable", "heart_beat_interval", "heart_beat_content", "heart_beat_is_hex",
65
		"disabled"}
J
Jason 已提交
66
	app.POST("/project/:id/tunnels", curdApiListById(mod, "project_id"))
67 68 69 70
	app.POST("/tunnels", curdApiList(mod))
	app.POST("/tunnel", curdApiCreate(mod, nil))            //TODO 启动
	app.DELETE("/tunnel/:id", curdApiDelete(mod, nil))      //TODO 停止
	app.PUT("/tunnel/:id", curdApiModify(mod, fields, nil)) //TODO 重新启动
71 72 73 74 75 76 77 78 79
	app.GET("/tunnel/:id", curdApiGet(mod))

	app.GET("/tunnel/:id/start", tunnelStart)
	app.GET("/tunnel/:id/stop", tunnelStop)

	//app.POST("/channel/:id/links")

	//连接管理
	mod = reflect.TypeOf(models.Link{})
80
	fields = []string{"name"}
J
Jason 已提交
81
	app.POST("/tunnel/:id/links", curdApiListById(mod, "tunnel_id"))
82
	app.POST("/links", curdApiList(mod))
J
Jason 已提交
83
	app.DELETE("/link/:id", curdApiDelete(mod, nil)) //TODO 停止
84
	app.PUT("/link/:id", curdApiModify(mod, fields, nil))
85 86
	app.GET("/link/:id", curdApiGet(mod))

J
Jason 已提交
87 88
	mod = reflect.TypeOf(models.Device{})
	fields = []string{"name"}
J
Jason 已提交
89
	app.POST("/project/:id/devices", curdApiListById(mod, "project_id"))
J
Jason 已提交
90 91 92 93 94 95 96 97
	app.POST("/devices", curdApiList(mod))
	app.POST("/device", curdApiCreate(mod, nil))
	app.DELETE("/device/:id", curdApiDelete(mod, nil))
	app.PUT("/device/:id", curdApiModify(mod, fields, nil))
	app.GET("/device/:id", curdApiGet(mod))

	mod = reflect.TypeOf(models.Location{})
	fields = []string{"name"}
J
Jason 已提交
98 99
	app.POST("/device/:id/locations", curdApiListById(mod, "device_id"))
	//app.POST("/locations", curdApiList(mod))
J
Jason 已提交
100 101 102 103 104
	//app.POST("/location", curdApiCreate(mod, nil))
	app.DELETE("/location/:id", curdApiDelete(mod, nil))
	//app.PUT("/location/:id", curdApiModify(mod, fields, nil))
	app.GET("/location/:id", curdApiGet(mod))

105 106
	//插件管理
	mod = reflect.TypeOf(models.Plugin{})
107
	fields = []string{"name"}
108 109 110
	app.POST("/plugins", curdApiList(mod))
	app.POST("/plugin", curdApiCreate(mod, nil))
	app.DELETE("/plugin/:id", curdApiDelete(mod, nil))
111
	app.PUT("/plugin/:id", curdApiModify(mod, fields, nil))
112 113 114
	app.GET("/plugin/:id", curdApiGet(mod))

	//模型管理
115
	mod = reflect.TypeOf(models.Project{})
116
	fields = []string{"name"}
117 118 119
	app.POST("/projects", curdApiList(mod))
	app.POST("/project", curdApiCreate(mod, nil))
	app.DELETE("/project/:id", curdApiDelete(mod, nil))
120
	app.PUT("/project/:id", curdApiModify(mod, fields, nil))
121 122 123 124 125 126 127 128
	app.GET("/project/:id", curdApiGet(mod))

	//app.GET("/project/:id/tunnels", nop)
	//app.GET("/project/:id/variables", nop)
	//app.GET("/project/:id/batches", nop)
	//app.GET("/project/:id/jobs", nop)
	//app.GET("/project/:id/strategies", nop)

J
Jason 已提交
129
	app.POST("/project-import", projectImport)
130 131 132
	app.GET("/project/:id/export", projectExport)
	app.GET("/project/:id/deploy", projectDeploy)

J
Jason 已提交
133
	
134 135
	mod = reflect.TypeOf(models.ProjectElement{})
	fields = []string{"name"}
J
Jason 已提交
136 137
	app.POST("/project/:id/elements", curdApiListById(mod, "project_id"))
	//app.POST("/project-elements", curdApiList(mod))
138 139 140 141
	app.POST("/project-element", curdApiCreate(mod, nil))
	app.DELETE("/project-element/:id", curdApiDelete(mod, nil))
	app.PUT("/project-element/:id", curdApiModify(mod, fields, nil))
	app.GET("/project-element/:id", curdApiGet(mod))
142 143

	mod = reflect.TypeOf(models.ProjectJob{})
144
	fields = []string{"name"}
J
Jason 已提交
145 146
	app.POST("/project/:id/jobs", curdApiListById(mod, "project_id"))
	//app.POST("/project-jobs", curdApiList(mod))
147 148
	app.POST("/project-job", curdApiCreate(mod, nil))
	app.DELETE("/project-job/:id", curdApiDelete(mod, nil))
149
	app.PUT("/project-job/:id", curdApiModify(mod, fields, nil))
150 151 152
	app.GET("/project-job/:id", curdApiGet(mod))

	mod = reflect.TypeOf(models.ProjectStrategy{})
153
	fields = []string{"name"}
J
Jason 已提交
154 155
	app.POST("/project/:id/strategies", curdApiListById(mod, "project_id"))
	//app.POST("/project-strategies", curdApiList(mod))
156 157
	app.POST("/project-strategy", curdApiCreate(mod, nil))
	app.DELETE("/project-strategy/:id", curdApiDelete(mod, nil))
158
	app.PUT("/project-strategy/:id", curdApiModify(mod, fields, nil))
159
	app.GET("/project-strategy/:id", curdApiGet(mod))
160 161 162 163 164 165 166 167 168 169 170 171 172

	//元件管理
	mod = reflect.TypeOf(models.Element{})
	fields = []string{"name"}
	app.POST("/elements", curdApiList(mod))
	app.POST("/element", curdApiCreate(mod, nil))
	app.DELETE("/element/:id", curdApiDelete(mod, nil))
	app.PUT("/element/:id", curdApiModify(mod, fields, nil))
	app.GET("/element/:id", curdApiGet(mod))

	//元件变量
	mod = reflect.TypeOf(models.ElementVariable{})
	fields = []string{"name"}
J
Jason 已提交
173 174
	app.POST("/element/:id/variables", curdApiListById(mod, "element_id"))
	//app.POST("/element-variables", curdApiList(mod))
175 176 177 178 179 180 181 182
	app.POST("/element-variable", curdApiCreate(mod, nil))
	app.DELETE("/element-variable/:id", curdApiDelete(mod, nil))
	app.PUT("/element-variable/:id", curdApiModify(mod, fields, nil))
	app.GET("/element-variable/:id", curdApiGet(mod))

	//元件批量操作
	mod = reflect.TypeOf(models.ElementBatch{})
	fields = []string{"name"}
J
Jason 已提交
183 184
	app.POST("/element/:id/batches", curdApiListById(mod, "element_id"))
	//app.POST("/element-batches", curdApiList(mod))
185 186 187 188 189
	app.POST("/element-batch", curdApiCreate(mod, nil))
	app.DELETE("/element-batch/:id", curdApiDelete(mod, nil))
	app.PUT("/element-batch/:id", curdApiModify(mod, fields, nil))
	app.GET("/element-batch/:id", curdApiGet(mod))

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

func replyOk(ctx *gin.Context, data interface{}) {
	ctx.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": data,
	})
}

func replyFail(ctx *gin.Context, err string) {
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err,
	})
}

func replyError(ctx *gin.Context, err error) {
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err.Error(),
	})
}

func nop(c *gin.Context) {
	c.String(http.StatusForbidden, "Unsupported")
}