adding resuable snippets
(logging) {
log {
level DEBUG
format console
}
}
{
admin off
auto_https off
import logging
}
#Variables in Caddyfile
&(dweb3-api) {
(snippets) {
log {
level DEBUG
format console
}
}
# Match requests that dont start with a name and tld (assets, subpaths, etc)
@no_domain {
path_regexp no_domain ^/([^/.]+/|[^/]+$)
}
# Match requests that need a trailing slash
@missingSlash {
not path */ # Skip requests that already end with /
not path *.* # Skip requests for files (e.g., .js, .css, etc.)
}
# Match requests that end with any domain in DomainEnum and do not have a trailing slash
# Redirect to ensure trailing slash
# Redirect domain requests to ensure trailing slash
handle @domainWithoutSlash {
redir {path}/ 301
}
handle @no_domain {
@has_referer header Referer http://localhost:8000/*
handle @has_referer {
reverse_proxy localhost:3000 {
transport http
method GET
# Pass a static value indicating that we have a referer
header_up X-Id-Name hasReferer
# Redirect to X-Content-Path in case we don't have a domain and its the html call
@redirect301 status 301
handle_response @redirect301 {
redir {rp.header.X-Content-Path} permanent
}
# Reverse proxy to ipfs
@proxy status 200
handle_response @proxy {
reverse_proxy {rp.header.X-Content-Location} {
rewrite {rp.header.X-Content-Path}
header_up Host {rp.header.X-Content-Location}
header_up -X-Forwarded-Host
header_up X-Id-Name {rp.header.X-Id-Name}
header_up X-Id-Cid {rp.header.X-Id-Cid}
transport http {
dial_timeout 2s
}
@redirect301 status 301
handle_response @redirect301 {
redir {rp.header.Location} permanent
}
}
}
}
}
}
reverse_proxy localhost:3000 {
transport http
method GET
header_up X-Id-Name {http.request.uri.path.0}
@redirect301 status 301
handle_response @redirect301 {
redir {rp.header.X-Content-Path} permanent
}
@proxy status 200
handle_response @proxy {
reverse_proxy {rp.header.X-Content-Location} {
rewrite {rp.header.X-Content-Path}
header_up Host {rp.header.X-Content-Location}
header_up -X-Forwarded-Host
header_up X-Id-Name {rp.header.X-Id-Name}
header_up X-Id-Cid {rp.header.X-Id-Cid}
transport http {
dial_timeout 2s
}
@redirect301 status 301
handle_response @redirect301 {
redir {rp.header.Location} permanent
}
}
}
}
}
:8000 {
import logging
bind 0.0.0.0
invoke dweb3-api
}
This part is repeated two times after two differnt conditions.
@proxy status 200
handle_response @proxy {
reverse_proxy {rp.header.X-Content-Location} {
rewrite {rp.header.X-Content-Path}
header_up Host {rp.header.X-Content-Location}
header_up -X-Forwarded-Host
header_up X-Id-Name {rp.header.X-Id-Name}
header_up X-Id-Cid {rp.header.X-Id-Cid}
transport http {
dial_timeout 2s
}
@redirect301 status 301
handle_response @redirect301 {
redir {rp.header.Location} permanent
}
}
}
I tried to make it a snippet to reuse it but Caddy throws errors. Does anyone know how to solve this issue? How to make this part reusable?