This repository contains the Extra Placeholders plugin for the Caddy server. This plugin provides additional placeholders that can be used for enhanced server-side information, such as system load averages, random numbers, Caddy version details, and system uptime.
Features
This plugin introduces new placeholders that can be used within Caddy configurations:
| Placeholder |
Description |
{extra.caddy.version.simple} |
Simple version information of the Caddy server. |
{extra.caddy.version.full} |
Full version information of the Caddy server. |
{extra.rand.float} |
Random float value between 0.0 and 1.0. |
{extra.rand.int} |
Random integer value between the configured min and max (default is 0 to 100). |
{extra.loadavg.1} |
System load average over the last 1 minute. |
{extra.loadavg.5} |
System load average over the last 5 minutes. |
{extra.loadavg.15} |
System load average over the last 15 minutes. |
{extra.hostinfo.uptime} |
System uptime in a human-readable format. |
These placeholders can be used in Caddyfiles to provide dynamic content and system information in responses.
Building
To build Caddy with this module, use xcaddy:
$ xcaddy build --with github.com/steffenbusch/caddy-extra-placeholders
Caddyfile config
By default, the extra_placeholders directive is ordered before redir in the Caddyfile. This simplifies configuration and removes the need for manual ordering in most cases.
However, if this default order does not suit your needs, you can still change the order using the order global directive.
To use the extra placeholders, you can add the following directive to your Caddyfile:
:8080 {
extra_placeholders {
rand_int 10 50
}
respond "Caddy Version: {extra.caddy.version.full}, Uptime: {extra.hostinfo.uptime}, Random Int: {extra.rand.int}"
}
This example demonstrates how to use the additional placeholders provided by this plugin to dynamically insert Caddy version information, system uptime, and a random integer between 10 and 50 into an HTTP response.
Random Integer Configuration
To configure the range for the {extra.rand.int} placeholder, use the rand_int subdirective inside the extra_placeholders directive. The format is:
extra_placeholders {
rand_int <min> <max>
}
<min>: The minimum value for the random integer (inclusive).
<max>: The maximum value for the random integer (inclusive).
If rand_int is not specified, the default values are:
RandIntMin = 0
RandIntMax = 100
This means that {extra.rand.int} will default to generating a random integer between 0 and 100 if not explicitly configured.
Example: Conditional Redirect Based on Random Value
The following example demonstrates how you can use conditional expressions with the random integer placeholder to redirect users to different search engines based on the generated random number:
:8080 {
extra_placeholders {
rand_int 1 100
}
@redirectToGoogle expression `{extra.rand.int} <= 25`
redir @redirectToGoogle https://www.google.com
@redirectToBing expression `{extra.rand.int} > 25 && {extra.rand.int} <= 50`
redir @redirectToBing https://www.bing.com
@redirectToYahoo expression `{extra.rand.int} > 50 && {extra.rand.int} <= 75`
redir @redirectToYahoo https://www.yahoo.com
@redirectToDuckDuckGo expression `{extra.rand.int} > 75`
redir @redirectToDuckDuckGo https://www.duckduckgo.com
}
In this example:
This example demonstrates how to use the random integer placeholder in combination with conditional expressions to create dynamic redirection rules.
License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Acknowledgements
- Caddy for providing a powerful and extensible web server.
- gopsutil for system metrics, such as load averages and uptime, used under the BSD 3-Clause License.