-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (99 loc) · 2.69 KB
/
Copy pathmain.go
File metadata and controls
124 lines (99 loc) · 2.69 KB
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"flag"
"github.com/gin-contrib/pprof"
_ "github.com/GoAdminGroup/go-admin/adapter/gin" // web framework adapter
ada "github.com/GoAdminGroup/go-admin/adapter/gin"
"github.com/GoAdminGroup/go-admin/modules/config"
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql" // sql driver
"github.com/GoAdminGroup/go-admin/plugins"
_ "github.com/GoAdminGroup/themes/sword" // ui theme
"github.com/GoAdminGroup/go-admin/engine"
"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"
"testmgmt/biz"
"testmgmt/models"
"testmgmt/pages"
"testmgmt/tables"
"github.com/GoAdminGroup/filemanager"
)
type Args struct {
Config string
Upload string
}
var args Args
type FileMode uint32
const ModePerm FileMode = 0644
func (args *Args) InstallFlags() {
flag.StringVar(&args.Config, "c", "./config.json", "Config file")
flag.StringVar(&args.Upload, "u", "./uploads", "Upload file")
}
func init() {
args.InstallFlags()
if _, err := os.Stat(args.Config); os.IsNotExist(err) {
panic(err)
}
if _, err := os.Stat(args.Upload); os.IsNotExist(err) {
os.Mkdir(args.Upload, os.ModePerm)
}
return
}
func main() {
flag.Parse()
startServer()
}
func startServer() {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
r := gin.Default()
pprof.Register(r)
template.AddComp(chartjs.NewChart())
eng := engine.Default()
if err := eng.AddConfigFromJSON(args.Config).
AddGenerators(tables.Generators).
Use(r); err != nil {
panic(err)
}
r.Static("/uploads", args.Upload)
// you can custom your pages like:
r.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/admin/dashboard")
})
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.GetDashBoardContent(ctx)
}
}))
r.GET("/admin/dashboard", ada.Content(pages.GetDashBoardContent))
r.POST("/admin/smoke", biz.StartSmoke)
plug, _ := plugins.FindByName("filemanager")
plug.(*filemanager.FileManager).SetPathValidator(func(path string) error {
if !strings.Contains(path, "testmgmt") {
return errors.New("没有权限")
}
return nil
})
models.Init(eng.MysqlConnection())
listen := fmt.Sprintf(":%d", biz.SERVER_PORT)
err := r.Run(listen)
if err != nil {
panic(err)
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
eng.MysqlConnection().Close()
}