configworker/main.go

123 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-01-01 21:33:26 +00:00
package main
2020-01-02 18:12:31 +00:00
import (
"log"
"net/http"
2020-01-06 20:25:45 +00:00
"time"
2020-01-01 21:33:26 +00:00
2020-01-02 18:12:31 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
const (
2020-01-06 10:09:49 +00:00
// SessionKey Session主Key
SessionKey = "token"
2020-01-02 18:12:31 +00:00
// SessionUser 用户登录的Session标签
2020-01-06 10:09:49 +00:00
SessionUser = "user"
2020-01-02 18:12:31 +00:00
)
func auth(ctx *gin.Context) {
2020-01-06 10:09:49 +00:00
2020-01-02 18:12:31 +00:00
if ctx.Request.RequestURI != "/api/login" {
2020-01-06 10:09:49 +00:00
2020-01-02 18:12:31 +00:00
session := sessions.Default(ctx)
2020-01-06 10:09:49 +00:00
2020-01-06 20:25:45 +00:00
if user := session.Get(SessionUser); user == nil {
2020-01-06 10:09:49 +00:00
session.Clear()
session.Save()
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "需要登录"})
2020-01-02 18:12:31 +00:00
return
2020-01-06 20:25:45 +00:00
} else if user.(*User).Expired < time.Now().Unix() {
session.Clear()
session.Save()
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "账号过期"})
return
2020-01-02 18:12:31 +00:00
}
}
2020-01-06 10:09:49 +00:00
2020-01-02 18:12:31 +00:00
ctx.Next()
}
2020-01-01 21:33:26 +00:00
func login(ctx *gin.Context) {
2020-01-06 20:25:45 +00:00
userName := ctx.PostForm("user")
2020-01-06 10:09:49 +00:00
session := sessions.Default(ctx)
2020-01-06 20:25:45 +00:00
if userName == "" {
2020-01-06 10:09:49 +00:00
if tokenUser := session.Get(SessionUser); tokenUser != nil {
ctx.JSON(http.StatusOK, gin.H{"user": tokenUser})
return
}
}
2020-01-02 18:12:31 +00:00
2020-01-06 20:25:45 +00:00
if realPassword, ok := GlobalConfig.GetUser(userName); ok {
2020-01-02 18:12:31 +00:00
pwd := ctx.PostForm("pwd")
if realPassword == pwd {
2020-01-06 20:25:45 +00:00
user := &User{Name: userName,
Expired: time.Now().Unix() + 15,
ConfigPath: "",
Config: nil,
}
2020-01-02 18:12:31 +00:00
session.Set(SessionUser, user)
session.Save()
2020-01-06 10:09:49 +00:00
ctx.JSON(http.StatusOK, gin.H{"message": "登录成功"})
2020-01-02 18:12:31 +00:00
return
}
2020-01-06 10:09:49 +00:00
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "密码错误"})
2020-01-02 18:12:31 +00:00
return
}
2020-01-06 10:09:49 +00:00
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "不存在该用户"})
return
2020-01-03 10:16:42 +00:00
// ctx.Redirect(http.StatusOK, "/worker")
2020-01-06 10:09:49 +00:00
// ctx.Next()
}
2020-01-02 18:12:31 +00:00
2020-01-06 10:09:49 +00:00
func logout(ctx *gin.Context) {
session := sessions.Default(ctx)
session.Clear()
session.Save()
2020-01-06 20:25:45 +00:00
ctx.JSON(http.StatusOK, gin.H{"message": "退出登录成功"})
2020-01-01 21:33:26 +00:00
}
2020-01-05 18:38:55 +00:00
func userConfig(ctx *gin.Context) {
2020-01-06 10:09:49 +00:00
2020-01-05 18:38:55 +00:00
session := sessions.Default(ctx)
2020-01-07 03:52:50 +00:00
userSession := session.Get(SessionUser)
2020-01-06 10:09:49 +00:00
// log.Println(user)
2020-01-07 03:52:50 +00:00
if userSession == nil {
2020-01-05 18:38:55 +00:00
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "权限错误"})
return
}
2020-01-07 03:52:50 +00:00
user := userSession.(*User)
user.Config = map[string]string{"a": "b", "ocean-app-id": "dasdsa"}
ctx.JSON(http.StatusOK, gin.H{"message": "获取配置成功", "user": user.Name, "config": user.Config})
2020-01-05 18:38:55 +00:00
}
2020-01-01 21:33:26 +00:00
func main() {
2020-01-02 18:12:31 +00:00
2020-01-01 21:33:26 +00:00
eg := gin.New()
2020-01-02 18:12:31 +00:00
2020-01-06 10:09:49 +00:00
eg.Use(sessions.Sessions(SessionKey, cookie.NewStore([]byte("yame"))))
2020-01-02 18:12:31 +00:00
eg.Use(auth)
eg.POST("/api/login", login)
2020-01-05 18:38:55 +00:00
eg.POST("/api/user/config", userConfig)
2020-01-06 10:09:49 +00:00
eg.POST("/api/user/logout", logout)
2020-01-01 21:33:26 +00:00
log.Fatal(eg.Run(":3001"))
}