-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (94 loc) · 2.67 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
113
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/lucaono13/361_project/controllers"
"github.com/lucaono13/361_project/structure"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
var errorLogger = log.New(os.Stderr, "ERROR ", log.Llongfile)
func signIn(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
usr := new(structure.User)
usr.Email = req.QueryStringParameters["email"]
usr.Password = req.QueryStringParameters["pass"]
err := controllers.SignIn(*usr)
return err, nil
}
func register(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
usr := new(structure.User)
usr.Email = req.QueryStringParameters["email"]
usr.Password = req.QueryStringParameters["pass"]
err := controllers.CreateUser(*usr)
return err, nil
}
func updateBio(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
b := new(structure.BioUpdate)
b.Email = req.QueryStringParameters["email"]
b.Bio = req.Body
if b.Bio == "" {
return events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "Bad request",
}, nil
}
return controllers.UpdateBio(b), nil
}
func getInfo(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
usr := new(structure.User)
usr.Email = req.QueryStringParameters["email"]
user, err := controllers.FindUser(usr.Email)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "Bad request",
}, nil
}
nbu := new(structure.BioUpdate)
nbu.Bio = user.Bio
nbu.Email = user.Email
stringBody, _ := json.Marshal(nbu)
return events.APIGatewayProxyResponse{
StatusCode: 200,
Body: string(stringBody),
}, nil
}
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) {
log.Println("Hello why")
return events.APIGatewayProxyResponse{
StatusCode: status,
Body: http.StatusText(status),
}, nil
}
func main() {
lambda.Start(router)
}
func router(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
qtype := req.QueryStringParameters["type"]
switch req.HTTPMethod {
case "GET":
switch qtype {
case "signin":
return signIn(req)
case "getInfo":
return getInfo(req)
default:
return clientError(http.StatusMethodNotAllowed)
}
case "POST":
return register(req)
case "PUT":
return updateBio(req)
default:
return clientError(http.StatusMethodNotAllowed)
}
}