Skip to content

Commit 4c4255e

Browse files
committed
Create proxies only specificed in the configuration file and clean-up gateway.New() function
Signed-off-by: Doğukan Teber <[email protected]>
1 parent 9ac6ccf commit 4c4255e

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

gateway/gateway.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"net/http"
55

66
"github.com/cortexproject/auth-gateway/server"
7+
"github.com/sirupsen/logrus"
78
)
89

910
type Gateway struct {
1011
distributorProxy *Proxy
1112
queryFrontendProxy *Proxy
1213
alertmanagerProxy *Proxy
13-
ruler *Proxy
14+
rulerProxy *Proxy
1415
srv *server.Server
1516
}
1617

@@ -58,44 +59,68 @@ var defaultRulerAPIs = []string{
5859
}
5960

6061
func New(config *Config, srv *server.Server) (*Gateway, error) {
61-
distributor, err := NewProxy(config.Distributor.URL, config.Distributor, DISTRIBUTOR)
62-
if err != nil {
63-
return nil, err
62+
gateway := &Gateway{
63+
srv: srv,
6464
}
6565

66-
frontend, err := NewProxy(config.QueryFrontend.URL, config.QueryFrontend, FRONTEND)
67-
if err != nil {
68-
return nil, err
66+
components := []string{DISTRIBUTOR, FRONTEND, ALERTMANAGER, RULER}
67+
for _, componentName := range components {
68+
upstreamConfig := config.getUpstreamConfig(componentName)
69+
proxy, err := setupProxy(upstreamConfig, componentName, componentName)
70+
if err != nil {
71+
return nil, err
72+
}
73+
switch componentName {
74+
case DISTRIBUTOR:
75+
gateway.distributorProxy = proxy
76+
case FRONTEND:
77+
gateway.queryFrontendProxy = proxy
78+
case ALERTMANAGER:
79+
gateway.alertmanagerProxy = proxy
80+
case RULER:
81+
gateway.rulerProxy = proxy
82+
}
6983
}
7084

71-
alertmanager, err := NewProxy(config.Alertmanager.URL, config.Alertmanager, ALERTMANAGER)
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
ruler, err := NewProxy(config.Ruler.URL, config.Ruler, RULER)
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
return &Gateway{
82-
distributorProxy: distributor,
83-
queryFrontendProxy: frontend,
84-
alertmanagerProxy: alertmanager,
85-
ruler: ruler,
86-
srv: srv,
87-
}, nil
85+
return gateway, nil
8886
}
8987

9088
func (g *Gateway) Start(config *Config) {
9189
g.registerRoutes(config)
9290
}
9391

92+
func (c *Config) getUpstreamConfig(componentName string) Upstream {
93+
switch componentName {
94+
case DISTRIBUTOR:
95+
return c.Distributor
96+
case FRONTEND:
97+
return c.QueryFrontend
98+
case ALERTMANAGER:
99+
return c.Alertmanager
100+
case RULER:
101+
return c.Ruler
102+
default:
103+
return Upstream{}
104+
}
105+
}
106+
107+
func setupProxy(upstreamConfig Upstream, proxyType string, description string) (*Proxy, error) {
108+
if upstreamConfig.URL != "" {
109+
proxy, err := NewProxy(upstreamConfig.URL, upstreamConfig, proxyType)
110+
if err != nil {
111+
return nil, err
112+
}
113+
return proxy, nil
114+
}
115+
logrus.Infof("%s URL configuration not provided. %s will not be set up.", description, description)
116+
return nil, nil
117+
}
118+
94119
func (g *Gateway) registerRoutes(config *Config) {
95120
g.registerProxyRoutes(config.Distributor.Paths, defaultDistributorAPIs, http.HandlerFunc(g.distributorProxy.Handler))
96121
g.registerProxyRoutes(config.QueryFrontend.Paths, defaultQueryFrontendAPIs, http.HandlerFunc(g.queryFrontendProxy.Handler))
97122
g.registerProxyRoutes(config.Alertmanager.Paths, defaultAlertmanagerAPIs, http.HandlerFunc(g.alertmanagerProxy.Handler))
98-
g.registerProxyRoutes(config.Ruler.Paths, defaultRulerAPIs, http.HandlerFunc(g.ruler.Handler))
123+
g.registerProxyRoutes(config.Ruler.Paths, defaultRulerAPIs, http.HandlerFunc(g.rulerProxy.Handler))
99124
g.srv.RegisterTo("/", http.HandlerFunc(g.notFoundHandler), server.UNAUTH)
100125
}
101126

0 commit comments

Comments
 (0)