-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.28 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/timshannon/bolthold"
)
type EMailRecord struct {
EMail string
}
var (
db *bolthold.Store
)
func handleExport(w http.ResponseWriter, r *http.Request) {
var rrs []EMailRecord
db.Find(&rrs, nil)
f, err := os.Create("defattd-email.txt")
if err != nil {
log.Printf("export os.Create(): %v", err)
return
}
defer f.Close()
for _, rr := range rrs {
f.WriteString(fmt.Sprintf("%s\n", rr.EMail))
}
}
func handleEMail(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Expected POST", 500)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, fmt.Sprintf("ParseForm() err: %v", err), 500)
return
}
email := r.FormValue("email")
if email == "" {
http.Error(w, "Missing email field", 500)
return
}
db.Insert(bolthold.NextSequence(), EMailRecord{
EMail: r.FormValue("email"),
})
}
func main() {
var bindAddr string
flag.StringVar(&bindAddr, "bind", "localhost:1405", "address to bind to")
flag.Parse()
http.HandleFunc("/email", handleEMail)
http.HandleFunc("/export", handleExport)
var err error
db, err = bolthold.Open("defattd.db", 0600, nil)
if err != nil {
log.Fatalf("bolthold.Open(): %v", err)
}
log.Fatal(http.ListenAndServe(bindAddr, nil))
}