-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.go
94 lines (85 loc) · 2.73 KB
/
functions.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
package soteriafunctions
import (
"context"
"net/http"
"soteria-functions/functions"
"soteria-functions/logger"
)
// CreateBtcAccount verify user & re PUT
func CreateBtcAccount(w http.ResponseWriter, r *http.Request) {
defer logger.Log.Flush()
result := corsEnabledFunctionAuth(w, r)
if !result {
logger.Log.Info("[CreateBtcAccount] result is false")
return
}
functions.CreateBtcAccount(w, r)
}
// CreateEthAccount verify user & re PUT
func CreateEthAccount(w http.ResponseWriter, r *http.Request) {
defer logger.Log.Flush()
result := corsEnabledFunctionAuth(w, r)
if !result {
logger.Log.Info("[CreateEthAccount] result is false")
return
}
functions.CreateEthAccount(w, r)
}
// AddBtcBalance verify user & re PUT
func AddBtcBalance(w http.ResponseWriter, r *http.Request) {
defer logger.Log.Flush()
// result := corsEnabledFunctionAuth(w, r)
// if !result {
// logger.Log.Info("[AddBtcBalance] result is false")
// return
// }
functions.AddBtcBalance(w, r)
}
// AddUsdsBalance verify user & re PUT
func AddUsdsBalance(w http.ResponseWriter, r *http.Request) {
defer logger.Log.Flush()
// result := corsEnabledFunctionAuth(w, r)
// if !result {
// logger.Log.Info("[AddBtcBalance] result is false")
// return
// }
functions.AddUsdsBalance(w, r)
}
// AddUsdsBalance verify user & re PUT
func ConvertToken(w http.ResponseWriter, r *http.Request) {
defer logger.Log.Flush()
result := corsEnabledFunctionAuth(w, r)
if !result {
logger.Log.Info("[ConvertToken] result is false")
return
}
functions.ConvertToken(w, r)
}
// PubSubMessage is the payload of a Pub/Sub event.
type pubSubMessage struct {
Data []byte `json:"data"`
}
// UpdatePriceHistory
func UpdatePriceHistory(ctx context.Context, m pubSubMessage) error {
defer logger.Log.Flush()
return functions.UpdateBtcPrice(ctx)
}
// corsEnabledFunctionAuth https://cloud.google.com/functions/docs/writing/http?hl=ja#authentication_and_cors
// preflightの時はheaderをセットするだけで本実行してはいけない
func corsEnabledFunctionAuth(w http.ResponseWriter, r *http.Request) bool {
// Set CORS headers for the preflight request
// Originヘッダーはリクエスト元のホスト名を表している
host := r.Header.Get("Origin")
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "POST, PUT")
w.Header().Set("Access-Control-Allow-Origin", host)
w.Header().Set("Access-Control-Max-Age", "3600")
return false
}
// Set CORS headers for the main request.
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", host)
return true
}