-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (67 loc) · 1.64 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/rs/cors"
)
var csvPath string
var addr string
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "%s <csv-file> <addr>\n", os.Args[0])
os.Exit(2)
}
csvPath = os.Args[1]
addr = os.Args[2]
setupCsv(Subscriber{})
fmt.Println("CSV file located at", csvPath)
fmt.Println("listening on", addr)
http.Handle("/subscribe", cors.Default().Handler(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
handlePost(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
},
),
))
http.ListenAndServe(addr, nil)
}
func handlePost(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil || r.Form.Get("email") == "" {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
s := Subscriber{
Email: r.Form.Get("email"),
Host: getIp(r),
When: time.Now().String(),
}
err = s.Save(csvPath)
if err != nil {
fmt.Println(r.URL.Path, "POST", "Couldn't save subscriber: ", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
json, e := json.Marshal(s)
if e != nil {
fmt.Println(r.URL.Path, "POST", "Couldn't marshal JSON: ", e)
http.Error(w, fmt.Sprintf("Internal server error"), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
func getIp(r *http.Request) string {
// this is actually not reliable but ¯\_(ツ)_/¯
if r.Header.Get("X-FORWARDED-FOR") != "" {
return r.Header.Get("X-FORWARDED-FOR")
}
return r.RemoteAddr
}