forked from codegangsta/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
311 lines (276 loc) · 7 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
package main
import (
"fmt"
"github.com/codegangsta/envy/lib"
"github.com/codegangsta/gin/lib"
shellwords "github.com/mattn/go-shellwords"
"gopkg.in/urfave/cli.v1"
"github.com/0xAX/notificator"
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)
var (
startTime = time.Now()
logger = log.New(os.Stdout, "[gin] ", 0)
immediate = false
buildError error
colorGreen = string([]byte{27, 91, 57, 55, 59, 51, 50, 59, 49, 109})
colorRed = string([]byte{27, 91, 57, 55, 59, 51, 49, 59, 49, 109})
colorReset = string([]byte{27, 91, 48, 109})
notifier = notificator.New(notificator.Options{AppName: "Gin Build"})
notifications = false
)
func main() {
app := cli.NewApp()
app.Name = "gin"
app.Usage = "A live reload utility for Go web applications."
app.Action = MainAction
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "laddr,l",
Value: "",
EnvVar: "GIN_LADDR",
Usage: "listening address for the proxy server",
},
cli.IntFlag{
Name: "port,p",
Value: 3000,
EnvVar: "GIN_PORT",
Usage: "port for the proxy server",
},
cli.IntFlag{
Name: "appPort,a",
Value: 3001,
EnvVar: "BIN_APP_PORT",
Usage: "port for the Go web server",
},
cli.StringFlag{
Name: "bin,b",
Value: "gin-bin",
EnvVar: "GIN_BIN",
Usage: "name of generated binary file",
},
cli.StringFlag{
Name: "path,t",
Value: ".",
EnvVar: "GIN_PATH",
Usage: "Path to watch files from",
},
cli.StringFlag{
Name: "build,d",
Value: "",
EnvVar: "GIN_BUILD",
Usage: "Path to build files from (defaults to same value as --path)",
},
cli.StringSliceFlag{
Name: "excludeDir,x",
Value: &cli.StringSlice{},
EnvVar: "GIN_EXCLUDE_DIR",
Usage: "Relative directories to exclude",
},
cli.BoolFlag{
Name: "immediate,i",
EnvVar: "GIN_IMMEDIATE",
Usage: "run the server immediately after it's built",
},
cli.BoolFlag{
Name: "all",
EnvVar: "GIN_ALL",
Usage: "reloads whenever any file changes, as opposed to reloading only on .go file change",
},
cli.BoolFlag{
Name: "godep,g",
EnvVar: "GIN_GODEP",
Usage: "use godep when building",
},
cli.StringFlag{
Name: "buildArgs",
EnvVar: "GIN_BUILD_ARGS",
Usage: "Additional go build arguments",
},
cli.StringFlag{
Name: "certFile",
EnvVar: "GIN_CERT_FILE",
Usage: "TLS Certificate",
},
cli.StringFlag{
Name: "keyFile",
EnvVar: "GIN_KEY_FILE",
Usage: "TLS Certificate Key",
},
cli.StringFlag{
Name: "logPrefix",
EnvVar: "GIN_LOG_PREFIX",
Usage: "Log prefix",
Value: "gin",
},
cli.BoolFlag{
Name: "notifications",
EnvVar: "GIN_NOTIFICATIONS",
Usage: "Enables desktop notifications",
},
}
app.Commands = []cli.Command{
{
Name: "run",
ShortName: "r",
Usage: "Run the gin proxy in the current working directory",
Action: MainAction,
},
{
Name: "env",
ShortName: "e",
Usage: "Display environment variables set by the .env file",
Action: EnvAction,
},
}
app.Run(os.Args)
}
func MainAction(c *cli.Context) {
laddr := c.GlobalString("laddr")
port := c.GlobalInt("port")
all := c.GlobalBool("all")
appPort := strconv.Itoa(c.GlobalInt("appPort"))
immediate = c.GlobalBool("immediate")
keyFile := c.GlobalString("keyFile")
certFile := c.GlobalString("certFile")
logPrefix := c.GlobalString("logPrefix")
notifications = c.GlobalBool("notifications")
logger.SetPrefix(fmt.Sprintf("[%s] ", logPrefix))
// Bootstrap the environment
envy.Bootstrap()
// Set the PORT env
os.Setenv("PORT", appPort)
wd, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
buildArgs, err := shellwords.Parse(c.GlobalString("buildArgs"))
if err != nil {
logger.Fatal(err)
}
buildPath := c.GlobalString("build")
if buildPath == "" {
buildPath = c.GlobalString("path")
}
builder := gin.NewBuilder(buildPath, c.GlobalString("bin"), c.GlobalBool("godep"), wd, buildArgs)
runner := gin.NewRunner(filepath.Join(wd, builder.Binary()), c.Args()...)
runner.SetWriter(os.Stdout)
proxy := gin.NewProxy(builder, runner)
config := &gin.Config{
Laddr: laddr,
Port: port,
ProxyTo: "http://localhost:" + appPort,
KeyFile: keyFile,
CertFile: certFile,
}
err = proxy.Run(config)
if err != nil {
logger.Fatal(err)
}
if laddr != "" {
logger.Printf("Listening at %s:%d\n", laddr, port)
} else {
logger.Printf("Listening on port %d\n", port)
}
shutdown(runner)
// build right now
build(builder, runner, logger)
// scan for changes
scanChanges(c.GlobalString("path"), c.GlobalStringSlice("excludeDir"), all, func(path string) {
runner.Kill()
build(builder, runner, logger)
})
}
func EnvAction(c *cli.Context) {
logPrefix := c.GlobalString("logPrefix")
logger.SetPrefix(fmt.Sprintf("[%s] ", logPrefix))
// Bootstrap the environment
env, err := envy.Bootstrap()
if err != nil {
logger.Fatalln(err)
}
for k, v := range env {
fmt.Printf("%s: %s\n", k, v)
}
}
func build(builder gin.Builder, runner gin.Runner, logger *log.Logger) {
logger.Println("Building Project...")
if notifications {
notifier.Push("Build Started!", "Building "+builder.Binary()+"...", "", notificator.UR_NORMAL)
}
err := builder.Build()
if err != nil {
buildError = err
logger.Printf("%sBuild failed%s\n", colorRed, colorReset)
fmt.Println(builder.Errors())
buildErrors := strings.Split(builder.Errors(), "\n")
if notifications {
if err := notifier.Push("Build FAILED!", buildErrors[1], "", notificator.UR_CRITICAL); err != nil {
logger.Println("Notification send failed")
}
}
} else {
buildError = nil
logger.Printf("%sBuild finished%s\n", colorGreen, colorReset)
if immediate {
runner.Run()
}
if notifications {
if err := notifier.Push("Build Succeded", "Build Finished!", "", notificator.UR_CRITICAL); err != nil {
logger.Println("Notification send failed")
}
}
}
time.Sleep(100 * time.Millisecond)
}
type scanCallback func(path string)
func scanChanges(watchPath string, excludeDirs []string, allFiles bool, cb scanCallback) {
fileChanges := false
for {
filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" && info.IsDir() {
return filepath.SkipDir
}
for _, x := range excludeDirs {
if x == path {
return filepath.SkipDir
}
}
// ignore hidden files
if filepath.Base(path)[0] == '.' {
return nil
}
if (allFiles || filepath.Ext(path) == ".go") && info.ModTime().After(startTime) {
fileChanges = true
startTime = info.ModTime()
logger.Printf("file changes: %s at %s", path, info.ModTime().String())
}
return nil
})
if fileChanges{
fileChanges = false
cb("")
}
time.Sleep(500 * time.Millisecond)
}
}
func shutdown(runner gin.Runner) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
log.Println("Got signal: ", s)
err := runner.Kill()
if err != nil {
log.Print("Error killing: ", err)
}
os.Exit(1)
}()
}