1

I'm trying to get Caddy server to display a file that is kind of dynamic (assigned to a variable). I find the official documentation a bit opaque and searching Internets doesn't return much info either.

My example Caddyfile config:

example.com {
  route / {
    # own plugin that adds custom headers to the request
    my_plugin do_something

    map {header.something} {my_file} {
      x "x.html"
      y "y.html"
      default "404.html"
    }

    file_server {
      browse {my_file}
    }
  }
}

So the idea is Caddy displays "y.html" template if the request contains "something" with value "y". You get the picture.

However Caddy will complain with: http.log.error parsing browse template: parsing browse template file: open {my_file}: no such file or directory

Looks like it takes the "{my_file}" in literal matters(!).

How does one gets this done in Caddy?

N.B Other examples such as redir {my_file} work and redirect to "example.com/y.html".

Or is there better way in general to display a template "from a variable" ?

templates {my_file} does not work.

mlen108
  • 456
  • 1
  • 4
  • 12

1 Answers1

1

Okay, I figured it out. Eventually.

Simple yet not-so-obvious syntax:

*.example.com {
  map {labels.2} {my_file} {
    x       "x.html"
    default "404.html"
  }

  root * tmpl
  file_server
  templates

  try_files {my_file}
}

So if the domain is x.example.com it will then render the x.html template located in current working directory's tmpl folder. The templates directive is only needed if the template(s) contain any functions/actions (e.g. include).

mlen108
  • 456
  • 1
  • 4
  • 12