reject by timestamp using string

This commit is contained in:
0ceanSlim 2024-10-18 11:33:23 -04:00
parent e6188796d2
commit 158f284be9
3 changed files with 15 additions and 4 deletions

View File

@ -16,6 +16,7 @@ server:
event_time_constraints: event_time_constraints:
min_created_at: 1577836800 # January 1, 2020, as Unix timestamp min_created_at: 1577836800 # January 1, 2020, as Unix timestamp
# min_created_at_string: now-5m # Custom value to indicate 5 minutes in the past
# max_created_at: 0 # Set to 0 to use the default behavior of 'now' # max_created_at: 0 # Set to 0 to use the default behavior of 'now'
max_created_at_string: now+5m # Use a string to set a date for max created at in the future or past from current time max_created_at_string: now+5m # Use a string to set a date for max created at in the future or past from current time

View File

@ -2,6 +2,7 @@ package config
type EventTimeConstraints struct { type EventTimeConstraints struct {
MinCreatedAt int64 `yaml:"min_created_at"` // Minimum allowed timestamp MinCreatedAt int64 `yaml:"min_created_at"` // Minimum allowed timestamp
MinCreatedAtString string `yaml:"min_created_at_string"` // Original string value for parsing (e.g., "now-5m")
MaxCreatedAt int64 `yaml:"max_created_at"` // Maximum allowed timestamp MaxCreatedAt int64 `yaml:"max_created_at"` // Maximum allowed timestamp
MaxCreatedAtString string `yaml:"max_created_at_string"` // Original string value for parsing (e.g., "now+5m") MaxCreatedAtString string `yaml:"max_created_at_string"` // Original string value for parsing (e.g., "now+5m")
} }

View File

@ -11,14 +11,23 @@ import (
func AdjustEventTimeConstraints(cfg *config.ServerConfig) { func AdjustEventTimeConstraints(cfg *config.ServerConfig) {
now := time.Now() now := time.Now()
// Adjust min_created_at (no changes needed if it's already set in the config) // Adjust min_created_at based on string value or default to January 1, 2020
if cfg.EventTimeConstraints.MinCreatedAt == 0 { if strings.HasPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now") {
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now")
duration, err := time.ParseDuration(offset)
if err != nil {
fmt.Printf("Invalid time offset for min_created_at: %s\n", offset)
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
} else {
cfg.EventTimeConstraints.MinCreatedAt = now.Add(duration).Unix()
}
} else if cfg.EventTimeConstraints.MinCreatedAt == 0 {
// Default to January 1, 2020, if not set
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix() cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
} }
// Adjust max_created_at // Adjust max_created_at based on string value or default to current time
if strings.HasPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") { if strings.HasPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") {
// Extract the offset (e.g., "+5m")
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") offset := strings.TrimPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now")
duration, err := time.ParseDuration(offset) duration, err := time.ParseDuration(offset)
if err != nil { if err != nil {