-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (95 loc) · 2.78 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
var idRegexp = regexp.MustCompile(`[0-9]`)
var errorLogger = log.New(os.Stderr, "Error:", log.Llongfile)
type user struct {
ID string `json:"id"`
Name string `json:"name"`
Hobbies string `json:"hobbies"`
}
//router switch on the HTTP request method to determin whcih action to takes
func router(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
switch req.HTTPMethod {
case "GET":
return show(req)
case "POST":
return create(req)
default:
return clientError(http.StatusMethodNotAllowed)
}
}
//create adds a new user to the db
func create(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if req.Headers["content-type"] != "application/json" && req.Headers["Content-Type"] != "application/json" {
return clientError(http.StatusNotAcceptable)
}
u := new(user)
err := json.Unmarshal([]byte(req.Body), u)
if err != nil {
return clientError(http.StatusUnprocessableEntity)
}
if !idRegexp.MatchString(u.ID) {
return clientError(http.StatusBadRequest)
}
if u.Name == "" || u.Hobbies == "" {
return clientError(http.StatusBadRequest)
}
err = putItem(u)
if err != nil {
return serverError(err)
}
return events.APIGatewayProxyResponse{
StatusCode: 201,
Headers: map[string]string{"Location": fmt.Sprintf("/users?id=%s", u.ID)},
}, nil
}
//show retrieves from the db the user
func show(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
id := req.QueryStringParameters["id"]
if !idRegexp.MatchString(id) {
return clientError(http.StatusBadRequest)
}
//Fetch the user recored from the db based on the ID value
u, err := getItem(id)
if err != nil {
return serverError(err)
}
//the APIGatewayProxyResponse.Body field needs to be a string, so
//we marshal the users record into a JSON
js, err := json.Marshal(u)
if err != nil {
return serverError(err)
}
fmt.Printf("u: %v", u)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(js),
}, nil
}
//serverError is a helper for handling errors. This logs any error to os.Stderr and returns a 500 Internal Server Error
//response that the AWS API Gateway understands
func serverError(err error) (events.APIGatewayProxyResponse, error) {
errorLogger.Println(err.Error())
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: http.StatusText(http.StatusInternalServerError),
}, nil
}
func clientError(status int) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
StatusCode: status,
Body: http.StatusText(status),
}, nil
}
func main() {
lambda.Start(show)
}