-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (115 loc) · 3.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
proto "guestbook-web/proto"
"log"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"google.golang.org/grpc"
)
var isLoggedIn = middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte("s3cre7"),
})
func main() {
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
//CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.POST("/guest/checkin", checkin)
e.GET("/guest/visit/list", list)
e.PUT("/guest/checkout", checkout)
// Server
e.Logger.Fatal(e.Start(":1323"))
// conn, err := grpc.Dial("127.0.0.1:5009", grpc.WithInsecure())
// if err != nil {
// log.Fatal(err.Error())
// }
// client := proto.NewVisitServiceClient(conn)
// res, err := client.Add(context.Background(), &proto.VisitRequest{BuildingId: "B002", UserId: "test@gmail.com"})
// if err != nil {
// log.Fatal(err.Error())
// }
// log.Printf("Response from server: %s", res.GetToken())
// client.Signup(context.Background(), &proto.SignupRequest{Username: "Carl", Email: "carl@gmail.com", Password: "examplestring"})
}
type user struct {
Token string `json:"token" xml:"name"`
}
type checkinRequest struct {
BuildingID string `json:"building_id" xml:"building_id"`
UserID string `json:"user_id" xml:"userId"`
}
//Checkin
func checkin(c echo.Context) (err error) {
// Get name and email
cr := new(checkinRequest)
if err = c.Bind(cr); err != nil {
return
}
log.Printf("BuildingID: %s", cr.BuildingID)
conn, err := grpc.Dial("127.0.0.1:5009", grpc.WithInsecure())
if err != nil {
log.Fatal(err.Error())
}
client := proto.NewVisitServiceClient(conn)
res, err := client.Add(context.Background(), &proto.VisitRequest{BuildingId: cr.BuildingID, UserId: cr.UserID})
if err != nil {
log.Fatal(err.Error())
}
log.Printf("Response from server: %s", res.GetToken())
// u := controller.GetValue()
u := &user{
Token: res.GetToken(),
}
return c.JSON(http.StatusOK, u)
}
//Checkin
func list(c echo.Context) (err error) {
// Get name and email
token := c.Request().Header.Get("Authorization")
if token == "" {
return
}
conn, err := grpc.Dial("127.0.0.1:5009", grpc.WithInsecure())
if err != nil {
log.Fatal(err.Error())
}
client := proto.NewVisitServiceClient(conn)
res, err := client.List(context.Background(), &proto.VisitListRequest{Token: token})
if err != nil {
log.Fatal(err.Error())
}
// u := controller.GetValue()
// u := &visitList{
// List: res.GetVisit(),
// }
return c.JSON(http.StatusOK, res.GetVisit())
}
//Checkin
func checkout(c echo.Context) (err error) {
// Get name and email
token := c.Request().Header.Get("Authorization")
if token == "" {
return
}
conn, err := grpc.Dial("127.0.0.1:5009", grpc.WithInsecure())
if err != nil {
log.Fatal(err.Error())
}
client := proto.NewVisitServiceClient(conn)
client.Checkout(context.Background(), &proto.CheckoutRequest{Token: token})
// u := controller.GetValue()
// u := &visitList{
// List: res.GetVisit(),
// }
return c.NoContent(http.StatusNoContent)
}