forked from guyan0319/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (126 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"go-admin/conf"
"go-admin/ctrl"
"go-admin/ctrl/menu"
"go-admin/ctrl/role"
"go-admin/ctrl/user"
_ "go-admin/docs"
"go-admin/models"
"go-admin/modules/cache"
"go-admin/modules/response"
"go-admin/public/common"
"log"
"net/url"
)
/*
* 首页
* author Guo Zhiqiang
* datetime 2019/10/21 11:53
*/
func main() {
Load() //载入配置
//gin.SetMode(gin.DebugMode)//开发环境
gin.SetMode(gin.ReleaseMode) //线上环境
r := gin.Default()
store, _ := redis.NewStoreWithPool(cache.RedisClient, []byte("secret"))
r.Use(sessions.Sessions("gosession", store))
r.Use(cors.New(GetCorsConfig()))//跨域
//r.Use(cors.Default())//默认跨域
r.Use(Auth())
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.GET("/", ctrl.Index)
r.GET("/info", user.Info)
r.GET("/routes",menu.List)
r.GET("/dashboard",menu.Dashboard)
r.GET("/role/list",menu.Roles)
r.GET("/menu",menu.Index)
r.POST("/menu",menu.Create)
r.PUT("/menu",menu.Edit)
r.DELETE("/menu",menu.Delete)
r.GET("/user",user.Index)
r.GET("/user/detail",user.Detail)
r.POST("/user/create",user.Create)
r.POST("/user/edit",user.Edit)
r.POST("/user/repasswd",user.Repasswd)
r.GET("/user/delete",user.Delete)
r.POST("/role/delete/:name",role.DeleteRole)
r.POST("/role/update",role.UpdateRole)
r.POST("/role/add",role.AddRole)
r.GET("/role/index",role.Index)
r.POST("/logout", user.Logout)
r.POST("/login", user.Login)
r.POST("/reg", user.Reg)
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run(":8090") // listen and serve on 0.0.0.0:8080
}
func Load() {
c := conf.Config{}
c.Routes=[]string{"/login","/role/index","/info","/dashboard","/logout"}
conf.Set(c)
}
func GetCorsConfig() cors.Config {
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:9529","http://localhost:9528","http://localhost:9527","http://localhost"}
config.AllowMethods = []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}
config.AllowCredentials = true
config.AllowHeaders = []string{"x-requested-with", "Content-Type", "AccessToken", "X-CSRF-Token","X-Token", "Authorization","token"}
return config
}
func Auth() gin.HandlerFunc{
return func(c *gin.Context) {
u,err:= url.Parse(c.Request.RequestURI)
if err != nil {
panic(err)
}
if common.InArrayString(u.Path,&conf.Cfg.Routes) {
//c.Next()
return
}
session := sessions.Default(c)
v := session.Get(conf.Cfg.Token)
if v==nil {
c.Abort()
response.ShowError(c,"nologin")
return
}
uid:=session.Get(v)
users := models.SystemUser{Id:uid.(int),Status:1}
has:=users.GetRow()
if !has {
c.Abort()
response.ShowError(c,"user_error")
return
}
//特殊账号
if users.Name==conf.Cfg.Super {
c.Next()
return
}
menuModel:=models.SystemMenu{}
menuMap,err:=menuModel.GetRouteByUid(uid)
if err!=nil {
c.Abort()
response.ShowError(c,"unauthorized")
return
}
if _,ok:=menuMap[u.Path] ;!ok{
c.Abort()
response.ShowError(c,"unauthorized")
return
}
// access the status we are sending
status := c.Writer.Status()
log.Println(status) //状态 200
}
}