From 3ad3c19d826aaeaafcc9553818d12098fed78a2b Mon Sep 17 00:00:00 2001 From: Nicolai Rybnikar Date: Thu, 31 Aug 2023 10:37:18 +0200 Subject: [PATCH 1/2] Add CatchAll functionality --- CHANGELOG.md | 6 ++++++ docker-compose.yml | 2 +- internal/cli/serve/command.go | 8 ++++++++ internal/env/env.go | 1 + internal/env/env_test.go | 2 ++ internal/http/server.go | 8 +++++++- internal/options/errorpage.go | 1 + 7 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a0a7708..48487643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ 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`) + ## v2.24.0 ### Added diff --git a/docker-compose.yml b/docker-compose.yml index 423c024d..d69ea330 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,7 +25,7 @@ services: <<: *app-service ports: - "8080:8080/tcp" # Open - 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 diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index 8e66f7d7..0eb47912 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -32,6 +32,7 @@ const ( showDetailsFlagName = "show-details" proxyHTTPHeadersFlagName = "proxy-headers" disableL10nFlagName = "disable-l10n" + catchAllFlagName = "catch-all" ) const ( @@ -76,6 +77,7 @@ func NewCommand(log *zap.Logger) *cli.Command { //nolint:funlen o.Default.PageCode = c.String(defaultErrorPageFlagName) o.Default.HTTPCode = uint16(c.Uint(defaultHTTPCodeFlagName)) o.ShowDetails = c.Bool(showDetailsFlagName) + o.CatchAll = c.Bool(catchAllFlagName) if headers := c.String(proxyHTTPHeadersFlagName); headers != "" { //nolint:nestif var m = make(map[string]struct{}) @@ -151,6 +153,11 @@ func NewCommand(log *zap.Logger) *cli.Command { //nolint:funlen Usage: "disable error pages localization", EnvVars: []string{env.DisableL10n.String()}, }, + &cli.BoolFlag{ + Name: catchAllFlagName, + Usage: "catch all pages", + EnvVars: []string{env.CatchAll.String()}, + }, }, } @@ -240,6 +247,7 @@ func (cmd *command) Run( //nolint:funlen 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), ) if err := server.Start(ip, port); err != nil { diff --git a/internal/env/env.go b/internal/env/env.go index 4e0a8638..3c644602 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -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. diff --git a/internal/env/env_test.go b/internal/env/env_test.go index 3032e338..2e8d131f 100644 --- a/internal/env/env_test.go +++ b/internal/env/env_test.go @@ -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) { @@ -32,6 +33,7 @@ func TestEnvVariable_Lookup(t *testing.T) { {giveEnv: ShowDetails}, {giveEnv: ProxyHTTPHeaders}, {giveEnv: DisableL10n}, + {giveEnv: CatchAll}, } for _, tt := range cases { diff --git a/internal/http/server.go b/internal/http/server.go index 7ba0c529..8d7f2973 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -107,7 +107,13 @@ func (s *Server) Register(cfg *config.Config, templatePicker templatePicker, opt 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) + } return nil } diff --git a/internal/options/errorpage.go b/internal/options/errorpage.go index 873c0129..7516bcf9 100644 --- a/internal/options/errorpage.go +++ b/internal/options/errorpage.go @@ -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 } From bb70ad0af30fc860c484f832a8ce8f6db4e8ddc8 Mon Sep 17 00:00:00 2001 From: Nicolai Rybnikar Date: Thu, 31 Aug 2023 10:47:13 +0200 Subject: [PATCH 2/2] Added link to PR to changelog --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48487643..cd0a00e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,13 @@ 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 +## UNRELEASED ### Added -- Added possibility catch all paths with error page 404 (using `--catch-all` flag for the `serve` or environment variable `CATCH_ALL=true`) +- 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