-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (61 loc) · 1.51 KB
/
main.go
File metadata and controls
73 lines (61 loc) · 1.51 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
)
// SlackMsg represents the content of the slack message sent when contact us if filled out
type SlackMsg struct {
Text string `json:"text"`
}
func main() {
port := os.Getenv("SG_ADDR")
if port == "" {
port = ":10001"
}
hookURL := os.Getenv("SG_SLACK_HOOK")
if hookURL == "" {
panic("Set SG_SLACK_HOOK environment variable to a valid slack url")
}
http.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// read post args,
// TODO: Do something with the form value content.
name := r.FormValue("Name")
email := r.FormValue("Email")
phone := r.FormValue("Phone")
message := r.FormValue("Message")
data := bytes.NewBuffer(nil)
enc := json.NewEncoder(data)
msg := SlackMsg{Text: fmt.Sprintf("Yay! A person wants to be contacted\nName: %v\nEmail: %v\nPhone: %v\nMessage: %v", name, email, phone, message)}
err := enc.Encode(msg)
if err != nil {
panic(err)
}
resp, err := http.Post(hookURL, "application/json", data)
if err != nil {
panic(err)
}
if resp.StatusCode != http.StatusOK {
panic("wrong status")
}
}
tmpl, err := template.ParseFiles("contact.html")
if err != nil {
panic(err)
}
err = tmpl.Execute(w, nil)
if err != nil {
panic(err)
}
})
http.Handle("/", http.FileServer(http.Dir(".")))
fmt.Println("Serving on " + port)
err := http.ListenAndServe(port, nil)
if err != nil {
panic(err)
}
}