/
main.go
397 lines (325 loc) · 8.91 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
//go:generate esc -o static.go -prefix public -ignore \.map$ public
import (
"encoding/csv"
"flag"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/boltdb/bolt"
"github.com/coreos/fleet/log"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"gopkg.in/inconshreveable/log15.v2"
)
var dbFile = "/var/db/ringfree/ipc.db"
var db *bolt.DB
// addr is the listen address
var addr string
// agiaddr is the listen address for the FastAGI service
var agiaddr string
// debug enables debug mode, which uses local files
// instead of bundled ones
var debug bool
// Log is the top-level logger
var Log log15.Logger
// ErrNilTarget indicates that the row/day/date has no target
// specified.
var ErrNilTarget = errors.New("Empty Target")
func init() {
flag.StringVar(&addr, "addr", ":9000", "Address binding")
flag.StringVar(&agiaddr, "agiaddr", ":9001", "Address binding for FastAGI service")
flag.BoolVar(&debug, "debug", false, "Enable debug mode, which uses separate files for web development")
}
func main() {
flag.Parse()
// Create a logger
Log = log15.New()
// Open the database
db, err := dbOpen(dbFile)
if err != nil {
Log.Crit("Failed to open schedule database", "error", err)
return
}
defer db.Close()
// Create Echo web server
e := echo.New()
// Attach middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(func(ctx *echo.Context) error {
ctx.Set("db", db)
return nil
})
// Attach handlers
// Static content
assetHandler := http.FileServer(FS(debug))
// Handle the index
e.Get("/", func(c *echo.Context) error {
assetHandler.ServeHTTP(c.Response().Writer(), c.Request())
return nil
})
e.Get("/app/*", func(c *echo.Context) error {
http.StripPrefix("/app", assetHandler).
ServeHTTP(c.Response().Writer(), c.Request())
return nil
})
//e.Index("public/index.html")
//e.Favicon("public/favicon.ico")
//e.Static("/tags", "public/tags")
//e.Static("/scripts", "public/scripts")
// Data endpoints
e.Get("/target/:id", getTargetHandler)
e.Get("/groups", getGroups)
e.Post("/group", postGroup)
e.Get("/group/:id", getGroupHandler)
e.Delete("/group/:id", deleteGroupHandler)
//e.Put("/group/:id", editGroup)
// Import endpoints
e.Post("/sched/import/days", fileHandler(importDays))
e.Post("/sched/import/dates", fileHandler(importDates))
// Export endpoints
e.Get("/sched/export/:id", getScheduleHandler)
// Listen to OS kill signals
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
Log.Info("Exiting on signal")
os.Exit(100)
}()
// Start FastAGI service
go fastAGI(db)
// Listen for connections
Log.Info("Listening", "address", addr)
e.Run(addr)
}
func fileHandler(fn func(ctx *echo.Context, r io.Reader) error) func(ctx *echo.Context) error {
return func(ctx *echo.Context) error {
// Parse the attached file
req := ctx.Request()
var input io.Reader
if h, ok := req.Header["Content-Type"]; ok {
if h[0] == "text/csv" {
i := req.Body
defer i.Close()
input = i
} else {
i, _, err := req.FormFile("file")
if err != nil {
return err
}
defer i.Close()
input = i
}
}
return fn(ctx, input)
}
}
func importDates(ctx *echo.Context, file io.Reader) error {
return dbFromContext(ctx).Update(func(tx *bolt.Tx) error {
Log.Debug("Got a Dates upload request")
r := csv.NewReader(file)
seenGroups := make(map[string]bool)
var rowCount int
var validCount int
for rec, err := r.Read(); err == nil; rec, err = r.Read() {
rowCount++
date, err := NewDateFromCSV(dbFromContext(ctx), rec)
if err != nil {
if err == ErrNilTarget {
Log.Debug("Ignoring row with empty target")
continue
}
if rowCount > 1 {
Log.Error("Failed to parse Date", "row", rec, "error", err)
return err
}
Log.Debug("Ignoring first row; presuming it is a header")
continue // assume first row is header and skip
}
Log.Debug("Got Date row", "date", date)
// Confirm group exists
g, err := getGroupWithTx(tx, date.Group)
if err != nil {
Log.Error("Failed to load group", "group", date.Group)
return err
}
// if we haven't seen the group this upload, then clear the dates schedule
// of this group
if _, ok := seenGroups[g.ID]; !ok {
g.ClearDates(tx)
seenGroups[g.ID] = true // mark group as seen
}
if err := date.Save(tx); err != nil {
Log.Error("Failed to save the date", "date", date)
return err
}
Log.Debug("Saved date", "date", date)
validCount++
}
Log.Debug("Finished Dates import", "validCount", validCount, "rowCount", rowCount)
return nil
})
}
func importDays(ctx *echo.Context, file io.Reader) error {
return dbFromContext(ctx).Update(func(tx *bolt.Tx) error {
r := csv.NewReader(file)
seenGroups := make(map[string]bool)
var rowCount int
var validCount int
for rec, err := r.Read(); err == nil; rec, err = r.Read() {
Log.Debug("Got Day row", "day", rec)
rowCount++
// Convert the row to a Day
day, err := NewDayFromCSVRow(rec)
if err != nil {
if err == ErrNilTarget {
Log.Debug("Ignoring row with empty target")
continue
}
if rowCount > 1 {
Log.Error("Failed to parse Day", "row", rec, "error", err)
return err
}
Log.Debug("Ignoring first row; presuming it is a header")
continue // assume first row is header and skip
}
// Ignore the row if target is ""
if day.Target == "" {
continue
}
// Confirm group exists
g, err := getGroupWithTx(tx, day.Group)
if err != nil {
Log.Error("Failed to load group", "group", day.Group)
return err
}
// if we haven't seen the group this upload, then clear the days
if _, ok := seenGroups[g.ID]; !ok {
g.ClearDays(tx)
seenGroups[g.ID] = true // mark group as seen
}
// Copy over location to day entity
Log.Debug("Setting location", "location", g.Location)
day.Location = g.Location
// Save the Day
if err := day.Save(tx); err != nil {
Log.Error("Failed to save the day", "day", day)
return err
}
validCount++
Log.Debug("Saved day", "day", day)
}
Log.Debug("Finished Days import", "validCount", validCount, "rowCount", rowCount)
return nil
})
}
// ScheduleDump is a dump of the database of schedules for a group
type ScheduleDump struct {
// Group is the group for which the schedule is presented
Group *Group
// Dates is the list of explicit dates in the schedule
Dates []Date
// Days is the list of relative days in the schedule
Days []Day
}
func getScheduleHandler(ctx *echo.Context) error {
db := dbFromContext(ctx)
s, err := getSchedule(db, ctx.Param("id"))
if err != nil {
return ctx.String(500, err.Error())
}
return ctx.JSON(200, s)
}
func getSchedule(db *bolt.DB, groupID string) (*ScheduleDump, error) {
// Load the group
g, err := getGroup(db, groupID)
if err != nil {
return nil, errors.Wrap(err, "failed to load group")
}
// Load the Date schedule
dates, err := DatesForGroup(db, g)
if err != nil {
Log.Error("failed to load dates", "error", err)
}
// Load the Date schedule
days, err := DaysForGroup(db, g)
if err != nil {
Log.Error("failed to load days", "error", err)
}
return &ScheduleDump{
Group: g,
Dates: dates,
Days: days,
}, nil
}
// getTarget returns the target for the present time
func getTargetHandler(ctx *echo.Context) error {
db := dbFromContext(ctx)
t := getTarget(db, ctx.Param("id"))
if t == "" {
return ctx.String(404, "Not found")
}
return ctx.String(200, t)
}
// getTarget returns the target for the present time
func getTarget(db *bolt.DB, groupID string) string {
// Load the group
g, err := getGroup(db, groupID)
if err != nil {
log.Error("Failed to load group", "error", err)
return ""
}
// See if we have an explicit date entry
d := ActiveDate(db, g, time.Now())
if d != nil {
Log.Debug("Found matching Date", "day", d)
return d.Target
}
// Otherwise, use the day schedule
d2 := ActiveDay(db, g, time.Now())
if d2 != nil {
Log.Debug("Found matching Day", "day", d2)
return d2.Target
}
// Finally, check to see if the group has a default target
if g.DefaultTarget != "" {
return g.DefaultTarget
}
return ""
}
func getGroups(ctx *echo.Context) error {
list, err := allGroups(dbFromContext(ctx))
if err != nil {
return err
}
return ctx.JSON(200, list)
}
func postGroup(ctx *echo.Context) error {
g := Group{
ID: ctx.Form("id"),
Name: ctx.Form("name"),
Location: ctx.Form("timezone"),
DefaultTarget: ctx.Form("defaultTarget"),
}
if g.ID == "" {
g.ID = uuid.NewV1().String()
}
return saveGroup(dbFromContext(ctx), &g)
}
func getGroupHandler(ctx *echo.Context) error {
g, err := getGroup(dbFromContext(ctx), ctx.Param("id"))
if err != nil {
return err
}
return ctx.JSON(200, g)
}
func deleteGroupHandler(ctx *echo.Context) error {
return deleteGroup(dbFromContext(ctx), ctx.Param("id"))
}