-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (79 loc) · 2.05 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"github.com/boltdb/bolt"
"github.com/gorilla/mux"
"github.com/securepollingsystem/tallyspider/screed"
)
var Debug = false
func init() {
if os.Getenv("DEBUG") == "1" {
Debug = true
}
}
func main() {
db := mustInitDB()
defer db.Close()
r := mux.NewRouter()
r.HandleFunc("/screed/{pubkey}", GetScreed(db)).Methods("GET")
r.HandleFunc("/screed", PostScreed(db)).Methods("POST")
http.Handle("/", r)
listenAddr := ":8000"
if port := os.Getenv("PORT"); port != "" {
listenAddr = ":" + port
}
log.Printf("Listening on %v\n", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, r))
}
func GetScreed(db *bolt.DB) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
pubkeyhex := mux.Vars(req)["pubkey"]
screedB, err := GetScreedByPubkey(db, pubkeyhex)
if err != nil {
if err == ErrScreedNotFound {
writeErrorStatus(w, err.Error(), http.StatusNotFound, err)
return
}
writeError(w, "Error fetching screed ", err)
return
}
w.Write(screedB)
return
}
}
func PostScreed(db *bolt.DB) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
writeError(w, "Error reading POST data", err)
return
}
defer req.Body.Close()
screed, err := screed.DeserializeScreed(string(body))
if err != nil {
// TODO: Consider giving less info back to the user, for
// security reasons
writeErrorStatus(w, "Invalid POST: "+err.Error(),
http.StatusBadRequest, nil)
return
}
if err = screed.Valid(); err != nil {
writeErrorStatus(w, "Invalid screed: "+err.Error(),
http.StatusBadRequest, err)
}
pubkeyhex, err := CreateScreedByPubkey(db, screed)
if err != nil {
if err == ErrScreedExists {
writeErrorStatus(w, err.Error(), http.StatusConflict, err)
return
}
writeError(w, "Error saving new screed ", err)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte(pubkeyhex))
}
}