Disable block via API

Hi,

Please advise how I could implement given workflow.

I would like to toggle/bypass certain blocks via API.
For example, ip blocking or maintenance mode(503).

Normally, I use Caddyfile and I comment out blocks that I would like to be omited, then uncomment to bring them back. But now I need to implement that in own admin panel.

Whats the easiest way to disable certain blocks in config via API ?

	@blocked {
		not remote_ip 123.123.123.123 private_ranges
	}

	handle @blocked {
		respond "Forbidden" 403 {
			close
		}
	}

In the example above, I would like to turn off and on handle @block by calling API.
The only thing I can think of is to use DELETE and PUT to modify config. Is there some if/else or “commenting” workflow that I missed?

Can I place @id tags in Caddyfile so I can use then via API ?

Thank you

Essentially, using the API and using a Caddyfile are designed as mutually exclusive usecases.

It’s fine to use the Caddyfile to start and then use caddy adapt to get a JSON config, but it doesn’t make sense to try to manipulate the Caddyfile’s config in-memory with the admin API, because it will not be reflected back to the Caddyfile when you do.

So basically, go all-in on the Caddyfile, or go all-in on JSON via the API.

No, either the config exists, or it doesn’t.

No, that’s a feature exclusive to the JSON config as an ergonomic aid in making quick changes.

What I would recommend doing in the case of maintenance mode is using a file that exists on disk as the condition for enabling maintenance.

@maintenance file {
	root /path/to/dir
	try_files maintenance.txt
}
handle @maintenance {
	respond 503 "Maintenance mode!"
}

So basically you can just do touch /path/to/dir/maintenance.txt and then Caddy will start serving 503s. Remove the file to turn off maintenance mode. No config reloads involved.

You could do the same kind of thing for IP blocking. Add a file condition matcher in your @blocked matcher or something.

1 Like

that’s super awesome solution to use file as a condition :rocket:

Thank you!

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