/
signup.go
57 lines (50 loc) · 1.5 KB
/
signup.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
package main
import (
"fmt"
"github.com/ant0ine/go-json-rest"
"net/http"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
)
type Signup struct {
Id string
Email string
}
func PostSignup(w *rest.ResponseWriter, req *rest.Request) {
signup := Signup{}
err := req.DecodeJsonPayload(&signup)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if signup.Email == "" {
rest.Error(w, "email is required", 400)
return
}
auth, err := aws.EnvAuth()
if err != nil {
panic(err.Error())
}
s := s3.New(auth, aws.EUWest)
bucket := s.Bucket("seq-signup")
data := []byte(signup.Email)
err = bucket.Put(signup.Email, data, "text/plain", s3.BucketOwnerFull)
if err != nil {
panic(err.Error())
}
fmt.Println(signup)
w.Header().Set("Access-Control-Allow-Origin", "http://sequenceiq.com")
w.WriteJson(&signup)
}
func OptionsSignup(w *rest.ResponseWriter, req *rest.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}
func main() {
handler := rest.ResourceHandler{}
handler.SetRoutes(
rest.Route{"POST", "/signup", PostSignup},
rest.Route{"OPTIONS", "/signup", OptionsSignup},
)
http.ListenAndServe(":8288", &handler)
}