-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
52 lines (48 loc) · 1.7 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
package email_func
import (
"cloud.google.com/go/logging"
"context"
"fmt"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/itmayziii/email/send"
"github.com/mailgun/mailgun-go/v4"
"gocloud.dev/blob"
_ "gocloud.dev/blob/fileblob"
_ "gocloud.dev/blob/gcsblob"
"log"
"os"
)
// init exists purely as an entry point for GCP Cloud functions.
// https://cloud.google.com/functions/docs/writing#entry-point
func init() {
loggingClient, err := logging.NewClient(context.Background(), os.Getenv("PROJECT_ID"))
if err != nil {
log.Fatal("failed to create logging client", err)
}
logger := loggingClient.Logger("email", logging.RedirectAsJSON(os.Stdout))
infoLogger := logger.StandardLogger(logging.Info)
errorLogger := logger.StandardLogger(logging.Error)
ctx := context.Background()
bucketName := os.Getenv("BUCKET")
// Do not worry about closing the bucket, this is a no-op for all the implementations that we use i.e. file, GCS, S3.
// It would be more difficult to close the bucket because function invocations sharing the same cloud function would
// not be able to share resources.
bucket, err := blob.OpenBucket(ctx, bucketName)
if err != nil {
logger.LogSync(ctx, logging.Entry{
Severity: logging.Alert,
Payload: fmt.Sprintf("failed to open bucket %s - %v", bucketName, err),
})
os.Exit(1)
}
app := send.NewApp(
send.AppWithFlusher(logger),
send.AppWithInfoLogger(infoLogger),
send.AppWithErrorLogger(errorLogger),
send.AppWithFileStorage(bucket),
send.AppWithDomainSender("tommymay.dev", send.NewMailgunSender(
mailgun.NewMailgun("mg.tommymay.dev", os.Getenv("MG_API_KEY_MG_TOMMYMAY_DEV")),
)),
)
functions.CloudEvent("Email", send.EmailEvent(app))
}