forked from codegangsta/gin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
197 lines (168 loc) · 3.73 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
package main
import (
"errors"
"fmt"
cli "github.com/urfave/cli"
"github.com/af83/gin/lib"
"github.com/codegangsta/envy/lib"
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"
)
var (
startTime = time.Now()
logger = log.New(os.Stdout, "[gin] ", 0)
immediate = false
buildError error
)
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.IntFlag{
Name: "port,p",
Value: 3000,
Usage: "port for the proxy server",
},
cli.IntFlag{
Name: "appPort,a",
Value: 3001,
Usage: "port for the Go web server",
},
cli.StringFlag{
Name: "bin,b",
Value: "gin-bin",
Usage: "name of generated binary file",
},
cli.StringFlag{
Name: "path,t",
Value: ".",
Usage: "Path to watch files from",
},
cli.BoolFlag{
Name: "immediate,i",
Usage: "run the server immediately after it's built",
},
cli.BoolFlag{
Name: "godep,g",
Usage: "use godep when building",
},
}
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) {
port := c.GlobalInt("port")
appPort := strconv.Itoa(c.GlobalInt("appPort"))
immediate = c.GlobalBool("immediate")
// Bootstrap the environment
envy.Bootstrap()
// Set the PORT env
os.Setenv("PORT", appPort)
wd, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
builder := gin.NewBuilder(c.GlobalString("path"), c.GlobalString("bin"), c.GlobalBool("godep"))
runner := gin.NewRunner(filepath.Join(wd, builder.Binary()), c.Args()...)
runner.SetWriter(os.Stdout)
proxy := gin.NewProxy(builder, runner)
config := &gin.Config{
Port: port,
ProxyTo: "http://localhost:" + appPort,
}
err = proxy.Run(config)
if err != nil {
logger.Fatal(err)
}
logger.Printf("listening on port %d\n", port)
shutdown(runner)
// build right now
build(builder, runner, logger)
// scan for changes
scanChanges(c.GlobalString("path"), func(path string) {
runner.Kill()
build(builder, runner, logger)
})
}
func EnvAction(c *cli.Context) {
// 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) {
err := builder.Build()
if err != nil {
buildError = err
logger.Println("ERROR! Build failed.")
fmt.Println(builder.Errors())
} else {
// print success only if there were errors before
if buildError != nil {
logger.Println("Build Successful")
}
buildError = nil
if immediate {
runner.Run()
}
}
time.Sleep(100 * time.Millisecond)
}
type scanCallback func(path string)
func scanChanges(watchPath string, cb scanCallback) {
for {
filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" {
return filepath.SkipDir
}
// ignore hidden files
if filepath.Base(path)[0] == '.' {
return nil
}
if filepath.Ext(path) == ".go" && info.ModTime().After(startTime) {
cb(path)
startTime = time.Now()
return errors.New("done")
}
return nil
})
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)
}()
}