Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CatchAll functionality #217

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## UNRELEASED

### Added

- Added possibility catch all paths with error page 404 (using `--catch-all` flag for the `serve` or environment variable `CATCH_ALL=true`) [#217]

[#217]:https://github.com/tarampampam/error-pages/issues/217

## v2.24.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
<<: *app-service
ports:
- "8080:8080/tcp" # Open <http://127.0.0.1:8080>
command: sh -c "go build -buildvcs=false -o /tmp/app ./cmd/error-pages && /tmp/app serve --show-details --proxy-headers=X-Foo,Bar,Baz_blah"
command: sh -c "go build -buildvcs=false -o /tmp/app ./cmd/error-pages && /tmp/app serve --show-details --proxy-headers=X-Foo,Bar,Baz_blah --catch-all"
healthcheck:
test: ['CMD', '/tmp/app', '--log-json', 'healthcheck']
interval: 4s
Expand Down
8 changes: 8 additions & 0 deletions internal/cli/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
showDetailsFlagName = "show-details"
proxyHTTPHeadersFlagName = "proxy-headers"
disableL10nFlagName = "disable-l10n"
catchAllFlagName = "catch-all"
)

const (
Expand Down Expand Up @@ -76,6 +77,7 @@
o.Default.PageCode = c.String(defaultErrorPageFlagName)
o.Default.HTTPCode = uint16(c.Uint(defaultHTTPCodeFlagName))
o.ShowDetails = c.Bool(showDetailsFlagName)
o.CatchAll = c.Bool(catchAllFlagName)

Check warning on line 80 in internal/cli/serve/command.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/serve/command.go#L80

Added line #L80 was not covered by tests

if headers := c.String(proxyHTTPHeadersFlagName); headers != "" { //nolint:nestif
var m = make(map[string]struct{})
Expand Down Expand Up @@ -151,6 +153,11 @@
Usage: "disable error pages localization",
EnvVars: []string{env.DisableL10n.String()},
},
&cli.BoolFlag{
Name: catchAllFlagName,
Usage: "catch all pages",
EnvVars: []string{env.CatchAll.String()},
},
},
}

Expand Down Expand Up @@ -240,6 +247,7 @@
zap.Strings("proxy headers", opt.ProxyHTTPHeaders),
zap.Bool("show request details", opt.ShowDetails),
zap.Bool("localization disabled", opt.L10n.Disabled),
zap.Bool("catch all enabled", opt.CatchAll),

Check warning on line 250 in internal/cli/serve/command.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/serve/command.go#L250

Added line #L250 was not covered by tests
)

if err := server.Start(ip, port); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
ShowDetails envVariable = "SHOW_DETAILS" // show request details in response
ProxyHTTPHeaders envVariable = "PROXY_HTTP_HEADERS" // proxy HTTP request headers list (request -> response)
DisableL10n envVariable = "DISABLE_L10N" // disable pages localization
CatchAll envVariable = "CATCH_ALL" // catch all pages
)

// String returns environment variable name in the string representation.
Expand Down
2 changes: 2 additions & 0 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestConstants(t *testing.T) {
assert.Equal(t, "SHOW_DETAILS", string(ShowDetails))
assert.Equal(t, "PROXY_HTTP_HEADERS", string(ProxyHTTPHeaders))
assert.Equal(t, "DISABLE_L10N", string(DisableL10n))
assert.Equal(t, "CATCH_ALL", string(CatchAll))
}

func TestEnvVariable_Lookup(t *testing.T) {
Expand All @@ -32,6 +33,7 @@ func TestEnvVariable_Lookup(t *testing.T) {
{giveEnv: ShowDetails},
{giveEnv: ProxyHTTPHeaders},
{giveEnv: DisableL10n},
{giveEnv: CatchAll},
}

for _, tt := range cases {
Expand Down
8 changes: 7 additions & 1 deletion internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@

s.router.GET("/metrics", metricsHandler.NewHandler(reg))

s.router.NotFound = notfoundHandler.NewHandler(cfg, templatePicker, s.rdr, opt)
// use index handler to catch all paths? Uses DEFAULT_ERROR_PAGE
if opt.CatchAll {
s.router.NotFound = indexHandler.NewHandler(cfg, templatePicker, s.rdr, opt)
} else {
// use default not found handler
s.router.NotFound = notfoundHandler.NewHandler(cfg, templatePicker, s.rdr, opt)
}

Check warning on line 116 in internal/http/server.go

View check run for this annotation

Codecov / codecov/patch

internal/http/server.go#L110-L116

Added lines #L110 - L116 were not covered by tests

return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/options/errorpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type ErrorPage struct {
}
ShowDetails bool // show request details in response
ProxyHTTPHeaders []string // proxy HTTP request headers list
CatchAll bool // catch all pages
}
Loading