A Traefik middleware plugin for responding to requests inline without a backend.
This allows you to make Traefik act as a request handler with a custom set of rules that can be configured and without the need for a backend.
The responses to the requests are configurable based on the path of the request. The response status code and the body are also configurable. Responses can be static or dynamic based on the specified template.
The Traefik plugins doc can be used as the general reference for installing the plugin.
experimental:plugins:inlineResponse:moduleName: github.com/tuxgal/traefik_inline_responseversion: v0.1.2
http:routers:to-local-backend:rule: 'HostRegexp(`^.*$`)'service: local-backendmiddlewares:- inline-responsemiddlewares:inline-response:plugin:inlineResponse:matchers:- path:abs: /path1statusCode: 200response:raw: Hello from /path1- path:prefix: /path2statusCode: 200response:json:name: traefikcategory: pluginhealth:frontend: okmiddleware: okbackend: ok- path:regex: '^.*/path3/foo/.*$'statusCode: 403response:template: '{{ .Method }}-{{ .URL.Scheme }}-{{ .URL.Host }}-{{ .URL.Path }}'- path:regex: '^/path4/.+$'statusCode: 405fallback:statusCode: 404response:template: '{{ .Proto }} {{ .URL.Path }} Not Found'services:local-backend:loadBalancer:servers:- url: noop@internal
Note, how we configure the router to send the request to a service whose
backend is noop@internal
a special no-op traefik backend. When the plugin
is able to match and respond to the requests, the requests do not reach
this special backend at all.
In the above example, the middleware will respond to the requests based on the following rules in order without the need for any backend service:
Any request paths matching the absolute path /path1
will return a
response with 200
status code and body Hello from /path1
.
Any request paths with the prefix /path2
will return a response with
200
status code and the JSON:
{"name": "traefik","category": "plugin","health": {"frontend": "ok","middleware": "ok","backend": "ok"}}
Any request paths matching the regular expression ^.*/path3/foo/.*$
will
return a response with 403
status code and body whose result will be
the evaluation of the go template {{ .Method }}-{{ .URL.Scheme }}-{{ .URL.Host }}-{{ .URL.Path }}
with the input to the template being the
Request
type from net/http
package.
Any request paths matching the regular expression ^/path4/.+$
will
return an empty response body with status code 405
.
Requests not matching any of the above rules will be handled as per the
configuration defined under fallback
. In this case, it will lead to
a response with status code 404
with the body whose result will be
the evaluation of the go template {{ .Proto }} {{ .URL.Path }} Not Found
with the input to the template being the
Request
type from net/http
package.