-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
55 lines (46 loc) · 1.36 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
package apiprox
import (
"context"
"log"
"os"
"cloud.google.com/go/errorreporting"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/go-chi/chi/v5"
)
func init() {
// Register an HTTP function with the Functions Framework
// This handler name maps to the entry point name in the Google Cloud Function platform.
// https://cloud.google.com/functions/docs/writing/write-http-functions
functions.HTTP("CloudFunc", newApp().ServeHTTP)
// Disable log prefixes such as the default timestamp.
// Prefix text prevents the message from being parsed as JSON.
// A timestamp is added when shipping logs to Cloud Logging.
log.SetFlags(0)
}
var errorClient *errorreporting.Client
func newApp() *chi.Mux {
// Avoid variable shadow for errorClient
var err error
errorClient, err = initErrorReporting()
if err != nil {
log.Fatalf("initErrorReporting: %v", err)
}
return New().Router
}
func initErrorReporting() (*errorreporting.Client, error) {
if os.Getenv("CI") == "true" {
return &errorreporting.Client{}, nil
}
ctx := context.Background()
projectID := os.Getenv("GCP_PROJECT_ID")
errorClient, err := errorreporting.NewClient(ctx, projectID, errorreporting.Config{
ServiceName: "cors-proxy",
OnError: func(err error) {
log.Printf("Could not log error: %v", err)
},
})
if err != nil {
return nil, err
}
return errorClient, nil
}