-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
126 lines (104 loc) · 2.56 KB
/
utils.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"github.com/joho/godotenv"
"log"
"os"
"strings"
"time"
)
var (
ApiPort string
TwitterToken string
PostgresUrl string
ServerTz string
ServerlessSenderUrl string
RssPollingRate time.Duration
TwitterPollingRate time.Duration
AlmanaxPollingRate time.Duration
SendBatchEnabled bool
)
func ReadEnvs() {
gopath := getEnv("GOPATH", ".")
envDir := getEnv("ENV_DIR", gopath+"/src/github.com/dofusdude/ankama-discord-hooks")
err := godotenv.Load(envDir + "/.env")
if err != nil {
log.Println("Could not find .env file, loading from env variables")
}
ApiPort = getEnv("API_PORT", "3000")
if RssPollingRate, err = time.ParseDuration(getEnv("RSS_POLLING_RATE", "5m")); err != nil {
log.Fatal("could not convert RSS_POLLING_RATE", err)
}
if TwitterPollingRate, err = time.ParseDuration(getEnv("TWITTER_POLLING_RATE", "5m")); err != nil {
log.Fatal("could not convert TWITTER_POLLING_RATE", err)
}
if AlmanaxPollingRate, err = time.ParseDuration(getEnv("ALMANAX_POLLING_RATE", "1m")); err != nil {
log.Fatal("could not convert ALMANAX_POLLING_RATE", err)
}
TwitterToken = getEnv("TWITTER_TOKEN", "undefined")
PostgresUrl = getEnv("POSTGRES_URL", "")
if PostgresUrl == "" {
log.Fatal("POSTGRES_URL is not defined.")
}
ServerTz = getEnv("SERVER_TZ", "Europe/Berlin")
}
func TruncateText(s string, max int) string {
if max > len(s) {
return s
}
return s[:strings.LastIndex(s[:max], " ")] + " ..."
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func sliceContains[T comparable](s []T, str T) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// based on string only https://gist.github.com/bgadrian/cb8b9344d9c66571ef331a14eb7a2e80
// rewritten to be generic
type Set[T comparable] struct {
list map[T]struct{} //empty structs occupy 0 memory
}
func (s *Set[T]) Has(v T) bool {
_, ok := s.list[v]
return ok
}
func (s *Set[T]) Add(v T) {
s.list[v] = struct{}{}
}
func (s *Set[T]) Remove(v T) {
delete(s.list, v)
}
func (s *Set[T]) Clear() {
s.list = make(map[T]struct{})
}
func (s *Set[T]) Size() int {
return len(s.list)
}
func NewSet[T comparable]() *Set[T] {
s := &Set[T]{}
s.list = make(map[T]struct{})
return s
}
func (s *Set[T]) Slice() []T {
var res []T
for v := range s.list {
res = append(res, v)
}
return res
}
// fp
func Map[T, U any](data []T, f func(T) U) []U {
res := make([]U, 0, len(data))
for _, e := range data {
res = append(res, f(e))
}
return res
}