-
Notifications
You must be signed in to change notification settings - Fork 7
/
web_command.go
158 lines (138 loc) · 4.39 KB
/
web_command.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
package question
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"golang.org/x/exp/slices"
"github.com/platformsh/platformify/internal/question/models"
"github.com/platformsh/platformify/internal/utils"
"github.com/platformsh/platformify/vendorization"
)
type WebCommand struct{}
func (q *WebCommand) Ask(ctx context.Context) error {
answers, ok := models.FromContext(ctx)
if !ok {
return nil
}
if answers.WebCommand != "" {
// Skip the step
return nil
}
// Do not ask the command for PHP applications
if answers.Type.Runtime == models.PHP {
return nil
}
assets, _ := vendorization.FromContext(ctx)
//nolint:lll
answers.WebCommand = fmt.Sprintf(
"echo 'Put your web server command in here! You need to listen to \"$UNIX\" unix socket. Read more about it here: %s#web-commands'; sleep 60",
assets.Docs().AppReference,
)
if answers.SocketFamily == models.TCP {
//nolint:lll
answers.WebCommand = fmt.Sprintf(
"echo 'Put your web server command in here! You need to listen to \"$PORT\" port. Read more about it here: %s#web-commands'; sleep 60",
assets.Docs().AppReference,
)
}
switch answers.Stack {
case models.Django:
prefix := ""
pythonPath := ""
wsgi := "app.wsgi"
// try to find the wsgi.py file to change the default command
wsgiPath := utils.FindFile(path.Join(answers.WorkingDirectory, answers.ApplicationRoot), "wsgi.py")
if wsgiPath != "" {
wsgiParentDir := path.Base(path.Dir(wsgiPath))
wsgi = fmt.Sprintf("%s.wsgi", wsgiParentDir)
// add the pythonpath if the wsgi.py file is not in the root of the app
wsgiRel, _ := filepath.Rel(
path.Join(answers.WorkingDirectory, answers.ApplicationRoot),
path.Dir(path.Dir(wsgiPath)),
)
if wsgiRel != "." {
pythonPath = "--pythonpath=" + path.Base(path.Dir(path.Dir(wsgiPath)))
}
}
if slices.Contains(answers.DependencyManagers, models.Pipenv) {
prefix = "pipenv run "
} else if slices.Contains(answers.DependencyManagers, models.Poetry) {
prefix = "poetry run "
}
if answers.SocketFamily == models.TCP {
answers.WebCommand = fmt.Sprintf("%sgunicorn %s -b 0.0.0.0:$PORT %s --log-file -", prefix, pythonPath, wsgi)
return nil
}
answers.WebCommand = fmt.Sprintf("%sgunicorn %s -b unix:$SOCKET %s --log-file -", prefix, pythonPath, wsgi)
return nil
case models.NextJS:
answers.WebCommand = "npx next start -p $PORT"
return nil
case models.Strapi:
if _, ok := utils.GetJSONValue(
[]string{"scripts", "start"},
path.Join(answers.WorkingDirectory, "package.json"),
true,
); ok {
if slices.Contains(answers.DependencyManagers, models.Yarn) {
answers.WebCommand = "NODE_ENV=production yarn start"
} else {
answers.WebCommand = "NODE_ENV=production npm start"
}
}
case models.Express:
if _, ok := utils.GetJSONValue(
[]string{"scripts", "start"},
path.Join(answers.WorkingDirectory, "package.json"),
true,
); ok {
if slices.Contains(answers.DependencyManagers, models.Yarn) {
answers.WebCommand = "NODE_ENV=production yarn start"
} else {
answers.WebCommand = "NODE_ENV=production npm start"
}
return nil
}
if mainPath, ok := utils.GetJSONValue(
[]string{"main"},
path.Join(answers.WorkingDirectory, "package.json"),
true,
); ok {
answers.WebCommand = fmt.Sprintf("node %s", mainPath.(string))
return nil
}
if indexFile := utils.FindFile(answers.WorkingDirectory, "index.js"); indexFile != "" {
indexFile, _ = filepath.Rel(answers.WorkingDirectory, indexFile)
answers.WebCommand = fmt.Sprintf("node %s", indexFile)
return nil
}
case models.Flask:
appPath := ""
// try to find the app.py, api.py or server.py files
for _, name := range []string{"app.py", "server.py", "api.py"} {
if _, err := os.Stat(path.Join(answers.WorkingDirectory, name)); err == nil {
appPath = fmt.Sprintf("'%s:app'", strings.TrimSuffix(name, ".py"))
break
}
}
if appPath == "" {
return nil
}
prefix := ""
if slices.Contains(answers.DependencyManagers, models.Pipenv) {
prefix = "pipenv run "
} else if slices.Contains(answers.DependencyManagers, models.Poetry) {
prefix = "poetry run "
}
if answers.SocketFamily == models.TCP {
answers.WebCommand = fmt.Sprintf("%sgunicorn -b 0.0.0.0:$PORT %s --log-file -", prefix, appPath)
return nil
}
answers.WebCommand = fmt.Sprintf("%sgunicorn -b unix:$SOCKET %s --log-file -", prefix, appPath)
return nil
}
return nil
}