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

import (
J
二种  
Jason 已提交
4
	"encoding/gob"
J
Jason 已提交
5
	"github.com/gin-contrib/sessions"
J
二种  
Jason 已提交
6
	"github.com/gin-contrib/sessions/memstore"
J
Jason 已提交
7
	"github.com/gin-gonic/gin"
J
顶替  
Jason 已提交
8
	"github.com/zgwit/dtu-admin/types"
J
Jason 已提交
9 10 11 12 13 14 15 16 17 18 19
	"net/http"
)

type paramSearch struct {
	Offset    int    `form:"offset"`
	Length    int    `form:"length"`
	SortKey   string `form:"sortKey"`
	SortOrder string `form:"sortOrder"`
}

type paramId struct {
J
Jason 已提交
20
	Id int `uri:"id"`
J
Jason 已提交
21 22 23
}

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

func RegisterRoutes(app *gin.RouterGroup) {
J
二种  
Jason 已提交
29
	//注册 User类型
J
顶替  
Jason 已提交
30
	gob.Register(&types.User{})
J
二种  
Jason 已提交
31 32
	//启用session
	app.Use(sessions.Sessions("dtu-admin", memstore.NewStore([]byte("dtu-admin-secret"))))
J
Jason 已提交
33

J
 
Jason 已提交
34
	app.POST("/login", authLogin)
J
 
Jason 已提交
35

J
顶替  
Jason 已提交
36
	//检查登录状态
J
二种  
Jason 已提交
37 38 39 40 41 42 43 44
	app.Use(func(c *gin.Context) {
		session := sessions.Default(c)
		if user := session.Get("user"); user != nil {
			c.Next()
		} else {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			c.Abort()
		}
J
 
Jason 已提交
45 46
	})

J
 
Jason 已提交
47 48 49 50
	app.DELETE("/logout", authLogout)
	app.POST("/password", authPassword)

	//TODO 转移至子目录,并使用中间件,检查session及权限
J
顶替  
Jason 已提交
51 52 53 54 55 56 57 58
	app.GET("/channels", channels)
	//app.POST("/channels")
	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 已提交
59 60 61 62 63

	app.GET("/channel/:id/connections")
	app.POST("/channel/:id/connections")
	app.DELETE("/channel/:id/connection/:id2") //关闭连接
	app.GET("/channel/:id/connection/:id2/statistic")
J
Jason 已提交
64
	app.GET("/channel/:id/connection/:id2/pipe") //转Websocket透传
J
 
Jason 已提交
65

J
Jason 已提交
66 67
}

J
顶替  
Jason 已提交
68
func replyOk(ctx *gin.Context, data interface{}) {
J
Jason 已提交
69 70 71 72 73 74
	ctx.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": data,
	})
}

J
顶替  
Jason 已提交
75
func replyFail(ctx *gin.Context, err string) {
J
Jason 已提交
76 77 78 79 80 81
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err,
	})
}

J
顶替  
Jason 已提交
82 83 84 85 86 87 88 89
func replyError(ctx *gin.Context, err error) {
	ctx.JSON(http.StatusOK, gin.H{
		"ok":    false,
		"error": err.Error(),
	})
}


J
Jason 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
func nop(c *gin.Context) {
	c.String(http.StatusForbidden, "Unsupported")
}

func mustLogin(c *gin.Context) {
	//测试
	session := sessions.Default(c)
	if user := session.Get("user"); user != nil {
		c.Next()
	} else {
		//c.Redirect(http.StatusSeeOther, "/login")
		c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
		c.Abort()
	}
}