Caddyfile use env.VAR in json

1. Caddy version (caddy version):

./caddy version
v2.5.1 h1:bAWwslD1jNeCzDa+jDCNwb8M3UJ2tPa8UZFFzPVmGKs=

2. How I run Caddy:

APPPORT=:8888 ./caddy run -config docker-files/opt/webroot/config/Caddyfile
# or
APPPORT=:8888 ./caddy run -adapter jsonc -config config/Caddyfile-upload.json

a. System environment:

lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.4 LTS
Release:	20.04
Codename:	focal

b. Command:

APPPORT=:8888 ./caddy run -config docker-files/opt/webroot/config/Caddyfile
# or
APPPORT=:8888 ./caddy run -adapter jsonc -config config/Caddyfile-upload.json

c. Service/unit/compose file:

podman run --rm \
  --network host \
  --name caddy-test \
  --env APPPORT=:8888 \
  -it \
  --entrypoint /bin/bash \
  docker.io/me2digital/caddyv2-upload:0.5

# in the container
caddy run -adapter jsonc -config config/Caddyfile-upload.json

d. My complete Caddyfile or JSON config:

# https://github.com/git001/caddyv2-upload/blob/main/docker-files/opt/webroot/config/Caddyfile
{
	order upload before file_server
	log {
		level DEBUG
	}

}

{$APPPORT} {
	root .

	file_server browse
	templates

	@mypost method POST
	upload @mypost {
		dest_dir tmp-upl
		max_form_buffer 1G
		max_filesize 4MB
		response_template templates/upload-resp-template.txt
	}

	log {
		output file access.log
	}
}

# https://github.com/git001/caddyv2-upload/blob/main/docker-files/opt/webroot/config/Caddyfile-upload.json
{
  "admin": {
    "listen": "127.0.0.1:8081"
  },
  "apps": {
    "http": {
      "grace_period": "30m",
      "servers": {
        "example": {
          "listen": ["{env.APPPORT}"],
          "logs": {
            "default_logger_name": "myaccesslogger"
          },
          "routes": [
            {
              "match":[
                {
                  "method": ["GET"],
                  "path": ["/print-headers-template.txt"]
                  
                }
              ],
              "handle": [
                {
                  "handler": "templates"
                },
                {
                  "handler": "headers",
                  "response": {
                    "set": {
                      "Content-Type": ["text/plain; charset=utf-8"]
                    }
                  }
                },
                {
                  "handler": "file_server",
                  "root": "templates"
                }
              ]
            },
            {
              "match":[
                {
                  "method": ["GET"],
                  "path": ["/upload-template.html"]
                }
              ],
              "handle": [
                {
                  "handler": "templates"
                },
                {
                  "handler": "file_server",
                  "root": "templates"
                }
              ]
            },
            {
              "match":[
                {
                  "method": ["GET"]
                }
              ],
              "handle": [
                {
                  "handler": "templates"
                },
                {
                  "handler": "file_server",
                  "browse": 
                    {
                    }
                }
              ]
            },
            {
              "match":[
                {
                  "method": ["POST"]
                }
              ],
              "handle": [
                {
                  "handler": "templates"
                },
                {
                  "handler": "headers",
                  "response": {
                    "set": {
                      "Content-Type": ["text/plain; charset=utf-8"]
                    }
                  }
                },
                {
                  "handler": "upload",
                  "dest_dir": "tmp-upl",
                  "max_filesize": 1024000,
                  "response_template":"upload-resp-template.txt"
                },
                {
                  "handler": "file_server",
                  "root": "templates"
                }
              ]
            }
          ]
        }
      }
    }
  },
  "logging": {
    "logs": {
      "default": {
        "level": "INFO"
      },
      "myaccesslogger": {
        "writer": {
          "output": "stdout"
        },
        "encoder": {
          "format": "json"
        }
      }
    }
  }
}

3. The problem I’m having:

I would like to configure in the Caddyfile-upload.json also the “max_filesize 4MB” similar to the Caddyfile and that via “{env.MAXFILESIZE}” is this possible or do I need to use a own adapter for this?

4. Error messages and/or full log output:

5. What I already tried:

I tried to use ““max_filesize”: “{env.MAXFILESIZE}”” but got a json parsing error.

6. Links to relevant resources:

1 Like

The plugin needs to run the replacer on that config value. You can grab the caddy.Replacer out of the request context (if you can replace it at runtime during requests) or in your Provision by getting a replacer with caddy.NewReplacer().

1 Like

Thank you for your help.

I have now add the “humanize.ParseBytes” in the provisioner.

and the replacer for the json config at provision stage

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.