Skip to content

Instantly share code, notes, and snippets.

@blacksheep--
Created March 11, 2020 12:26
Show Gist options
  • Save blacksheep--/bab37452ce913083df958b6ddbd56f51 to your computer and use it in GitHub Desktop.
Save blacksheep--/bab37452ce913083df958b6ddbd56f51 to your computer and use it in GitHub Desktop.
Traefik Catch-All to return HTTP421 for unresolved routes
// inspired by https://github.com/mergermarket/404
package main
import "net/http"
func healthcheck_handler(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
}
func default_handler(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusMisdirectedRequest)
}
func main() {
http.HandleFunc("/internal/healthcheck", healthcheck_handler)
http.HandleFunc("/", default_handler)
http.ListenAndServe(":80", nil)
}
version: "2.1"
services:
catchall:
restart: always
build:
context: .
networks:
- web
labels:
- "traefik.enable=true"
- "traefik.docker.network=web"
- "traefik.http.routers.catchall_router.entrypoints=web"
- "traefik.http.routers.catchall_router.rule=HostRegexp:(`{catchall:.*}`)" # <<---
- "traefik.http.routers.catchall_router.priority=1" # <<---
- "traefik.http.routers.catchall_router.service=catchall_service@docker"
- "traefik.http.routers.catchall_router_ssl.entrypoints=web-secure"
- "traefik.http.routers.catchall_router_ssl.rule=HostRegexp:(`{catchall:.*}`)" # <<---
- "traefik.http.routers.catchall_router_ssl.priority=1" # <<---
- "traefik.http.routers.catchall_router_ssl.tls=true"
- "traefik.http.routers.catchall_router_ssl.service=catchall_service@docker"
- "traefik.http.services.catchall_service.loadbalancer.server.port=80"
networks:
web:
external: true
FROM golang:1.14-alpine
COPY . /go/src/app
WORKDIR /go/src/app
RUN go get -v -d
RUN go install
CMD ["app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment