-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
173 lines (153 loc) · 4.85 KB
/
handler.go
File metadata and controls
173 lines (153 loc) · 4.85 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"net/http"
"net/url"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/oauth2"
)
func HealthCheck(c echo.Context) error {
return c.String(http.StatusOK, "system operational")
}
type AuthUrlResponseBody struct {
Url string `json:"url"`
}
func PublishGoogleAuthUrl(c echo.Context) error {
authURL := AuthConfig.AuthCodeURL("state-token", oauth2.AccessTypeOffline, oauth2.ApprovalForce)
c.Logger().Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
return c.JSON(200, AuthUrlResponseBody{Url: authURL})
}
type CodeExchangeRequestBody struct {
Code string `json:"code"`
UserExId string `json:"userExId"`
}
func CodeExchange(c echo.Context) error {
var err error
body := new(CodeExchangeRequestBody)
if err = c.Bind(body); err != nil {
return c.String(http.StatusBadRequest, "bad request")
}
decodedCode, err := url.QueryUnescape(body.Code)
if err != nil {
return c.String(http.StatusBadRequest, "Code is invalid")
}
tok, err := AuthConfig.Exchange(context.TODO(), decodedCode)
if err != nil {
return c.String(http.StatusBadRequest, "Code is invalid")
}
err = DbClient.PutToken(body.UserExId, tok)
if err != nil {
c.Logger().Errorf("Got dynamoDb error: %v", err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
return c.String(http.StatusOK, "Reserved user tokens")
}
type InsertOneTaskRequestBody struct {
UserExId string `json:"userExId"`
Task OismTask `json:"task"`
}
type OismTask struct {
ListName string `json:"listName"`
Title string `json:"title"`
Notes string `json:"notes,omitempty"`
Duo string `json:"duo,omitempty"`
}
type InsertOneTaskResponseBody struct {
Ticket string `json:"ticket"`
}
func InsertOneTask(c echo.Context) error {
requestBody := InsertOneTaskRequestBody{}
if err := c.Bind(&requestBody); err != nil {
return c.String(http.StatusBadRequest, "bad request")
}
token, err := DbClient.FetchToken(requestBody.UserExId)
if err != nil {
c.Logger().Errorf("Got dynamoDb error: %v", err)
if IsNotSuchKeyError(err) {
return c.String(http.StatusNotFound, "No Such UserExId")
}
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
ticket := publishInsertTaskAction(token, requestBody.Task)
return c.JSON(http.StatusAccepted, InsertOneTaskResponseBody{Ticket: ticket.String()})
}
type InsertManyTasksRequestBody struct {
UserExId string `json:"userExId"`
Tasks []OismTask `json:"tasks"`
}
type InsertManyResponseBody struct {
Tickets []string `json:"tickets"`
}
func InsertManyTasks(c echo.Context) error {
requestBody := InsertManyTasksRequestBody{}
if err := c.Bind(&requestBody); err != nil {
return c.String(http.StatusBadRequest, "bad request")
}
token, err := DbClient.FetchToken(requestBody.UserExId)
if err != nil {
c.Logger().Errorf("Got dynamoDb error: %v", err)
if IsNotSuchKeyError(err) {
return c.String(http.StatusNotFound, "No Such UserExId")
}
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
tickets := []string{}
for _, task := range requestBody.Tasks {
ticket := publishInsertTaskAction(token, task)
tickets = append(tickets, ticket.String())
}
return c.JSON(http.StatusAccepted, InsertManyResponseBody{Tickets: tickets})
}
type GoogleActionStatus string
const (
Started GoogleActionStatus = "started"
InProgress GoogleActionStatus = "onGoing"
Error GoogleActionStatus = "error"
Done GoogleActionStatus = "done"
)
func publishInsertTaskAction(token *oauth2.Token, task OismTask) uuid.UUID {
ticket := uuid.New()
GoogleActionStore.Store(ticket, Started)
go func() {
GoogleActionStore.Store(ticket, InProgress)
svs, err := NewTasksService(token)
if err != nil {
GoogleActionStore.Store(ticket, Error)
NotiClient.NotifyInsertTaskError(task, err)
return
}
err = svs.InsertTask(task.ListName, task.Title, task.Notes, task.Duo)
if err != nil {
GoogleActionStore.Store(ticket, Error)
NotiClient.NotifyInsertTaskError(task, err)
return
}
GoogleActionStore.Store(ticket, Done)
}()
return ticket
}
type Ticket struct {
Ticket string `param:"name"`
}
type CheckGoogleActionStatusResponseBody struct {
Status GoogleActionStatus `json:"status"`
}
func CheckGoogleActionStatus(c echo.Context) error {
var ticket_s string
err := echo.PathParamsBinder(c).String("ticket", &ticket_s).BindError()
if err != nil {
return c.String(http.StatusBadRequest, "No \"ticket\" Path Parameter")
}
ticket, err := uuid.Parse(ticket_s)
if err != nil {
return c.String(http.StatusBadRequest, "Ticket Should Be UUID")
}
status_i, found := GoogleActionStore.Load(ticket)
if !found {
return c.String(http.StatusNotFound, "No Found such ticket")
}
status := status_i.(GoogleActionStatus)
return c.JSON(http.StatusOK, CheckGoogleActionStatusResponseBody{Status: status})
}