-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
81 lines (74 loc) · 1.78 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"github.com/BurntSushi/toml"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/vbatts/acme-reverseproxy/config"
)
var cfg config.Config
func main() {
app := cli.NewApp()
app.Name = "acme-reverseproxy"
app.Usage = "A TLS-serving reverse-proxy, with the certificates generated from LetsEncrypt"
app.Authors = []cli.Author{
{Name: "Vincent Batts", Email: "vbatts@hashbangbash.com"},
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug,D",
Usage: "debug output",
},
}
app.Commands = []cli.Command{
{
Name: "gen",
Description: "generators of sorts",
Subcommands: []cli.Command{
{
Name: "config",
Description: "generate a sample mapping configuration",
Action: genConfigAction,
},
},
},
{
Name: "srv",
Description: "Start the reverseproxy server",
Action: srvCommand,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Value: filepath.Join(os.Getenv("HOME"), ".acme-reverseproxy.toml"),
Usage: "Configuration of mapping of hostname -> listener",
},
},
Before: beforeAction,
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}
func beforeAction(c *cli.Context) error {
if c.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
// find and read in the toml config file for hostname -> listener mappingj
buf, err := ioutil.ReadFile(c.String("config"))
if err != nil {
if os.IsNotExist(err) {
logrus.Errorf("No config file found at %q. Try 'gen config'", c.String("config"))
}
return err
}
tmpConfig := config.Config{}
if err := toml.Unmarshal(buf, &tmpConfig); err != nil {
return err
}
cfg = tmpConfig
return nil
}