Skip to content

Commit

Permalink
Placeholder support for subdirective time_format_custom
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenbusch committed Oct 27, 2024
1 parent b66c4b0 commit 3b57290
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ extra_placeholders {

If `time_format_custom` is not specified, it defaults to `"2006-01-02 15:04:05"`. This format will be applied to both `{extra.time.now.custom}` (server’s local timezone) and `{extra.time.now.utc.custom}` (UTC time) placeholders.

#### Placeholder Support within `time_format_custom`

You can also specify placeholders within `time_format_custom`. For example, if you want the format to depend on an environment variable or request data, use `{env.*}` or `{http.request.*}` placeholders:

```caddyfile
extra_placeholders {
time_format_custom {http.request.uri.query.format}
}
```

In this example, if a request contains a query parameter like `?format=02.01.2006`, the `{http.request.uri.query.format}` placeholder will dynamically resolve to `02.01.2006` for that specific request, which then sets the date format for `{extra.time.now.custom}`. This feature allows you to adjust time formatting based on user input, query parameters, or environment variables, creating a context-sensitive custom format.

> [!NOTE]
> When using placeholders in `time_format_custom`, ensure that the placeholder content aligns with [Go's time format syntax](https://pkg.go.dev/time#pkg-constants) to avoid formatting issues.
### 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:
Expand Down
5 changes: 4 additions & 1 deletion extra_placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"go.uber.org/zap"
)

// defaultTimeFormatCustom is the fallback format used if no custom format is specified for the custom time placeholders.
const defaultTimeFormatCustom = "2006-01-02 15:04:05"

// ExtraPlaceholders provides additional placeholders that can be used within Caddy configurations:
//
// Placeholder | Description
Expand Down Expand Up @@ -107,7 +110,7 @@ func (e *ExtraPlaceholders) Provision(ctx caddy.Context) error {
e.RandIntMax = 100
}
if e.TimeFormatCustom == "" {
e.TimeFormatCustom = "2006-01-02 15:04:05" // Default format for custom time placeholder
e.TimeFormatCustom = defaultTimeFormatCustom
}

// Log the chosen configuration values
Expand Down
6 changes: 5 additions & 1 deletion placeholder_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (e ExtraPlaceholders) setTimePlaceholders(repl *caddy.Replacer, t time.Time
base += ".utc"
}

// Placeholder support
// Dynamically resolve the time format using the replacer
timeFormatCustom := repl.ReplaceAll(e.TimeFormatCustom, defaultTimeFormatCustom)

// Set date and time components with the specified base path
repl.Set(fmt.Sprintf("%s.month", base), int(t.Month()))
repl.Set(fmt.Sprintf("%s.month_padded", base), fmt.Sprintf("%02d", t.Month()))
Expand All @@ -38,5 +42,5 @@ func (e ExtraPlaceholders) setTimePlaceholders(repl *caddy.Replacer, t time.Time
repl.Set(fmt.Sprintf("%s.iso_year", base), isoYear)

// Set custom time format placeholder
repl.Set(fmt.Sprintf("%s.custom", base), t.Format(e.TimeFormatCustom))
repl.Set(fmt.Sprintf("%s.custom", base), t.Format(timeFormatCustom))
}

0 comments on commit 3b57290

Please sign in to comment.