/
cron.go
64 lines (50 loc) · 1.17 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Package cron implements custom predicates to match routes
only when they also match the system time matches the given
cron-like expressions.
Package includes a single predicate: Cron.
For supported & unsupported features refer to the "cronmask" package
documentation (https://github.com/sarslanhan/cronmask).
*/
package cron
import (
"github.com/sarslanhan/cronmask"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
"net/http"
"time"
)
type clock func() time.Time
type spec struct {
}
func (*spec) Name() string {
return predicates.CronName
}
func (*spec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) != 1 {
return nil, predicates.ErrInvalidPredicateParameters
}
expr, ok := args[0].(string)
if !ok {
return nil, predicates.ErrInvalidPredicateParameters
}
mask, err := cronmask.New(expr)
if err != nil {
return nil, err
}
return &predicate{
mask: mask,
getTime: time.Now,
}, nil
}
type predicate struct {
mask *cronmask.CronMask
getTime clock
}
func (p *predicate) Match(r *http.Request) bool {
now := p.getTime()
return p.mask.Match(now)
}
func New() routing.PredicateSpec {
return &spec{}
}