-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.go
197 lines (174 loc) · 6.33 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/GoAdminGroup/filemanager"
"github.com/GoAdminGroup/go-admin/plugins"
"log"
"net/http"
"os"
"os/signal"
"time"
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
_ "github.com/GoAdminGroup/themes/adminlte"
_ "github.com/GoAdminGroup/themes/sword"
"github.com/GoAdminGroup/components/echarts"
"github.com/GoAdminGroup/demo/login"
"github.com/GoAdminGroup/demo/pages"
"github.com/GoAdminGroup/demo/tables"
ada "github.com/GoAdminGroup/go-admin/adapter/gin"
adminContext "github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/engine"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/chartjs"
"github.com/GoAdminGroup/go-admin/template/types"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
eng := engine.Default()
var loginComps = map[string]template.Component{
"default": login.Get(),
}
template.AddLoginComp(loginComps["default"])
template.AddComp(chartjs.NewChart())
template.AddComp(echarts.NewChart())
rootPath := "/data/www/go-admin"
//rootPath = "."
cfg := config.ReadFromJson(rootPath + "/config.json")
cfg.CustomFootHtml = template.HTML(`<div style="display:none;">
<script type="text/javascript" src="https://s9.cnzz.com/z_stat.php?id=1278156902&web_id=1278156902"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-103003647-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-103003647-2');
</script>
</div>`)
cfg.CustomHeadHtml = template.HTML(`<link rel="icon" type="image/png" sizes="32x32" href="//quick.go-admin.cn/official/assets/imgs/icons.ico/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="//quick.go-admin.cn/official/assets/imgs/icons.ico/favicon-64x64.png">
<link rel="icon" type="image/png" sizes="16x16" href="//quick.go-admin.cn/official/assets/imgs/icons.ico/favicon-16x16.png">`)
cfg.Animation = config.PageAnimation{
Type: "fadeInUp",
Duration: 0.9,
}
cfg.AddUpdateProcessFn(func(values form.Values) (values2 form.Values, e error) {
if values.Get("theme") == "adminlte" && values.Get("asset_url") == "//quick.go-admin.cn/demo/sword" {
values.Add("asset_url", "//quick.go-admin.cn/demo")
}
if values.Get("theme") == "sword" && values.Get("asset_url") == "//quick.go-admin.cn/demo" {
values.Add("asset_url", "//quick.go-admin.cn/demo/sword")
}
if values.Get("site_off") == "true" || values.Get("no_limit_login_ip") == "false" {
return nil, errors.New("不允许的操作")
}
if values.Get("login_title") != "GoAdmin" {
return nil, errors.New("不允许的操作")
}
if values.Get("custom_head_html") != string(cfg.CustomHeadHtml) {
return nil, errors.New("不允许的操作")
}
if values.Get("custom_foot_html") != string(cfg.CustomFootHtml) {
return nil, errors.New("不允许的操作")
}
if values.Get("footer_info") != "" || values.Get("login_logo") != string(cfg.LoginLogo) ||
values.Get("logo") != string(cfg.Logo) || values.Get("mini_logo") != string(cfg.MiniLogo) {
return nil, errors.New("不允许的操作")
}
if e := values.Get("extra"); e != "" {
var extra = make(map[string]interface{})
err := json.Unmarshal([]byte(e), &extra)
if err != nil && extra["login_theme"] != "" {
if comp, ok := loginComps[extra["login_theme"].(string)]; ok {
template.AddLoginComp(comp)
}
}
}
return values, nil
})
//cfg.HideConfigCenterEntrance = true
if err := eng.AddConfig(&cfg).
AddGenerators(tables.Generators).
AddGenerator("user", tables.GetUserTable).
//AddPlugins(filemanager.NewFileManagerWithConfig(filemanager.Config{
// Path: "/data/www/go-admin/fm_example",
// AllowDelete: false,
// AllowUpload: true,
// AllowDownload: true,
// AllowRename: true,
// AllowMove: true,
//}), librarian.NewLibrarianWithConfig(librarian.Config{
// Path: "/data/www/go-admin/fm_example/markdown",
// BuildMenu: false,
// Prefix: "librarian",
//})).
//AddNavButtons("网站信息", "", action.PopUp("/website/info", "网站信息",
// func(ctx *adminContext.Context) (success bool, msg string, data interface{}) {
// return true, "ok", `<p>网站由 <a href="https://github.com/chenhg5">cg33<a/> 创造</p>`
// })).
//AddNavButtons("用户管理", "", action.Jump("/admin/info/manager")).
Use(r); err != nil {
panic(err)
}
r.Static("/uploads", rootPath+"/uploads")
// you can custom your pages like:
r.GET("/admin", ada.Content(func(ctx *gin.Context) (panel types.Panel, e error) {
if config.GetTheme() == "adminlte" {
return pages.GetDashBoardContent(ctx)
} else {
return pages.GetDashBoard2Content(ctx)
}
}))
r.GET("/admin/echarts", ada.Content(pages.GetDashBoard3Content))
r.GET("/admin/table", ada.Content(pages.GetTableContent))
r.GET("/admin/form1", ada.Content(pages.GetForm1Content))
eng.Data("POST", "/admin/form/update", func(ctx *adminContext.Context) {
fmt.Println("ctx.PostForm()", ctx.PostForm())
ctx.PjaxUrl("/admin")
})
eng.Data("POST", "/admin/popup/form", func(ctx *adminContext.Context) {
ctx.JSON(http.StatusOK, map[string]interface{}{
"code": 200,
"msg": "ok",
"data": map[string]string{
"url": "/admin/info/profile",
},
})
})
r.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/admin")
})
plug, _ := plugins.FindByName("filemanager")
plug.(*filemanager.FileManager).SetPathValidator(func(path string) error {
if path != "/data/www/go-admin/fm_example" {
return errors.New("没有权限")
}
return nil
})
srv := &http.Server{
Addr: ":9033",
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
log.Print("closing database connection")
eng.MysqlConnection().Close()
}