-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (105 loc) · 3.93 KB
/
main.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
//go:build !test
package main
import (
"context"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/tyler-cromwell/forage/api"
"github.com/tyler-cromwell/forage/clients"
"github.com/tyler-cromwell/forage/config"
)
func main() {
// Specify common fields
log := logrus.WithFields(logrus.Fields{"at": "main.main"})
// Configure logrus logging
levelStr := os.Getenv("LOGRUS_LEVEL")
if levelStr == "" {
log.Fatal("Logging level not specified")
}
level, err := logrus.ParseLevel(levelStr)
if err != nil {
log.WithFields(logrus.Fields{"level": levelStr}).WithError(err).Fatal("Failed to parse logging level")
}
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
logrus.SetLevel(level)
logrus.SetOutput(os.Stdout)
// logrus.SetReportCaller(true)
log.WithFields(logrus.Fields{"level": levelStr}).Info("Logging configured")
// Get other environment variables
contextTimeoutStr := os.Getenv("FORAGE_CONTEXT_TIMEOUT")
if contextTimeoutStr == "" {
// Default case
contextTimeoutStr = "5s"
log.WithFields(logrus.Fields{"timeout": contextTimeoutStr}).Info("Using default context timeout")
}
forageContextTimeout, err := time.ParseDuration(contextTimeoutStr)
if err != nil {
log.WithFields(logrus.Fields{"timeout": contextTimeoutStr}).WithError(err).Fatal("Failed to parse context timeout")
}
intervalStr := os.Getenv("FORAGE_INTERVAL")
if intervalStr == "" {
// Default case
intervalStr = "24h"
log.WithFields(logrus.Fields{"interval": intervalStr}).Info("Using default expiration interval")
}
lookaheadStr := os.Getenv("FORAGE_LOOKAHEAD")
if lookaheadStr == "" {
// Default case
lookaheadStr = "48h"
log.WithFields(logrus.Fields{"lookahead": lookaheadStr}).Info("Using default expiration lookahead")
}
forageTime := os.Getenv("FORAGE_TIME")
if forageTime == "" {
// Default case
forageTime = "19:00"
log.WithFields(logrus.Fields{"timezone": forageTime}).Info("Using default time")
}
forageTimezone := os.Getenv("FORAGE_TIMEZONE")
if forageTimezone == "" {
// Default case
forageTimezone = "America/New_York"
log.WithFields(logrus.Fields{"timezone": forageTimezone}).Info("Using default timezone")
}
forageLookahead, err := time.ParseDuration(lookaheadStr)
if err != nil {
log.WithFields(logrus.Fields{"lookahead": lookaheadStr}).WithError(err).Fatal("Failed to parse expiration lookahead")
}
mongoUri := os.Getenv("MONGO_URI")
listenSocket := os.Getenv("LISTEN_SOCKET")
trelloMemberID := os.Getenv("TRELLO_MEMBER")
trelloBoardName := os.Getenv("TRELLO_BOARD")
trelloListName := os.Getenv("TRELLO_LIST")
trelloLabels := os.Getenv("TRELLO_LABELS")
trelloApiKey := os.Getenv("TRELLO_API_KEY")
trelloApiToken := os.Getenv("TRELLO_API_TOKEN")
twilioAccountSid := os.Getenv("TWILIO_ACCOUNT_SID")
twilioAuthToken := os.Getenv("TWILIO_AUTH_TOKEN")
twilioPhoneFrom := os.Getenv("TWILIO_PHONE_FROM")
twilioPhoneTo := os.Getenv("TWILIO_PHONE_TO")
// Initialize context/timeout
ctx, cancel := context.WithTimeout(context.Background(), forageContextTimeout)
log.WithFields(logrus.Fields{"timeout": forageContextTimeout}).Info("Initialized context")
defer cancel()
// Initialize clients
mongoClient, err := clients.NewMongoClientWrapper(ctx, mongoUri)
if err != nil {
log.WithError(err).Fatal("Failed to create MongoDB client wrapper")
} else {
defer mongoClient.Client.Disconnect(ctx)
}
trelloClient := clients.NewTrelloClientWrapper(trelloApiKey, trelloApiToken, trelloMemberID, trelloBoardName, trelloListName, trelloLabels)
twilioClient := clients.NewTwilioClientWrapper(twilioAccountSid, twilioAuthToken, twilioPhoneFrom, twilioPhoneTo)
config := config.Configuration{
ContextTimeout: forageContextTimeout,
Lookahead: forageLookahead,
Time: forageTime,
Timezone: forageTimezone,
LogrusLevel: level,
ListenSocket: listenSocket,
Mongo: mongoClient,
Trello: trelloClient,
Twilio: twilioClient,
}
api.ListenAndServe(ctx, &config)
}