-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_for_frontend_with_user_secret.go
93 lines (88 loc) · 2.29 KB
/
function_for_frontend_with_user_secret.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
package p
import (
"errors"
"fmt"
"log"
"net/http"
"github.com/asendia/legacy-api/api"
"github.com/asendia/legacy-api/data"
"github.com/google/uuid"
"github.com/joho/godotenv"
)
// Google Cloud Function
func CloudFunctionForFrontendWithUserSecret(w http.ResponseWriter, r *http.Request) {
corsStatus, err := api.VerifyCORS(w, r)
if err != nil || corsStatus != http.StatusAccepted {
return
}
godotenv.Load()
secret, messageID, err := VerifyQueryString(r)
if err != nil {
log.Println(err.Error())
http.Error(w, "Invalid query string", http.StatusForbidden)
return
}
// Establishing connection to database
ctx := r.Context()
conn, err := data.ConnectDB(ctx, data.LoadDBURLConfig())
if err != nil {
log.Printf("Cannot connect to the database: %v\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer conn.Close()
tx, err := conn.Begin(ctx)
if err != nil {
log.Printf("Cannot begin database transaction: %v\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer tx.Rollback(ctx)
a := api.APIForFrontend{
Context: ctx,
Tx: tx,
}
var res api.APIResponse
action := r.URL.Query().Get("action")
switch action {
case "extend-message":
res, err = a.ExtendMessageInactiveAt(secret, messageID)
break
case "unsubscribe-message":
res, err = a.UnsubscribeMessage(secret, messageID)
break
default:
err = errors.New("Invalid Action")
res.StatusCode = http.StatusNotFound
res.ResponseMsg = err.Error()
break
}
w.Header().Set("Content-Type", "application/json")
// Handle controller error
if err != nil {
log.Println(err.Error())
http.Error(w, `{"err":"`+err.Error()+`"}`, res.GetValidStatusCode())
return
}
// Generate response
resStr, err := res.ToString()
if err != nil {
log.Println(err.Error())
http.Error(w, "Cannot generate a response", http.StatusInternalServerError)
return
}
tx.Commit(ctx)
fmt.Fprint(w, resStr)
}
func VerifyQueryString(r *http.Request) (secret string, id uuid.UUID, err error) {
q := r.URL.Query()
id, err = uuid.Parse(q.Get("id"))
if err != nil {
return secret, id, err
}
secret = q.Get("secret")
if len(secret) != api.ExtensionSecretLength {
return secret, id, errors.New("Invalid secret")
}
return secret, id, err
}