-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_handlers.go
80 lines (74 loc) · 1.86 KB
/
page_handlers.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
package main
import (
"database/sql"
"github.com/go-sql-driver/mysql"
"github.com/patrickmn/go-cache"
"html/template"
"log"
"net/http"
"time"
)
type displayUser struct {
UUID string
PubKey string
Connected bool
}
type displayTransfer struct {
ToUUID string
FromUUID string
FileHash string
FileExpiry time.Time
FileSize string
Downloading bool
Finished bool
Failed bool
}
type liveContent struct {
Uploads []displayTransfer
Users []displayUser
}
func getAllDisplayTransfers(db *sql.DB) []displayTransfer {
var transfers []displayTransfer
if u, found := c.Get("transfers"); found {
transfers = u.([]displayTransfer)
} else {
log.Println("Refreshed transfer cache")
// fetch transfers from db if not in cache
rows, err := db.Query(`
SELECT from_UUID, to_UUID, expiry_dttm, size, file_hash, failed, updated_dttm, finished_dttm
FROM transfer`)
defer rows.Close()
Handle(err)
for rows.Next() {
var (
dt displayTransfer
fileSize int
updated mysql.NullTime
finished mysql.NullTime
)
err = rows.Scan(&dt.FromUUID, &dt.ToUUID, &dt.FileExpiry, &fileSize, &dt.FileHash, &dt.Failed, &updated, &finished)
Handle(err)
dt.Downloading = updated.Valid
dt.Finished = finished.Valid
dt.FileSize = BytesToReadable(fileSize)
transfers = append(transfers, dt)
}
c.Set("transfers", transfers, cache.DefaultExpiration)
}
return transfers
}
// LiveHandler returns a page displaying all historic transfers
func (s *Server) LiveHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
WriteError(w, r, 400, "Invalid method")
return
}
tmplPath := "web/templates/live.html"
tmpl := template.Must(template.ParseFiles(tmplPath))
data := liveContent{
Users: getAllDisplayUsers(s.db),
Uploads: getAllDisplayTransfers(s.db),
}
err := tmpl.Execute(w, data)
Handle(err)
}