caddyserver / caddy

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
https://caddyserver.com
Apache License 2.0
58.32k stars 4.04k forks source link

Feature-Request: Placeholder with default value #1793

Open blaubaer opened 7 years ago

blaubaer commented 7 years ago

If there is no/empty for a placeholder use default/fallback value/placeholder.

Acceptance Test

Pattern

{foo:-{bar}}

Expectations

foo bar Result
John Mark John
John empty John
empty Mark Mark
empty empty empty

Usage example

proxy /api my-api:80 {
    transparent
    websocket
    without /api
    header_upstream X-Forwarded-Path {>X-Forwarded-Path:-{path}}
}

If this caddy is also behind a reverse proxy it also will set the header X-Forwarded-Path ... in this case use this header. If not set we expect to be not behind a reverse proxy and directly called ... in this case use path.

Other software which uses this pattern

https://logback.qos.ch/manual/configuration.html#defaultValuesForVariables

mholt commented 5 years ago

Will be mostly irrelevant in Caddy 2, which provides this capability through other means.

yesiyan01 commented 3 years ago

Any updates on this one?

francislavoie commented 3 years ago

@yesiyan01 what exactly are you looking for? Could you be more specific? What you want is probably possible another way.

yesiyan01 commented 3 years ago

@francislavoie

I have a reverse proxy handler that overrides authorization header e.g.

handler: reverse_proxy
headers:
  request:
    set:
      Authorization:
        - '{http.oauth1}'
transport:
   protocol: http
   tls: {}
upstreams:
   - dial: 'xxx.com:443'

where 'http.oauth1' is a custom header value set from a custom module.

I'd like the ability to use 'http.oauth1' in the placeholder but fallback to a static API-KEY when http.oauth1 is not present.

francislavoie commented 3 years ago

Thanks for the example!

The changes would need to be made to https://github.com/caddyserver/caddy/blob/master/replacer.go, if you'd like to take a crack at it.

We recently implemented defaults for environment variables in the Caddyfile, so I think we should follow suit in this case and use the same syntax, i.e. : as the delimiter for the default value.

This change would need to be thoroughly tested (see replacer_test.go)