Skip to content

Commit

Permalink
cmd/forwarder/httpbin: run api server
Browse files Browse the repository at this point in the history
This is not to mix healthcheck with test requests.
After enabling logging the log was littered with healthcheck lines.
This way it's much cleaner and more consistent with the other containers.
  • Loading branch information
mmatczuk committed Nov 15, 2022
1 parent 4a760a0 commit a672f99
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 24 deletions.
24 changes: 23 additions & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"os/signal"
"syscall"

"github.com/prometheus/client_golang/prometheus"
"github.com/saucelabs/forwarder"
"github.com/saucelabs/forwarder/bind"
"github.com/saucelabs/forwarder/httpbin"
"github.com/saucelabs/forwarder/log"
"github.com/saucelabs/forwarder/log/stdlog"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

type command struct {
httpServerConfig *forwarder.HTTPServerConfig
apiServerConfig *forwarder.HTTPServerConfig
logConfig *log.Config
}

Expand All @@ -29,20 +32,39 @@ func (c *command) RunE(cmd *cobra.Command, args []string) error {
return err
}

r := prometheus.NewRegistry()
a, err := forwarder.NewHTTPServer(c.apiServerConfig, forwarder.NewAPIHandler(r, s, ""), logger.Named("api"))
if err != nil {
return err
}

return c.runHTTPServers(s, a)
}

func (c *command) runHTTPServers(servers ...*forwarder.HTTPServer) error {
var eg *errgroup.Group
ctx := context.Background()
ctx, _ = signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
return s.Run(ctx)
eg, ctx = errgroup.WithContext(ctx)
for _, s := range servers {
s := s
eg.Go(func() error { return s.Run(ctx) })
}
return eg.Wait()
}

func Command() (cmd *cobra.Command) {
c := command{
httpServerConfig: forwarder.DefaultHTTPServerConfig(),
apiServerConfig: forwarder.DefaultHTTPServerConfig(),
logConfig: log.DefaultConfig(),
}
c.apiServerConfig.Addr = ":10000"

defer func() {
fs := cmd.Flags()
bind.HTTPServerConfig(fs, c.httpServerConfig, "")
bind.HTTPServerConfig(fs, c.apiServerConfig, "api")
bind.LogConfig(fs, c.logConfig)
bind.MarkFlagFilename(cmd, "cert-file", "key-file", "log-file")
}()
Expand Down
2 changes: 1 addition & 1 deletion e2e/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ source ./lib.sh
HTTPBIN_SCHEME="https"
PROXY_SCHEME="https"

make up CONF="httpbin-${HTTPBIN_SCHEME}.yaml override/proxy-${PROXY_SCHEME}.yaml"
make up CONF="override/httpbin-${HTTPBIN_SCHEME}.yaml override/proxy-${PROXY_SCHEME}.yaml"

docker-compose logs -f
1 change: 1 addition & 0 deletions e2e/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ services:

httpbin:
extends: proxy
command: httpbin
7 changes: 0 additions & 7 deletions e2e/httpbin-h2.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions e2e/httpbin-http.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions e2e/httpbin-https.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion e2e/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ run_test() {
UPSTREAM_PROXY_SCHEME=$3
EXTRA_CONF=$4

CONF="httpbin-${HTTPBIN_SCHEME}.yaml override/proxy-${PROXY_SCHEME}.yaml"
CONF="override/httpbin-${HTTPBIN_SCHEME}.yaml override/proxy-${PROXY_SCHEME}.yaml"
if [[ -n "${UPSTREAM_PROXY_SCHEME}" ]]; then
CONF="${CONF} override/upstream-proxy-${UPSTREAM_PROXY_SCHEME}.yaml"
fi
Expand Down
7 changes: 7 additions & 0 deletions e2e/override/httpbin-h2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.8"

services:
httpbin:
environment:
FORWARDER_PROTOCOL: "H2"
FORWARDER_ADDRESS: ":443"
7 changes: 7 additions & 0 deletions e2e/override/httpbin-http.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.8"

services:
httpbin:
environment:
FORWARDER_PROTOCOL: "HTTP"
FORWARDER_ADDRESS: ":80"
7 changes: 7 additions & 0 deletions e2e/override/httpbin-https.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.8"

services:
httpbin:
environment:
FORWARDER_PROTOCOL: "HTTPS"
FORWARDER_ADDRESS: ":443"

0 comments on commit a672f99

Please sign in to comment.