-
Notifications
You must be signed in to change notification settings - Fork 40
/
config.go
80 lines (68 loc) · 1.87 KB
/
config.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package tracker
import (
"github.com/pirsch-analytics/pirsch/v6/pkg/db"
"github.com/pirsch-analytics/pirsch/v6/pkg/tracker/geodb"
"github.com/pirsch-analytics/pirsch/v6/pkg/tracker/ip"
"github.com/pirsch-analytics/pirsch/v6/pkg/tracker/session"
"github.com/pirsch-analytics/pirsch/v6/pkg/util"
"log/slog"
"net"
"os"
"runtime"
"time"
)
const (
defaultWorkerBufferSize = 500
defaultWorkerTimeout = time.Second * 5
maxWorkerTimeout = time.Second * 60
defaultMaxPageViews = uint16(200)
)
// Config is the configuration for the Tracker.
type Config struct {
Store db.Store
Salt string
FingerprintKey0 uint64
FingerprintKey1 uint64
Worker int
WorkerBufferSize int
WorkerTimeout time.Duration
SessionCache session.Cache
HeaderParser []ip.HeaderParser
AllowedProxySubnets []net.IPNet
MaxPageViews uint16
GeoDB *geodb.GeoDB
IPFilter ip.Filter
LogIP bool
Logger *slog.Logger
}
func (config *Config) validate() {
if config.Salt == "" {
config.Salt = util.RandString(20)
}
if config.FingerprintKey0 == 0 {
config.FingerprintKey0 = util.RandUint64()
}
if config.FingerprintKey1 == 0 {
config.FingerprintKey1 = util.RandUint64()
}
if config.Worker < 1 {
config.Worker = runtime.NumCPU()
}
if config.WorkerBufferSize < 1 {
config.WorkerBufferSize = defaultWorkerBufferSize
}
if config.WorkerTimeout <= 0 {
config.WorkerTimeout = defaultWorkerTimeout
} else if config.WorkerTimeout > maxWorkerTimeout {
config.WorkerTimeout = maxWorkerTimeout
}
if config.SessionCache == nil {
config.SessionCache = session.NewMemCache(config.Store, 0)
}
if config.MaxPageViews == 0 {
config.MaxPageViews = defaultMaxPageViews
}
if config.Logger == nil {
config.Logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
}
}