mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
reject by timestamp using string
This commit is contained in:
parent
e6188796d2
commit
158f284be9
@ -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
|
||||||
|
|
||||||
|
@ -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")
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user