forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (103 loc) · 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
package main
import (
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
_ "github.com/GoAdminGroup/go-admin/adapter/chi"
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
"github.com/GoAdminGroup/go-admin/engine"
"github.com/GoAdminGroup/go-admin/examples/datamodel"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/language"
"github.com/GoAdminGroup/go-admin/plugins/example"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/chartjs"
"github.com/GoAdminGroup/themes/adminlte"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
eng := engine.Default()
cfg := config.Config{
Env: config.EnvLocal,
Databases: config.DatabaseList{
"default": {
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pwd: "root",
Name: "godmin",
MaxIdleCon: 50,
MaxOpenCon: 150,
Driver: config.DriverMysql,
},
},
UrlPrefix: "admin",
Store: config.Store{
Path: "./uploads",
Prefix: "uploads",
},
Language: language.EN,
IndexUrl: "/",
Debug: true,
ColorScheme: adminlte.ColorschemeSkinBlack,
}
template.AddComp(chartjs.NewChart())
// customize a plugin
examplePlugin := example.NewExample()
// load from golang.Plugin
//
// examplePlugin := plugins.LoadFromPlugin("../datamodel/example.so")
// customize the login page
// example: https://github.com/GoAdminGroup/demo.go-admin.cn/blob/master/main.go#L39
//
// template.AddComp("login", datamodel.LoginPage)
// load config from json file
//
// eng.AddConfigFromJSON("../datamodel/config.json")
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
// example:
//
// "user" => http://localhost:9033/admin/info/user
//
AddGenerator("user", datamodel.GetUserTable).
AddPlugins(examplePlugin).
Use(r); err != nil {
panic(err)
}
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "uploads")
FileServer(r, "/uploads", http.Dir(filesDir))
// you can custom your pages like:
eng.HTML("GET", "/admin", datamodel.GetContent)
go func() {
_ = http.ListenAndServe(":3333", r)
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
eng.MysqlConnection().Close()
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}