-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
executable file
·36 lines (30 loc) · 894 Bytes
/
storage.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
package storage
import (
"fmt"
"log"
"net/smtp"
)
func bytesInUse(username string) int64 { return 0 /* ... */ }
// Email sender configuration
// NOTE: never put passwords in source code!
const sender = "notifications@example.com"
const password = "example"
const hostname = "smtp.example.com"
const template = `warning: you are using %d bytes of storage, %d%% of your quota.`
func CheckQuota(username string) {
used := bytesInUse(username)
const quota = 1000000000 // 1GB
percent := 100 * used / quota
if percent < 90 {
return //OK
}
msg := fmt.Sprintf(template, used, percent)
notifyUser(username, msg)
}
var notifyUser = func(username, msg string) {
auth := smtp.PlainAuth("", sender, password, hostname)
err := smtp.SendMail(hostname+":587", auth, sender, []string{username}, []byte(msg))
if err != nil {
log.Printf("stmp.SendMail(%s) failed: %s", username, err)
}
}