-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.54 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/cloudtasks/apiv2beta3"
"cloud.google.com/go/spanner"
"github.com/sinmetal/gcpmetadata"
)
var ProjectID string
var ServiceAccountEmail string
var TasksClient *cloudtasks.Client
var SpannerClient *spanner.Client
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.")
target := os.Getenv("TARGET")
if target == "" {
target = "World"
}
fmt.Fprintf(w, "Hello %s!\n", target)
}
func main() {
log.Print("Hello world sample started.")
http.HandleFunc("/setup/", HandleSetupAPI)
http.HandleFunc("/fire/", HandleFireAPI)
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
func init() {
projectID, err := gcpmetadata.GetProjectID()
if err != nil {
log.Fatalf("failed ProjectID.err=%+v\n", err)
os.Exit(1)
}
ProjectID = projectID
log.Printf("ProjectID is %s\n", projectID)
sa, err := gcpmetadata.GetServiceAccountEmail()
if err != nil {
log.Fatalf("failed get ServiceAccountEmail.err=%+v\n", err)
os.Exit(1)
}
ServiceAccountEmail = sa
{
client, err := cloudtasks.NewClient(context.Background())
if err != nil {
log.Fatalf("failed cloudtasks.NewClient.err=%+v", err)
}
TasksClient = client
}
{
db := os.Getenv("SPANNER_DATABASE")
SpannerClient, err = CreateSpannerClient(context.Background(), db)
if err != nil {
log.Fatalf("failed CreateSpannerClient. DB=%s, err=%+v", db, err)
}
}
}