From 3b57290e526cd083af2d7af3aa1ad27eefcec94f Mon Sep 17 00:00:00 2001 From: Steffen Busch Date: Sun, 27 Oct 2024 15:13:51 +0100 Subject: [PATCH] Placeholder support for subdirective time_format_custom --- README.md | 15 +++++++++++++++ extra_placeholders.go | 5 ++++- placeholder_time.go | 6 +++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ac385df..e78e731 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/extra_placeholders.go b/extra_placeholders.go index 1a73975..467be66 100644 --- a/extra_placeholders.go +++ b/extra_placeholders.go @@ -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 @@ -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 diff --git a/placeholder_time.go b/placeholder_time.go index 07e8196..ef39296 100644 --- a/placeholder_time.go +++ b/placeholder_time.go @@ -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())) @@ -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)) }