-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add extra.time.now.utc.* placeholders
- Loading branch information
1 parent
ff40b6d
commit b66c4b0
Showing
7 changed files
with
228 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Steffen Busch | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extraplaceholders | ||
|
||
import ( | ||
"github.com/caddyserver/caddy/v2" | ||
) | ||
|
||
// setCaddyPlaceholders sets placeholders for the Caddy version. | ||
func (e ExtraPlaceholders) setCaddyPlaceholders(repl *caddy.Replacer) { | ||
simpleVersion, fullVersion := caddy.Version() | ||
repl.Set("extra.caddy.version.simple", simpleVersion) | ||
repl.Set("extra.caddy.version.full", fullVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Steffen Busch | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extraplaceholders | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/shirou/gopsutil/v4/host" | ||
) | ||
|
||
// setHostinfoPlaceholders sets placeholders for system uptime in a human-readable format. | ||
func (e ExtraPlaceholders) setHostinfoPlaceholders(repl *caddy.Replacer) { | ||
uptime, err := host.Uptime() | ||
if err == nil { | ||
uptimeDuration := time.Duration(uptime) * time.Second | ||
repl.Set("extra.hostinfo.uptime", uptimeDuration.String()) | ||
} else { | ||
repl.Set("extra.hostinfo.uptime", "error retrieving uptime") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 Steffen Busch | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extraplaceholders | ||
|
||
import ( | ||
"github.com/caddyserver/caddy/v2" | ||
"github.com/shirou/gopsutil/v4/load" | ||
) | ||
|
||
// setLoadavgPlaceholders sets placeholders for system load averages (1, 5, and 15 minutes). | ||
func (e ExtraPlaceholders) setLoadavgPlaceholders(repl *caddy.Replacer) { | ||
loadAvg, err := load.Avg() | ||
if err == nil { | ||
repl.Set("extra.loadavg.1", loadAvg.Load1) | ||
repl.Set("extra.loadavg.5", loadAvg.Load5) | ||
repl.Set("extra.loadavg.15", loadAvg.Load15) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Steffen Busch | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extraplaceholders | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
) | ||
|
||
// setRandPlaceholders sets placeholders for random float and integer values. | ||
func (e ExtraPlaceholders) setRandPlaceholders(repl *caddy.Replacer) { | ||
repl.Set("extra.rand.float", rand.Float64()) | ||
if e.RandIntMax > e.RandIntMin { | ||
repl.Set("extra.rand.int", rand.Intn(e.RandIntMax-e.RandIntMin+1)+e.RandIntMin) | ||
} else { | ||
repl.Set("extra.rand.int", rand.Intn(101)) // Default range 0-100 if not properly configured | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package extraplaceholders | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
) | ||
|
||
// setTimePlaceholders sets placeholders for date, time, and custom format, | ||
// using the provided time.Time. If isUTC is true, ".utc" is added in the placeholder path. | ||
func (e ExtraPlaceholders) setTimePlaceholders(repl *caddy.Replacer, t time.Time, isUTC bool) { | ||
// Determine the base path, with or without ".utc" | ||
base := "extra.time.now" | ||
if isUTC { | ||
base += ".utc" | ||
} | ||
|
||
// 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())) | ||
repl.Set(fmt.Sprintf("%s.day", base), t.Day()) | ||
repl.Set(fmt.Sprintf("%s.day_padded", base), fmt.Sprintf("%02d", t.Day())) | ||
repl.Set(fmt.Sprintf("%s.hour", base), t.Hour()) | ||
repl.Set(fmt.Sprintf("%s.hour_padded", base), fmt.Sprintf("%02d", t.Hour())) | ||
repl.Set(fmt.Sprintf("%s.minute", base), t.Minute()) | ||
repl.Set(fmt.Sprintf("%s.minute_padded", base), fmt.Sprintf("%02d", t.Minute())) | ||
repl.Set(fmt.Sprintf("%s.second", base), t.Second()) | ||
repl.Set(fmt.Sprintf("%s.second_padded", base), fmt.Sprintf("%02d", t.Second())) | ||
|
||
// Set timezone offset and name | ||
repl.Set(fmt.Sprintf("%s.timezone_offset", base), t.Format("-0700")) | ||
repl.Set(fmt.Sprintf("%s.timezone_name", base), t.Format("MST")) | ||
|
||
// Set ISO week and year components | ||
isoYear, isoWeek := t.ISOWeek() | ||
repl.Set(fmt.Sprintf("%s.iso_week", base), isoWeek) | ||
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)) | ||
} |