Skip to content

Commit

Permalink
feat: Add response logging and improve error handling in config proce…
Browse files Browse the repository at this point in the history
…ssing
  • Loading branch information
presbrey committed Jan 31, 2025
1 parent 045aca9 commit cd2d786
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ func (lm *LightyMux) newReverseProxy(nextHop *url.URL) *httputil.ReverseProxy {
lm.logger.Printf("Proxying request to: %s", req.URL.String())
}
},
ModifyResponse: func(resp *http.Response) error {
if lm.options.LogResponses {
lm.logger.Printf("Response from %s: status=%d, headers=%v",
resp.Request.URL.String(), resp.StatusCode, resp.Header)
}
return nil
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
if lm.options.LogErrors {
lm.logger.Printf("Proxy error: %v", err)
Expand Down Expand Up @@ -329,15 +336,13 @@ func (lm *LightyMux) applyConfig(config *LightyConfig) error {
for path, route := range config.Routes {
target := route.Target
if target == "" {
lm.logger.Printf("Skipping route %s: no target specified", path)
continue
return fmt.Errorf("route %s: no target specified", path)
}

if isWebScheme(target) {
nextHop, err := url.Parse(target)
if err != nil {
lm.logger.Printf("Error parsing URL %s: %v", target, err)
continue
return fmt.Errorf("route %s: invalid URL %s: %v", path, target, err)
}
proxy := lm.newReverseProxy(nextHop)

Expand Down Expand Up @@ -399,8 +404,7 @@ func (lm *LightyMux) applyConfig(config *LightyConfig) error {
// Check if target is a directory or a file
fileInfo, err := os.Stat(target)
if err != nil {
lm.logger.Printf("Error accessing path %s: %v", target, err)
continue
return fmt.Errorf("route %s: error accessing path %s: %v", path, target, err)
}

if fileInfo.IsDir() {
Expand Down
78 changes: 78 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,81 @@ func TestRemoteReloaderErrors(t *testing.T) {
t.Error("Load() with non-existent server should return error")
}
}

func TestInitReloaderErrors(t *testing.T) {
tests := []struct {
name string
configPath string
wantErr string
}{
{
name: "nonexistent config file",
configPath: "/nonexistent/config.yaml",
wantErr: "failed to load initial config",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lm := &LightyMux{
options: &Options{
ConfigRefreshInterval: time.Second,
},
mux: http.NewServeMux(),
logger: log.New(os.Stderr, "", log.LstdFlags),
}
err := lm.initReloader(tt.configPath)
if err == nil {
t.Error("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("expected error containing %q, got %v", tt.wantErr, err)
}
})
}
}

func TestProcessConfigErrors(t *testing.T) {
tests := []struct {
name string
data []byte
wantErr string
setupLM func(*LightyMux)
}{
{
name: "invalid yaml",
data: []byte(`invalid: yaml: [`),
wantErr: "failed to parse YAML",
},
{
name: "invalid URL target",
data: []byte(`
routes:
"/api/":
target: "http://[invalid-url" # Invalid URL syntax
`),
wantErr: "failed to apply config",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &strings.Builder{}
lm := &LightyMux{
options: &Options{},
mux: http.NewServeMux(),
logger: log.New(buf, "", log.LstdFlags),
}
if tt.setupLM != nil {
tt.setupLM(lm)
}
err := lm.processConfig(tt.data)
if err == nil {
t.Error("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("expected error containing %q, got %v", tt.wantErr, err)
}
})
}
}

0 comments on commit cd2d786

Please sign in to comment.