-
Notifications
You must be signed in to change notification settings - Fork 19
/
project_web.go
275 lines (240 loc) · 7.49 KB
/
project_web.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
_ "embed"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/GoAdminGroup/go-admin/modules/utils"
)
//go:embed templates/installation/installation.tmpl
var projectWebTmpl string
func buildProjectWeb(port string) {
var startChan = make(chan struct{})
type InstallationPage struct {
Version string
GoVer string
Port string
CSRFToken string
CurrentLang string
GOOS string
DefModuleName string
}
var tokens = make([]string, 0)
rootPath, err := os.Getwd()
if err != nil {
rootPath = "."
} else {
rootPath = filepath.ToSlash(rootPath)
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
defModuleName := filepath.Base(dir)
go func(sc chan struct{}) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
lang := defaultLang
if r.URL.Query().Get("lang") != "" {
lang = r.URL.Query().Get("lang")
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
t, err := template.New("web_installation").Funcs(map[string]interface{}{
"local": local(lang),
}).Parse(projectWebTmpl)
if err != nil {
fmt.Println("get web installation page template error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
curLang := "web.simplified chinese"
if lang == "en" {
curLang = "web.english"
}
tk := utils.Uuid(25)
tokens = append(tokens, tk)
err = t.Execute(w, InstallationPage{
Version: system.Version(),
GoVer: strings.Title(runtime.Version()),
Port: port,
CSRFToken: tk,
CurrentLang: curLang,
GOOS: runtime.GOOS,
DefModuleName: defModuleName,
})
if err != nil {
fmt.Println("get web installation page template error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
})
var checkEmpty = func(list []string, r *http.Request) (string, bool) {
for i := 0; i < len(list); i++ {
if r.PostFormValue(list[i]) == "" {
return list[i], false
}
}
return "", true
}
http.HandleFunc("/install", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.Header().Add("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 400, "msg": "wrong method"}`))
return
}
lang := defaultLang
if r.URL.Query().Get("lang") != "" {
lang = r.URL.Query().Get("lang")
}
_ = r.ParseForm()
fields := []string{"db_type", "web_framework", "module_name", "theme", "language", "http_port", "prefix",
"web_title", "login_page_logo", "sidebar_logo", "sidebar_min_logo"}
if field, ok := checkEmpty(fields, r); !ok {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 400, "msg": "` + local(lang)("web.wrong parameter") + `: ` + field + `"}`))
return
}
if r.PostFormValue("db_type") == "sqlite" {
if r.PostFormValue("db_path") == "" {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 400, "msg": "` + local(lang)("web.wrong parameter") + `"}`))
return
}
} else if r.PostFormValue("db_type") == "postgresql" {
if field, ok := checkEmpty([]string{"db_schema", "db_port", "db_host", "db_user", "db_passwd", "db_name"}, r); !ok {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 400, "msg": "` + local(lang)("web.wrong parameter") + `: ` + field + `"}`))
return
}
} else {
if field, ok := checkEmpty([]string{"db_port", "db_host", "db_user", "db_passwd", "db_name"}, r); !ok {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 400, "msg": "` + local(lang)("web.wrong parameter") + `: ` + field + `"}`))
return
}
}
var p = Project{
Port: r.PostFormValue("http_port"),
Theme: r.PostFormValue("theme"),
Prefix: r.PostFormValue("prefix"),
Language: r.PostFormValue("language"),
Driver: r.PostFormValue("db_type"),
DriverModule: r.PostFormValue("db_type"),
Framework: r.PostFormValue("web_framework"),
Module: r.PostFormValue("module_name"),
Orm: r.PostFormValue("use_gorm"),
}
if p.Driver == db.DriverPostgresql {
p.DriverModule = "postgres"
}
var (
info = &dbInfo{
DriverName: r.PostFormValue("db_type"),
Host: r.PostFormValue("db_host"),
Port: r.PostFormValue("db_port"),
File: r.PostFormValue("db_path"),
User: r.PostFormValue("db_user"),
Password: r.PostFormValue("db_passwd"),
Schema: r.PostFormValue("db_schema"),
Database: r.PostFormValue("db_name"),
}
dbList config.DatabaseList
)
if info.DriverName != db.DriverSqlite {
dbList = map[string]config.Database{
"default": {
Host: info.Host,
Port: info.Port,
User: info.User,
Pwd: info.Password,
Name: info.Database,
MaxIdleConns: 5,
MaxOpenConns: 10,
Driver: info.DriverName,
},
}
} else {
dbList = map[string]config.Database{
"default": {
Driver: info.DriverName,
File: info.File,
},
}
}
cfg := config.SetDefault(&config.Config{
Debug: true,
Env: config.EnvLocal,
Theme: p.Theme,
Store: config.Store{
Path: "./uploads",
Prefix: "uploads",
},
Language: p.Language,
UrlPrefix: p.Prefix,
IndexUrl: "/",
AccessLogPath: rootPath + "/logs/access.log",
ErrorLogPath: rootPath + "/logs/error.log",
InfoLogPath: rootPath + "/logs/info.log",
BootstrapFilePath: rootPath + "/bootstrap.go",
GoModFilePath: rootPath + "/go.mod",
Logo: template.HTML(r.PostFormValue("sidebar_logo")),
LoginLogo: template.HTML(r.PostFormValue("login_page_logo")),
MiniLogo: template.HTML(r.PostFormValue("sidebar_min_logo")),
Title: r.PostFormValue("web_title"),
Databases: dbList,
AssetRootPath: r.PostFormValue("assets_path"),
})
installProjectTmpl(p, cfg, "", info)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"code": 0, "msg": "` + local(lang)("web.install success") + `", "data": {"readme": ""}}`))
})
l, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
fmt.Println("GoAdmin web install program start.")
sc <- struct{}{}
if err := http.Serve(l, nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}(startChan)
<-startChan
err = open("http://localhost:" + port + "/")
if err != nil {
fmt.Println("failed to open browser", err)
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
}
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}