Skip to content

Commit

Permalink
Add debug section for parsing config
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed May 26, 2024
1 parent 5dd76b6 commit dd5510e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
34 changes: 28 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"

"ella.to/baker/internal/httpclient"
Expand All @@ -34,6 +36,7 @@ type Server struct {
middlewareCacheMap *collection.Map[rule.Middleware]
runner *ActionRunner
close chan struct{}
isDebug bool
}

var _ http.Handler = (*Server)(nil)
Expand Down Expand Up @@ -161,16 +164,11 @@ func (s *Server) pingContainers() {
}
defer rc.Close()

config := Config{}
payload, err := io.ReadAll(rc)
config, err := s.parseConfig(rc)
if err != nil {
slog.Error("failed to read container config", "container_id", c.Id, "url", url, "error", err)
return
}
if err := json.Unmarshal(payload, &config); err != nil {
slog.Error("failed to decode container config", "container_id", c.Id, "url", url, "error", err, "payload", string(payload))
return
}

if statusCode >= 400 {
slog.Error("container config endpoint returned an error", "container_id", c.Id, "url", url, "status_code", statusCode)
Expand All @@ -184,6 +182,27 @@ func (s *Server) pingContainers() {
}
}

func (s *Server) parseConfig(rc io.ReadCloser) (*Config, error) {
config := &Config{}

if s.isDebug {
payload, err := io.ReadAll(rc)
if err != nil {
return nil, fmt.Errorf("failed to read config: %w", err)
}

if err := json.Unmarshal(payload, config); err != nil {
return nil, fmt.Errorf("failed to decode config: %w, payload=%s", err, payload)
}
} else {
if err := json.NewDecoder(rc).Decode(config); err != nil {
return nil, fmt.Errorf("failed to decode config: %w", err)
}
}

return config, nil
}

func (s *Server) addContainer(container *Container) {
_, ok := s.containersMap[container.Id]
if ok {
Expand Down Expand Up @@ -340,13 +359,16 @@ func WithRules(rules ...rule.RegisterFunc) serverOptFunc {
}

func NewServer(opts ...serverOpt) *Server {
logLevel := strings.ToLower(os.Getenv("BAKER_LOG_LEVEL"))

s := &Server{
bufferSize: 100,
pingDuration: 10 * time.Second,
containersMap: make(map[string]*containerInfo),
domainsMap: make(map[string]*trie.Node[*Service]),
middlewareCacheMap: collection.NewMap[rule.Middleware](),
close: make(chan struct{}),
isDebug: logLevel == "debug",
}

for _, opt := range opts {
Expand Down
17 changes: 11 additions & 6 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ import (

var count int

func createDummyContainer(t *testing.T, config *baker.Config) *baker.Container {
func createDummyContainerRaw(t *testing.T, config string) *baker.Container {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Debug("request", "host", r.Host, "path", r.URL.Path)

if r.URL.Path == "/config" {
b, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusOK)
w.Write(b)
w.Write([]byte(config))
return
}

Expand All @@ -51,6 +47,15 @@ func createDummyContainer(t *testing.T, config *baker.Config) *baker.Container {
}
}

func createDummyContainer(t *testing.T, config *baker.Config) *baker.Container {
configBytes, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}

return createDummyContainerRaw(t, string(configBytes))
}

func createBakerServer(t *testing.T) (*baker.Server, string) {
handler := baker.NewServer(
baker.WithPingDuration(2*time.Second),
Expand Down

0 comments on commit dd5510e

Please sign in to comment.