-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
131 lines (107 loc) · 2.95 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package signup
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/schema"
)
type registerer interface {
register(ctx context.Context, signup Signup) (Signup, error)
}
type signupServer struct {
service registerer
}
type errResp struct {
Message string `json:"message"`
Field string `json:"field"`
}
type response struct {
URL string `json:"url"`
}
func (ss *signupServer) HandleSignUp(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var su Signup
// Parse JSON or URL Encoded Signup Form
switch r.Header.Get("Content-Type") {
case "application/json":
err := handleJson(&su, r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
fmt.Fprintf(os.Stderr, "handleJson: %v", err)
return
}
case "application/x-www-form-urlencoded":
err := handleForm(&su, r)
if err != nil {
http.Error(w, "Error reading Form Body", http.StatusBadRequest)
fmt.Fprintf(os.Stderr, "handleForm: %v", err)
return
}
default:
http.Error(w, "Unacceptable Content-Type", http.StatusUnsupportedMediaType)
return
}
w.Header().Set("Content-Type", "application/json")
postRegistration, err := ss.service.register(r.Context(), su)
// depending on what we get back, respond accordingly
if err != nil {
// handle invalid phone number error
if strings.Contains(err.Error(), "invalid number") {
// marshall error response
errResp := errResp{
Message: "Invalid Phone Number",
Field: "phone",
}
w.WriteHeader(http.StatusBadRequest)
if err = json.NewEncoder(w).Encode(errResp); err != nil {
fmt.Fprintf(os.Stderr, "problem marshalling error response: %v", err)
http.Error(w, "problem marshalling error response", http.StatusInternalServerError)
}
return
}
fmt.Fprintf(os.Stderr, "\nproblem signing user up: %v\n\n", err)
fmt.Printf("Signup:\n%s\n", prettyPrint(su))
http.Error(w, "problem signing user up\n", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(response{URL: postRegistration.ShortLink}); err != nil {
fmt.Fprintf(os.Stderr, "problem marshalling response: %v", err)
http.Error(w, "problem marshalling response", http.StatusInternalServerError)
}
}
// handleJson unmarshalls a JSON payload from a signUp request into a Signup.
func handleJson(s *Signup, body io.Reader) error {
var timeParseError *time.ParseError
b, err := io.ReadAll(body)
if err != nil {
return err
}
err = json.Unmarshal(b, s)
if errors.As(err, &timeParseError) {
return &InvalidFieldError{Field: "startDateTime"}
}
if err != nil {
return err
}
return nil
}
// handleForm unmarshalls a FormData payload from a signUp request into a Signup
func handleForm(s *Signup, r *http.Request) error {
decoder := schema.NewDecoder()
err := r.ParseForm()
if err != nil {
return err
}
err = decoder.Decode(s, r.PostForm)
if err != nil {
return err
}
return nil
}