-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (86 loc) · 2.26 KB
/
main.go
File metadata and controls
100 lines (86 loc) · 2.26 KB
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
package main
import (
"os"
"github.com/juliantellez/openvpn-client/internal/client"
"github.com/juliantellez/openvpn-client/shared/logger"
"github.com/juliantellez/openvpn-client/shared/prometheus"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
const (
appName = "openvpnClient"
appVersion = "0.1.0"
errorServiceFailure = "Failed to run service"
)
var (
config struct {
LogLevel string
LogOutput string
AddressPrometheus string
AddressClient string
OpenVpnConfig string
}
flags = []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "Log Level",
EnvVars: []string{"LOG_LEVEL"},
Value: "trace",
Destination: &config.LogLevel,
},
&cli.StringFlag{
Name: "log-output",
Usage: "Log Output `text or json`",
EnvVars: []string{"LOG_OUTPUT"},
Value: "json",
Destination: &config.LogOutput,
},
&cli.StringFlag{
Name: "prometheus-address",
Usage: "Prometheus Address exposes '/__/metrics' ",
EnvVars: []string{"PROMETHEUS_ADDRESS"},
Value: ":8081",
Destination: &config.AddressPrometheus,
},
&cli.StringFlag{
Name: "client-address",
Usage: "Client Address exposes the vpn wrapper",
EnvVars: []string{"CLIENT_ADDRESS"},
Value: ":7979",
Destination: &config.AddressClient,
},
&cli.StringFlag{
Name: "openvpn-config",
Usage: "OVPN config file",
EnvVars: []string{"OPENVPN_CONFIG"},
Destination: &config.OpenVpnConfig,
},
}
)
func appAction(cliCtx *cli.Context) error {
if err := logger.New(config.LogLevel, config.LogOutput); err != nil {
return err
}
ctx := cliCtx.Context
errorGroup, ctx := errgroup.WithContext(ctx)
metrics := prometheus.New(ctx, config.AddressPrometheus)
client := client.New(ctx, config.AddressClient)
errorGroup.Go(func() error {
return metrics.Serve(ctx)
})
errorGroup.Go(func() error {
return client.Serve()
})
return errorGroup.Wait()
}
func main() {
app := cli.NewApp()
app.Name = appName
app.Flags = flags
app.Version = appVersion
app.Action = appAction
if err := app.Run(os.Args); err != nil {
logrus.WithError(err).Fatal(errorServiceFailure)
}
}