-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (160 loc) · 3.83 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"time"
"github.com/die-net/http-tarpit/tarpit"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/kevinburke/twilio-go"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
log "github.com/sirupsen/logrus"
)
var (
adminPassword,
adminUsername,
databaseURL,
twilioAccountSID,
twilioAuthToken,
twilioFromNumber,
twilioToNumber string
)
// TODO: Refactor many of these global vars into config if possible
var watcher = Watcher{
ctx: context.Background(),
}
var db *gorm.DB
var tp *tarpit.Tarpit
type Router interface {
Run(...string) error
ServeHTTP(http.ResponseWriter, *http.Request)
}
type Config interface {
buildRouter() Router
}
type config struct{}
var appConfig Config
type Client interface {
Messages() Sender
Calls() Caller
}
type CommsClient struct {
messages Sender
calls Caller
}
func (c *CommsClient) Messages() Sender {
return c.messages
}
func (c *CommsClient) Calls() Caller {
return c.calls
}
type Sender interface {
SendMessage(string, string, string, []*url.URL) (*twilio.Message, error)
}
type Caller interface {
Create(context.Context, url.Values) (*twilio.Call, error)
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
}
func main() {
appConfig = &config{}
if err := Run(); err != nil {
log.Fatal(err)
}
}
func Run() error {
log.Info("Starting up...")
log.Info("Parsing environment...")
env := os.Getenv("MEMINDERS_ENV")
if "" == env {
env = "development"
}
if err := godotenv.Load(".env." + env); err != nil {
log.Fatal("Error loading environment file for env: ", env)
}
adminPassword = getenv("ADMIN_PASSWORD")
adminUsername = getenv("ADMIN_USERNAME")
databaseURL = getenv("DATABASE_URL")
twilioAccountSID = getenv("TWILIO_ACCOUNT_SID")
twilioAuthToken = getenv("TWILIO_AUTH_TOKEN")
twilioFromNumber = getenv("TWILIO_FROM_NUMBER")
twilioToNumber = getenv("TWILIO_TO_NUMBER")
if env != "development" {
gin.SetMode(gin.ReleaseMode)
}
log.Info("Environment parsed...")
var err error
db, err = setUpDB()
if err != nil {
return err
}
client := setUpCommunicationsClient()
reminders, err := getReminders(db)
if err != nil {
return err
}
watcher.reminders = reminders
go watcher.WatchReminders(client)
router := appConfig.buildRouter()
log.Info("Routes built and serving on port :8080")
err = router.Run()
if err != nil {
return err
}
return nil
}
func (c *config) buildRouter() Router {
log.Info("Building routes...")
router := gin.Default()
authorized := router.Group("/", gin.BasicAuth(gin.Accounts{
adminUsername: adminPassword,
}))
authorized.POST("/reminders", postReminders)
router.Any("/robots.txt", robotsHandler)
// Tarpit
tp = tarpit.New(
runtime.NumCPU(),
"text/html",
16*time.Second,
50*time.Millisecond,
1048576,
10485760,
)
if tp == nil {
log.Fatal("Unable to build tarpit")
}
router.NoRoute(tarpitHandler)
return router
}
func robotsHandler(ctx *gin.Context) {
ctx.String(http.StatusOK, "User-agent: * Disallow /")
}
func tarpitHandler(ctx *gin.Context) {
tp.Handler(ctx.Writer, ctx.Request)
}
func setUpDB() (*gorm.DB, error) {
log.Info("Connecting to database " + databaseURL + "...")
db, err := gorm.Open(sqlite.Open(databaseURL), &gorm.Config{})
if err != nil {
return db, fmt.Errorf("Failed to connect to database: %v\n", err)
}
log.Info("Migrating database...")
if err := db.AutoMigrate(&Reminder{}); err != nil {
return db, fmt.Errorf("Failed to migrate reminders table: %v\n", err)
}
log.Info("Migrated database")
return db, nil
}
func setUpCommunicationsClient() Client {
log.Info("Initializing Communications client...")
client := twilio.NewClient(twilioAccountSID, twilioAuthToken, nil)
log.Info("Communcations client initialized")
return &CommsClient{calls: client.Calls, messages: client.Messages}
}