-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
325 lines (279 loc) · 6.45 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"errors"
"flag"
"fmt"
"github.com/manifoldco/promptui"
"net"
"os"
"path/filepath"
)
const DEMO_SERVICE_HOST = `192.168.33.6`
const FRPS_API = `http://192.168.33.6:7001/api`
var (
LOCAL_SERVICE_HOST = `host.docker.internal`
FRPS_LOCAL_API = `http://host.docker.internal:7001/api`
PROJECT_ROOT = ``
)
func prepareRuntime() error {
info, err := GetOSInfo()
if err != nil {
log.Error(err)
return err
}
// 处理 host.docker.internal 域名不可用的情况
patchLocalHost(info)
if info.IsWindows10() && info.MatchBuildVersion(DOCKER_DEPS_VERSION) {
log.Error("支持windows 10 版本过低,装不了Docker,请自己解决")
return err
}
err = IsDockerInstalled()
if err != nil {
log.Error(err)
if MatchLauncherError(err, ERROR_DOCKER_DESKTOP_NOT_RUNNING) {
startError := StartDockerDesktop()
if startError != nil {
log.Error(startError)
return startError
}
}
return err
}
return nil
}
func getProjectDomain(defaultValue string, asLocalService bool) (string, error) {
validate := func(input string) error {
if len(input) <= 0 {
return errors.New("必须提供一个项目名")
}
frpsEndPoint := FRPS_API
if asLocalService {
frpsEndPoint = FRPS_LOCAL_API
}
api := NewFrpApi(frpsEndPoint, "", "")
api.DisableAuth()
exist := api.SubDomainExist(input)
if exist {
return errors.New(fmt.Sprintf("三级域名 %s 已经存在并使用中,请重新选择另外一个名字", input))
}
return nil
}
prompt := promptui.Prompt{
Label: "请起一个霸气的项目名称, 最终将会能通过 {项目名}.dev.mixmedia.com 访问",
Validate: validate,
Default: defaultValue,
}
return prompt.Run()
}
func StartPHPWebProject(asLocalService bool) error {
if asLocalService {
log.Info(`启动本地 FPRS service`)
err := StartLocalFRPS()
if err != nil {
log.Error(err)
return err
}
}
dir, err := filepath.Abs(PROJECT_ROOT)
if err != nil {
log.Error(err)
return err
}
dir, err = FindPublicDir(dir)
if err != nil {
log.Error(err)
return err
}
frpcPath := filepath.Join(dir, "frpc.ini")
frpcCfg, err := LoadFrpcConfig(frpcPath)
defaultProjectName := ""
if err == nil && len(frpcCfg.SubDomain) > 0 {
defaultProjectName = frpcCfg.SubDomain
}
inputProjectName:
projectName, err := getProjectDomain(defaultProjectName, asLocalService)
if err != nil {
log.Error(err)
goto inputProjectName
}
// 匹配项目 composer.json
// 并根据composer 分析出php 版本
// 选择对应的 docker images
phpver, err := DetectPHPVersion(dir)
if err != nil {
log.Error(err)
return err
}
serviceHost := DEMO_SERVICE_HOST
if asLocalService {
serviceHost = LOCAL_SERVICE_HOST
}
builder := &BootStrapConfig{
Frp: &FRPConfig{
ServiceHost: serviceHost,
SubDomain: projectName,
},
Docker: &DockerComposeConfig{
ImageVersion: phpver,
},
}
// 覆盖项目 frpc.ini 及 docker-compose.yml
err = builder.BuildConfig(dir)
if err != nil {
log.Error(err)
return err
}
if asLocalService {
vHostName := fmt.Sprintf(`%s.localhost`, projectName)
err = AddLocalHostName(vHostName)
if err != nil {
log.Error(err)
return err
}
}
// 启动 docker compose 服务组
log.Info("准备启动容器")
err = StartContainer(dir, projectName)
if err != nil {
log.Error(err)
return err
}
// 打开browser 访问 外网绑定网址
go func() {
vhostURL := fmt.Sprintf(`http://%s.dev.mixmedia.com`, projectName)
if asLocalService {
vhostURL = fmt.Sprintf(`http://%s.localhost`, projectName)
}
log.Infof("请访问 %s \n", vhostURL)
OpenBrowser(vhostURL)
}()
return nil
}
func PHPConsole() error {
dir, err := filepath.Abs(PROJECT_ROOT)
if err != nil {
log.Error(err)
return err
}
frpcPath := filepath.Join(dir, "frpc.ini")
frpcCfg, err := LoadFrpcConfig(frpcPath)
if err == nil && len(frpcCfg.SubDomain) > 0 {
return RunPHPConsole(frpcCfg.SubDomain)
}
return nil
}
func ComposerInit() error {
dir, err := filepath.Abs(PROJECT_ROOT)
if err != nil {
log.Error(err)
return err
}
return PHPComposerInit(dir)
}
func StopPHPWebProject() error {
dir, err := filepath.Abs(PROJECT_ROOT)
if err != nil {
log.Error(err)
return err
}
frpcPath := filepath.Join(dir, "frpc.ini")
frpcCfg, err := LoadFrpcConfig(frpcPath)
if err == nil && len(frpcCfg.SubDomain) > 0 {
return StopContainer(dir, frpcCfg.SubDomain)
}
return nil
}
func StartMySQLServer() error {
err := StartLocalMySQLServer()
if err != nil {
log.Error(err)
return err
}
log.Warning("\n-------------\nMySQL 服务启动成功,即将打开 adminer 管理入 \nMySQL 默认账密 root/mysql50\nMySQL endpoint: host.docker.internal\n-------------\n")
OpenBrowser(`http://localhost:8088`)
return nil
}
func StartLocalRedisServer() error {
err := StartRedisService()
if err != nil {
log.Error(err)
return err
}
log.Warning("\n-------------\nRedis 服务启动成功 \nRedis endpoint: host.docker.internal\n-------------\n")
return nil
}
func SelectMethods() {
prompt := promptui.Select{
Label: "请选择操作",
Items: []string{
"1 启动PHP web项目 (外网地址)",
"2 启动PHP web项目 (本地地址)",
"3 进入PHP 项目 console",
"4 初始化 PHP项目 (composer update)",
"5 停止PHP web项目",
"6 启动 MySQL 服务",
"7 启动 Redis 服务",
},
}
index, _, err := prompt.Run()
if err != nil {
log.Error(err)
return
}
switch index {
case 0:
StartPHPWebProject(false)
return
case 1:
StartPHPWebProject(true)
return
case 2:
PHPConsole()
return
case 3:
ComposerInit()
return
case 4:
StopPHPWebProject()
return
case 5:
StartMySQLServer()
return
case 6:
StartLocalRedisServer()
return
}
}
func patchLocalHost(info *OSInfo) {
if info.IsMacOS() {
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Error(err)
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ipString := ipNet.IP.String()
LOCAL_SERVICE_HOST = ipString
FRPS_LOCAL_API = fmt.Sprintf(`http://%s:7001/api`, ipString)
break
}
}
}
}
func main() {
ReloadPathEnv()
runtimeError := prepareRuntime()
if runtimeError != nil {
log.Error(`环境检查有问题,请解决后重试, 输入任意键退出`)
fmt.Scanf("h")
os.Exit(1)
return
}
startPath := flag.String("p", filepath.Dir(os.Args[0]), "project root path")
flag.Parse()
PROJECT_ROOT = *startPath
start:
SelectMethods()
goto start
}