-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (109 loc) · 3.6 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"time"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
"cloud.google.com/go/compute/metadata"
"github.com/sinmetalcraft/gcpbox"
cloudrunbox "github.com/sinmetalcraft/gcpbox/cloudrun"
taskbox "github.com/sinmetalcraft/gcpbox/cloudtasks/appengine"
metadatabox "github.com/sinmetalcraft/gcpbox/metadata"
gaemetadatabox "github.com/sinmetalcraft/gcpbox/metadata/appengine"
gcpboxtestCloudtasks "github.com/sinmetalcraft/gcpboxtest/backend/cloudtasks"
gcpboxtestCloudtasksAppEngine "github.com/sinmetalcraft/gcpboxtest/backend/cloudtasks/appengine"
gcpboxtestCloudtasksRun "github.com/sinmetalcraft/gcpboxtest/backend/cloudtasks/run"
"github.com/sinmetalcraft/gcpboxtest/backend/log"
"github.com/sinmetalcraft/gcpboxtest/backend/storage"
)
func main() {
ctx := context.Background()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
fmt.Printf("Defaulting to port %s", port)
}
projectID, err := metadatabox.ProjectID()
if err != nil {
panic(err)
}
projectNumber, err := metadata.NumericProjectID()
if err != nil {
panic(err)
}
serviceAccountEmail, err := metadatabox.ServiceAccountEmail()
if err != nil {
panic(err)
}
runApiContainer, err := cloudrunbox.NewPrimitiveAPIContainer(ctx, gcpbox.TokyoRegion)
if err != nil {
panic(err)
}
cloudrunboxAdminService, err := cloudrunbox.NewAdminService(ctx, runApiContainer)
if err != nil {
panic(err)
}
gcpboxtestRunService, err := cloudrunboxAdminService.GetRunService(ctx, projectID, "gcpboxtest")
if err != nil {
panic(err)
}
onGAE := true
gaeService, err := gaemetadatabox.Service()
if errors.Is(err, metadatabox.ErrNotFound) {
onGAE = false
} else if err != nil {
panic(err)
}
cloudtasksClient, err := cloudtasks.NewClient(ctx)
if err != nil {
panic(err)
}
taskboxService, err := taskbox.NewService(ctx, cloudtasksClient)
if err != nil {
panic(err)
}
cloudtasksHandlers, err := gcpboxtestCloudtasks.NewHandlers(ctx, &gcpboxtestCloudtasks.HandlersConfig{
ProjectID: projectID,
ProjectNumber: projectNumber,
ServiceAccountEmail: serviceAccountEmail,
TargetGAEServiceID: gaeService,
TaskboxService: taskboxService,
CloudtasksClient: cloudtasksClient,
GcpboxtestCloudRunService: gcpboxtestRunService,
})
if err != nil {
panic(err)
}
if onGAE {
handlers, err := gcpboxtestCloudtasksAppEngine.NewHandlers(ctx, projectID, projectNumber, gaeService, taskboxService)
if err != nil {
panic(err)
}
http.HandleFunc(gcpboxtestCloudtasksAppEngine.AppEngineTasksHandlerUri, handlers.AppEngineTasksHandler)
http.HandleFunc(gcpboxtestCloudtasksAppEngine.HttpTargetTasksHandlerUri, handlers.HttpTargetTasksHandler)
} else {
handlers, err := gcpboxtestCloudtasksRun.NewHandlers(ctx, projectID, projectNumber, cloudtasksClient, gcpboxtestRunService)
if err != nil {
panic(err)
}
http.HandleFunc("/cloudtasks/run/json-post-task", handlers.TasksHandler)
}
fmt.Printf("Listening on port %s", port)
http.HandleFunc("/storage/pubsubnotify", storage.StoragePubSubNotifyHandler)
http.HandleFunc("/cloudtasks/appengine/add-task", cloudtasksHandlers.AddTask)
http.HandleFunc("/", HelloHandler)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), http.DefaultServeMux); err != nil {
fmt.Printf("failed ListenAndServe err=%+v", err)
}
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log.InfoKV(ctx, "request.header", r.Header)
_, err := w.Write([]byte(fmt.Sprintf("Hello GCPBOXTEST : %s", time.Now().String())))
if err != nil {
fmt.Println(err.Error())
}
}