-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.go
107 lines (90 loc) · 2.71 KB
/
common.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package sparkypmtatracking
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/go-redis/redis"
"gopkg.in/natefinch/lumberjack.v2" // timed rotating log handler
)
// ConsoleAndLogFatal writes error to both log and stdout
func ConsoleAndLogFatal(s ...interface{}) {
fmt.Println(s...)
log.Fatalln(s...)
}
// MyLogger sets up a custom logger, if filename is given, emitting to stdout as well
// If filename is blank string, then output is stdout only
func MyLogger(filename string) {
if filename != "" {
log.SetOutput(&lumberjack.Logger{
Filename: filename,
MaxAge: 7, //days
Compress: true, // disabled by default
})
}
}
// GetenvDefault returns an environment variable, with default if unset
func GetenvDefault(k string, d string) string {
x := os.Getenv(k)
if x == "" {
x = d
}
return x
}
//-----------------------------------------------------------------------------
// HostCleanup returns a SparkPost host address in canonical form (with schema, without /api/v1 path)
func HostCleanup(host string) string {
if !strings.HasPrefix(host, "https://") {
host = "https://" + host // Add schema
}
host = strings.TrimSuffix(host, "/")
host = strings.TrimSuffix(host, "/api/v1")
host = strings.TrimSuffix(host, "/")
return host
}
//-----------------------------------------------------------------------------
// PositionIn returns the position of a value within an array of strings, if an element exactly matches val
func PositionIn(arr []string, val string) (int, bool) {
for i, v := range arr {
if v == val {
return i, true
}
}
return 0, false
}
// Contains returns whether arr contains an element exactly matching val
func Contains(arr []string, val string) bool {
_, found := PositionIn(arr, val)
return found
}
// SafeStringToInt logs an error and returns zero if it can't convert
func SafeStringToInt(s string) int {
if s == "" {
return 0
}
i, err := strconv.Atoi(s)
if err != nil {
log.Println("Warning: cannot convert", s, "to int")
i = 0
}
return i
}
//-----------------------------------------------------------------------------
// RedisQueue connects the tracker and feeder tasks
const RedisQueue = "trk_queue"
// RedisAcctHeaders holds the PowerMTA accounting file headers
const RedisAcctHeaders = "acct_headers"
// TrackingPrefix is the prefix for keys holding augmentation data
const TrackingPrefix = "msgID_"
// MsgIDTTL defines the time-to-live for augmentation data
const MsgIDTTL = time.Duration(time.Hour * 24 * 10)
// MyRedis returns a client handle for Redis, for server the standard port
func MyRedis() (client *redis.Client) {
return redis.NewClient(&redis.Options{
Addr: ":6379",
Password: "", // no password set
DB: 0, // use default DB
})
}