router.go 3.5 KB
Newer Older
J
Jason 已提交
1
package api
J
Jason 已提交
2 3

import (
J
Jason 已提交
4
	"git.zgwit.com/zgwit/iot-admin/internal/conf"
J
Jason 已提交
5 6 7 8 9
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
)

J
Jason 已提交
10 11 12 13 14
type paramFilter struct {
	Key   string   `form:"key"`
	Value []string `form:"value"`
}

J
Jason 已提交
15
type paramSearch struct {
J
Jason 已提交
16 17 18 19 20 21
	Offset    int           `form:"offset"`
	Length    int           `form:"length"`
	SortKey   string        `form:"sortKey"`
	SortOrder string        `form:"sortOrder"`
	Filters   []paramFilter `form:"filters"`
	Keyword   string        `form:"keyword"`
J
Jason 已提交
22 23 24
}

type paramId struct {
25
	Id int `uri:"id"`
J
Jason 已提交
26 27 28
}

type paramId2 struct {
29 30
	Id  int `uri:"id"`
	Id2 int `uri:"id2"`
J
Jason 已提交
31 32
}

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

40 41 42 43 44
		c.JSON(http.StatusUnauthorized, gin.H{"ok": false, "error": "Unauthorized"})
		c.Abort()
	}
}

J
Jason 已提交
45
func RegisterRoutes(app *gin.RouterGroup) {
46

J
Jason 已提交
47 48 49 50
	if conf.Config.SysAdmin.Enable {
		//检查 session,必须登录
		app.Use(mustLogin)

J
Jason 已提交
51
		app.GET("/mqtt", mqtt)
J
Jason 已提交
52
	} else if conf.Config.BaseAuth.Enable {
J
Jason 已提交
53
		app.GET("/mqtt", mqtt)
J
Jason 已提交
54 55 56 57 58 59

		//检查HTTP认证
		app.Use(gin.BasicAuth(gin.Accounts(conf.Config.BaseAuth.Users)))
	} else {
		//支持匿名访问
	}
J
 
Jason 已提交
60 61

	//TODO 转移至子目录,并使用中间件,检查session及权限
J
Jason 已提交
62
	app.POST("/channels", channels)
J
Jason 已提交
63 64 65 66 67 68
	app.POST("/channel", channelCreate)
	app.DELETE("/channel/:id", channelDelete)
	app.PUT("/channel/:id", channelModify)
	app.GET("/channel/:id", channelGet)
	app.GET("/channel/:id/start", channelStart)
	app.GET("/channel/:id/stop", channelStop)
J
Jason 已提交
69

J
Jason 已提交
70
	//app.POST("/channel/:id/links")
71

J
Jason 已提交
72
	//连接管理
J
Jason 已提交
73
	app.POST("/links", links)
74 75 76
	app.DELETE("/link/:id", linkDelete)
	app.PUT("/link/:id", linkModify)
	app.GET("/link/:id", linkGet)
J
 
Jason 已提交
77

J
Jason 已提交
78 79 80 81 82 83 84
	//插件管理
	app.POST("/plugins", plugins)
	app.POST("/plugin", pluginCreate)
	app.DELETE("/plugin/:id", pluginDelete)
	app.PUT("/plugin/:id", pluginModify)
	app.GET("/plugin/:id", pluginGet)

J
Jason 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	//模型管理
	app.POST("/models", nop)
	app.POST("/model", nop)
	app.DELETE("/model/:id", nop)
	app.PUT("/model/:id", nop)
	app.GET("/model/:id", nop)

	app.POST("/model/:id/tunnels", nop)
	app.POST("/model/:id/variables", nop)
	app.POST("/model/:id/batches", nop)
	app.POST("/model/:id/jobs", nop)
	app.POST("/model/:id/strategies", nop)

	app.POST("/model-import", modelImport)
	app.GET("/model-export/:id", modelExport)

	app.POST("/tunnels", nop)
	app.POST("/tunnel", nop)
	app.DELETE("/tunnel/:id", nop)
	app.PUT("/tunnel/:id", nop)
	app.GET("/tunnel/:id", nop)

	app.POST("/variables", nop)
	app.POST("/variable", nop)
	app.DELETE("/variable/:id", nop)
	app.PUT("/variable/:id", nop)
	app.GET("/variable/:id", nop)

	app.POST("/batches", nop)
	app.POST("/batch", nop)
	app.DELETE("/batch/:id", nop)
	app.PUT("/batch/:id", nop)
	app.GET("/batch/:id", nop)

	app.POST("/jobs", nop)
	app.POST("/job", nop)
	app.DELETE("/job/:id", nop)
	app.PUT("/job/:id", nop)
	app.GET("/job/:id", nop)

	app.POST("/strategies", nop)
	app.POST("/strategy", nop)
	app.DELETE("/strategy/:id", nop)
	app.PUT("/strategy/:id", nop)
	app.GET("/strategy/:id", nop)

J
Jason 已提交
131 132
}

J
顶替  
Jason 已提交
133
func replyOk(ctx *gin.Context, data interface{}) {
J
Jason 已提交
134 135 136 137 138 139
	ctx.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": data,
	})
}

J
顶替  
Jason 已提交
140
func replyFail(ctx *gin.Context, err string) {
J
Jason 已提交
141 142 143 144 145 146
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err,
	})
}

J
顶替  
Jason 已提交
147 148 149 150 151 152 153
func replyError(ctx *gin.Context, err error) {
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err.Error(),
	})
}

J
Jason 已提交
154 155 156
func nop(c *gin.Context) {
	c.String(http.StatusForbidden, "Unsupported")
}