caddyserver / caddy

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

consider allowing global configuration in imported files #6401

Closed marten-seemann closed 2 weeks ago

marten-seemann commented 2 weeks ago

Setup

I'm trying to run with a modular config. I have a very short Caddyfile that imports the site-specific configurations from included Caddyfiles.

{
   // some global options
}

import sites-enabled/*.Caddyfile

This allows me to easily move sites from one server instance to the other, by (basically) just moving the site-specific Caddyfile from one server to the other.

Some of the sites use the Git plugin to automatically fetch and update the code for the sites. The Git plugin requires repos to be configured globally, it's not possible (or at least I haven't figured out how) to have a git config in a site block.

Naturally, to keep the setup modular, the git configuration should be defined in the site-specific Caddyfiles, not in the top-level one.

The Problem

Caddy really, really doesn't like any global configuration that's not first:

Error: adapting config using caddyfile: server block without any key is global configuration, and if used, it must be first

This even applies if there's only one imported file, and the global option is the first directive in the imported file. This means it's not even possible to build the following structure:

{
   // some global options
}

import sites-enabled/*_globals.Caddyfile // only global config options, no site blocks
import sites-enabled/*_sites.Caddyfile   // site blocks

Possible Solutions

I'm not very familiar with the intricacies of the design of the Caddyfile, so please let me know if I'm missing something here. I can see two solutions:

  1. Relax the requirement for global options, such that they're allowed in imported files. As suggested in the last section, this doesn't mean allowing global options at any place in the file, but would make it possible to import all files that define global options first.
  2. Maybe this is not a problem in Caddy at all, but should be resolved in the Git plugin itself? I'm not sure if there's any reason that the git config directive needs to be global.
francislavoie commented 2 weeks ago

You can do this though:

{
    // some global options
    import sites-enabled/*_globals.Caddyfile
}

import sites-enabled/*_sites.Caddyfile

Thing is, import is literally just doing copy paste from one file into the main Caddyfile. It's not any smarter than that. We'd have to pretty significantly complicate the parser.

marten-seemann commented 2 weeks ago

It works! Thank you for the quick response, @francislavoie!