-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (73 loc) · 2.11 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
package main
import (
"net/http"
"os"
"path"
"path/filepath"
"github.com/packaged/logger/v2"
"github.com/packaged/logger/v2/ld"
"go.uber.org/zap"
cli "gopkg.in/alecthomas/kingpin.v2"
)
var (
configPath = cli.Flag("config", "Path to the config yaml").Short('c').String()
devEnvironment = cli.Flag("development", "Development Environment").Short('d').Bool()
verboseLog = cli.Flag("verbose", "Verbose logging").Short('v').Bool()
)
var logs *logger.Logger
func main() {
cli.Parse()
var opts []logger.Option
if *verboseLog {
opts = append(opts, logger.Debug)
} else {
opts = append(opts, func(config *zap.Config) { logger.DisableStacktrace(config) })
}
if *devEnvironment {
opts = append(opts, logger.WithConsoleEncoding)
}
logs = logger.Instance(opts...)
var configPaths []string
if *configPath != "" {
// config file specified
if filepath.IsAbs(*configPath) {
// absolute path
configPaths = append(configPaths, *configPath)
} else if cwd, err := os.Getwd(); err == nil {
// relative path
configPaths = append(configPaths, path.Join(cwd, *configPath))
}
} else {
if cwd, err := os.Getwd(); err == nil {
// no config specified, search in current directory
configPaths = append(configPaths, path.Join(cwd, "config.yaml"))
}
if binPath, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
// search in binary directory
configPaths = append(configPaths, path.Join(binPath, "config.yaml"))
}
}
var cfg *Config
for _, configFile := range configPaths {
info, err := os.Stat(configFile)
if !os.IsNotExist(err) && !info.IsDir() {
cfg, err = LoadConfig(configFile)
logs.FatalIf(err, "loading config")
break
}
}
if cfg == nil {
logs.Fatal("Config file not found")
}
go startSshTunnel(cfg)
p := NewProxy(cfg)
httpServer := http.Server{Addr: cfg.ListenAddress, Handler: p}
logs.Debug("Listening", ld.TrustedString("host", cfg.ListenAddress))
if cfg.Tls {
logs.Debug("Serving with TLS")
}
if cfg.Tls {
logs.FatalIf(httpServer.ListenAndServeTLS(cfg.TlsCertFile, cfg.TlsKeyFile), "serve")
}
logs.FatalIf(httpServer.ListenAndServe(), "serve")
}