-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.go
58 lines (48 loc) · 1.69 KB
/
function.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
package function
import (
"context"
"fmt"
"net/http"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2beta3"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2beta3"
)
// createHTTPTask creates a new task in your with a HTTP target.
func createHTTPTask(projectID, locationID, queueID, url, message string) (*taskspb.Task, error) {
// Create a new Cloud Tasks client instance.
// See https://godoc.org/cloud.google.com/go/cloudtasks/apiv2beta3
ctx := context.Background()
client, err := cloudtasks.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("NewClient: %v", err)
}
// Build the Task queue path.
queuePath := fmt.Sprintf("projects/%s/locations/%s/queues/%s", projectID, locationID, queueID)
// Build the Task payload.
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2beta3#CreateTaskRequest
req := &taskspb.CreateTaskRequest{
Parent: queuePath,
Task: &taskspb.Task{
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2beta3#HttpRequest
PayloadType: &taskspb.Task_HttpRequest{
HttpRequest: &taskspb.HttpRequest{
HttpMethod: taskspb.HttpMethod_POST,
Url: url,
},
},
},
}
// Add a payload message if one is present.
req.Task.GetHttpRequest().Body = []byte(message)
createdTask, err := client.CreateTask(ctx, req)
if err != nil {
return nil, fmt.Errorf("cloudtasks.CreateTask: %v", err)
}
return createdTask, nil
}
func Execute(w http.ResponseWriter, r *http.Request) {
_, err := createHTTPTask(os.Getenv("GOOGLE_CLOUD_PROJECT_ID"), os.Getenv("LOCATION_ID"), os.Getenv("QUEUE_ID"), os.Getenv("TARGET_URL"), "")
if err != nil {
fmt.Println(err)
}
fmt.Fprint(w, "request is received.")
}