Skip to content

Commit

Permalink
cmd/forwarder/run: only add /pac endpoint if pac is set
Browse files Browse the repository at this point in the history
This refactors apiEndpoint to more generic list that can be modified by different code blocks.
  • Loading branch information
mmatczuk committed Sep 27, 2023
1 parent 85bdb74 commit 5c3679b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions cmd/forwarder/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
var (
pr forwarder.PACResolver
rt http.RoundTripper
ep []forwarder.APIEndpoint
)

{
Expand All @@ -92,14 +93,12 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
}
}

var pacScript string
if c.pac != nil {
var err error
pacScript, err = forwarder.ReadURLString(c.pac, rt)
script, err := forwarder.ReadURLString(c.pac, rt)
if err != nil {
return fmt.Errorf("read PAC file: %w", err)
}
pr, err = pac.NewProxyResolverPool(&pac.ProxyResolverConfig{Script: pacScript}, nil)
pr, err = pac.NewProxyResolverPool(&pac.ProxyResolverConfig{Script: script}, nil)
if err != nil {
return err
}
Expand All @@ -110,6 +109,11 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
Resolver: pr,
Logger: logger.Named("pac"),
}

ep = append(ep, forwarder.APIEndpoint{
Path: "/pac",
Handler: httphandler.SendFileString("application/x-ns-proxy-autoconfig", script),
})
}

cm, err := forwarder.NewCredentialsMatcher(c.credentials, logger.Named("credentials"))
Expand Down Expand Up @@ -175,22 +179,18 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
}

if c.apiServerConfig.Addr != "" {
apiEndpoints := []forwarder.APIEndpoint{
ep := append([]forwarder.APIEndpoint{
{
Path: "/configz",
Handler: httphandler.SendFileString("text/plain", config),
},
{
Path: "/pac",
Handler: httphandler.SendFileString("application/x-ns-proxy-autoconfig", pacScript),
},
{
Path: "/version",
Handler: httphandler.Version(version.Version, version.Time, version.Commit),
},
}
}, ep...)

h := forwarder.NewAPIHandler("Forwarder "+version.Version, c.promReg, nil, apiEndpoints...)
h := forwarder.NewAPIHandler("Forwarder "+version.Version, c.promReg, nil, ep...)
a, err := forwarder.NewHTTPServer(c.apiServerConfig, h, logger.Named("api"))
if err != nil {
return err
Expand Down

0 comments on commit 5c3679b

Please sign in to comment.