-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (61 loc) · 1.59 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
package main
import (
"flag"
"fmt"
"github.com/kylelemons/go-gypsy/yaml"
"os"
"os/exec"
"path"
"runtime"
)
const ConfigName = ".tower.yml"
func main() {
appMainFile := flag.String("m", "main.go", "path to your app's main file.")
appPort := flag.String("p", "5000", "port of your app.")
verbose := flag.Bool("v", false, "show more stuff.")
flag.Parse()
args := flag.Args()
if len(args) == 1 && args[0] == "init" {
generateExampleConfig()
return
}
startTower(*appMainFile, *appPort, *verbose)
}
func generateExampleConfig() {
_, file, _, _ := runtime.Caller(0)
exampleConfig := path.Dir(file) + "/tower.yml"
exec.Command("cp", exampleConfig, ConfigName).Run()
fmt.Println("== Generated config file " + ConfigName)
}
var (
app App
)
func startTower(appMainFile, appPort string, verbose bool) {
watchedFiles := ""
config, err := yaml.ReadFile(ConfigName)
if err == nil {
if verbose {
fmt.Println("== Load config from " + ConfigName)
}
appMainFile, _ = config.Get("main")
appPort, _ = config.Get("port")
watchedFiles, _ = config.Get("watch")
}
err = dialAddress("127.0.0.1:"+appPort, 1)
if err == nil {
fmt.Println("Error: port (" + appPort + ") already in used.")
os.Exit(1)
}
if verbose {
fmt.Println("== Application Info")
fmt.Printf(" build app with: %s\n", appMainFile)
fmt.Printf(" redirect requests from localhost:%s to localhost:%s\n\n", ProxyPort, appPort)
}
app = NewApp(appMainFile, appPort)
watcher := NewWatcher(app.Root, watchedFiles)
proxy := NewProxy(&app, &watcher)
go func() {
mustSuccess(watcher.Watch())
}()
mustSuccess(proxy.Listen())
}