-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (75 loc) · 2.03 KB
/
main.go
File metadata and controls
90 lines (75 loc) · 2.03 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
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"net/url"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
optionNameInternalPort = "internal-port"
optionNameExternalPort = "external-port"
optionNameIngressURL = "ingress-url"
)
func main() {
cli := &cli{}
cmd := &cobra.Command{
Short: "proxies the calls to an ingress address separating internal and external http routes",
Use: "auth-proxy",
PreRunE: cli.setupConfig,
RunE: cli.run,
}
cmd.Flags().Int(optionNameInternalPort, 1643, "Internal port for whitelisted resources.")
cmd.Flags().Int(optionNameExternalPort, 1645, "External port for blacklisted resources.")
cmd.Flags().String(optionNameIngressURL, "http://localhost:1633", "the URL of the ingress")
if err := viper.BindPFlags(cmd.Flags()); err != nil {
log.Fatal(err)
}
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
type cli struct {
internalPort int
externalPort int
ingressURL string
}
func (c *cli) setupConfig(cmd *cobra.Command, args []string) error {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.SetConfigType("yml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
c.internalPort = viper.GetInt(optionNameInternalPort)
c.externalPort = viper.GetInt(optionNameExternalPort)
c.ingressURL = viper.GetString(optionNameIngressURL)
return nil
}
func (c *cli) run(cmd *cobra.Command, args []string) (err error) {
ingress, err := url.Parse(c.ingressURL)
if err != nil {
return
}
app := proxy{
internalPort: c.internalPort,
externalPort: c.externalPort,
scheme: ingress.Scheme,
host: ingress.Host,
}
app.init()
go app.run()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done
app.shutdown()
return
}