Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/gci/app.yaml → apps/root-tools/app.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
runtime: go125
service: gci
service: root-tools
app_engine_apis: true

handlers:
Expand Down
2 changes: 1 addition & 1 deletion apps/gci/deploy.sh → apps/root-tools/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
#
# Deploys the Google Cloud Images microservice to Google App Engine.
# Deploys the Root.js tools microservice to Google App Engine.
#
# USAGE:
# ./deploy.sh [options]
Expand Down
6 changes: 5 additions & 1 deletion apps/gci/go.mod → apps/root-tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/blinkk/rootjs/apps/gci
module github.com/blinkk/rootjs/apps/tools

go 1.20

require (
cloud.google.com/go/firestore v1.9.0
cloud.google.com/go/storage v1.30.0
google.golang.org/appengine/v2 v2.0.2
)
Expand All @@ -12,6 +13,7 @@ require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/longrunning v0.3.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand All @@ -21,8 +23,10 @@ require (
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.110.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
7 changes: 7 additions & 0 deletions apps/gci/go.sum → apps/root-tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ cloud.google.com/go/compute v1.18.0 h1:FEigFqoDbys2cvFkZ9Fjq4gnHBP55anJ0yQyau2f9
cloud.google.com/go/compute v1.18.0/go.mod h1:1X7yHxec2Ga+Ss6jPyjxRxpu2uu7PLgsOVXvgU0yacs=
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
cloud.google.com/go/firestore v1.9.0 h1:IBlRyxgGySXu5VuW0RgGFlTtLukSnNkpDiEOMkQkmpA=
cloud.google.com/go/firestore v1.9.0/go.mod h1:HMkjKHNTtRyZNiMzu7YAsLr9K3X2udY2AMwDaMEQiiE=
cloud.google.com/go/iam v0.12.0 h1:DRtTY29b75ciH6Ov1PHb4/iat2CLCvrOm40Q0a6DFpE=
cloud.google.com/go/iam v0.12.0/go.mod h1:knyHGviacl11zrtZUoDuYpDgLjvr28sLQaG0YB2GYAY=
cloud.google.com/go/longrunning v0.3.0 h1:NjljC+FYPV3uh5/OwWT6pVU+doBqMg2x/rZlE+CamDs=
cloud.google.com/go/longrunning v0.3.0/go.mod h1:qth9Y41RRSUE69rDcOn6DdK3HfQfsUI0YSmW3iIlLJc=
cloud.google.com/go/storage v1.30.0 h1:g1yrbxAWOrvg/594228pETWkOi00MLTrOWfh56veU5o=
cloud.google.com/go/storage v1.30.0/go.mod h1:xAVretHSROm1BQX4IIsoVgJqw0LqOyX+I/O2GzRAzdE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down Expand Up @@ -86,6 +89,8 @@ golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -101,6 +106,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
117 changes: 116 additions & 1 deletion apps/gci/main.go → apps/root-tools/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

"cloud.google.com/go/firestore"
"cloud.google.com/go/storage"
"google.golang.org/appengine/v2"
"google.golang.org/appengine/v2/blobstore"
"google.golang.org/appengine/v2/image"
"google.golang.org/appengine/v2/mail"
)

type ServingURLData struct {
Expand All @@ -21,7 +24,8 @@ type ServingURLData struct {
func main() {
http.HandleFunc("/_/serving_url", servingURLHandler)
http.HandleFunc("/_/service_account", serviceAccountHandler)
log.Println("starting gci server")
http.HandleFunc("/_/send_emails", sendEmailsHandler)
log.Println("starting tools server")
appengine.Main()
}

Expand Down Expand Up @@ -136,3 +140,114 @@ func serviceAccountHandler(w http.ResponseWriter, r *http.Request) {
}
w.Write(data)
}

type SendEmailsResult struct {
Success bool `json:"success"`
Sent int `json:"sent"`
Failed int `json:"failed"`
Errors []string `json:"errors,omitempty"`
}

// sendEmailsHandler fetches pending emails from Firestore and sends them using
// the App Engine Mail API. It expects a ?projectId= query param to scope which
// project's email queue to process.
func sendEmailsHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
w.Header().Set("Content-Type", "application/json")

projectId := r.URL.Query().Get("projectId")
if projectId == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{"success": false, "error": "missing required param: projectId"}`)
return
}

fsClient, err := firestore.NewClient(ctx, firestore.DetectProjectID)
if err != nil {
log.Printf("failed to create firestore client: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"success": false, "error": "failed to init firestore"}`)
return
}
defer fsClient.Close()

result, err := processPendingEmails(ctx, fsClient, projectId)
if err != nil {
log.Printf("failed to process pending emails: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"success": false, "error": "failed to process emails"}`)
return
}

data, err := json.Marshal(result)
if err != nil {
log.Printf("failed to serialize json: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"success": false}`)
return
}
w.Write(data)
}

func processPendingEmails(ctx context.Context, fsClient *firestore.Client, projectId string) (*SendEmailsResult, error) {
colPath := fmt.Sprintf("Projects/%s/Emails", projectId)
query := fsClient.Collection(colPath).
Where("status", "==", "pending").
OrderBy("createdAt", firestore.Asc).
Limit(100)

docs, err := query.Documents(ctx).GetAll()
if err != nil {
return nil, fmt.Errorf("failed to query pending emails: %w", err)
}

result := &SendEmailsResult{Success: true}
for _, doc := range docs {
data := doc.Data()
msg := &mail.Message{
Sender: fmt.Sprintf("%v", data["from"]),
Subject: fmt.Sprintf("%v", data["subject"]),
Body: fmt.Sprintf("%v", data["body"]),
}

// Parse recipients.
if toList, ok := data["to"].([]interface{}); ok {
for _, t := range toList {
msg.To = append(msg.To, fmt.Sprintf("%v", t))
}
}

// Include HTML body if present.
if htmlBody, ok := data["htmlBody"].(string); ok && htmlBody != "" {
msg.HTMLBody = htmlBody
}

if err := mail.Send(ctx, msg); err != nil {
log.Printf("failed to send email %s: %v", doc.Ref.ID, err)
result.Failed++
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", doc.Ref.ID, err))
// Mark as failed in Firestore.
_, updateErr := doc.Ref.Update(ctx, []firestore.Update{
{Path: "status", Value: "failed"},
{Path: "error", Value: err.Error()},
})
if updateErr != nil {
log.Printf("failed to update email status to failed: %v", updateErr)
}
continue
}

// Mark as sent in Firestore.
_, updateErr := doc.Ref.Update(ctx, []firestore.Update{
{Path: "status", Value: "sent"},
{Path: "sentAt", Value: firestore.ServerTimestamp},
})
if updateErr != nil {
log.Printf("failed to update email status to sent: %v", updateErr)
}
result.Sent++
}

log.Printf("processed %d emails for project %s: %d sent, %d failed", len(docs), projectId, result.Sent, result.Failed)
return result, nil
}
Binary file added apps/root-tools/tools
Binary file not shown.
69 changes: 69 additions & 0 deletions packages/root-cms/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {type CMSCheck} from './checks.js';
import {RootCMSClient, parseDocId, unmarshalData} from './client.js';
import {runCronJobs} from './cron.js';
import {arrayToCsv, csvToArray} from './csv.js';
import {type CMSEmailConfig} from './plugin.js';
import {type CMSTranslationService} from './translations.js';

type AppModule = typeof import('./app.js');
Expand Down Expand Up @@ -108,6 +109,8 @@ export interface ApiOptions {
* may change from version to version as we add new features.
*/
translations?: CMSTranslationService[];
/** Email notification config registered via the CMS plugin config. */
email?: CMSEmailConfig;
}

/**
Expand Down Expand Up @@ -811,4 +814,70 @@ export function api(server: Server, options: ApiOptions) {
res.status(500).json({success: false, error: err.message || 'UNKNOWN'});
}
}

/**
* Sends pending emails using the registered email service. This endpoint
* processes queued emails from Firestore and sends them via the configured
* email service provider.
*
* ```
* POST /cms/api/email.send
* ```
*/
server.use('/cms/api/email.send', async (req: Request, res: Response) => {
if (req.method !== 'POST') {
res.status(400).json({success: false, error: 'BAD_REQUEST'});
return;
}
if (!req.user?.email) {
res.status(401).json({success: false, error: 'UNAUTHORIZED'});
return;
}

const emailConfig = options.email;
const emailService = emailConfig?.service;
if (!emailService) {
res.status(400).json({
success: false,
error: 'EMAIL_SERVICE_NOT_CONFIGURED',
});
return;
}

try {
const cmsClient = new RootCMSClient(req.rootConfig!);
const expiresAfterMs = emailConfig?.expiresAfterMs;
const pendingEmails = await cmsClient.listPendingEmails({
expiresAfterMs,
});
let sent = 0;
let failed = 0;

for (const email of pendingEmails) {
const result = await emailService.sendEmail({
to: email.to,
from: email.from,
subject: email.subject,
body: email.body,
htmlBody: email.htmlBody,
});
if (result.success) {
await cmsClient.updateEmailStatus(email.id, 'sent');
sent++;
} else {
await cmsClient.updateEmailStatus(
email.id,
'failed',
result.error || 'unknown error'
);
failed++;
}
}

res.status(200).json({success: true, sent, failed});
} catch (err: any) {
console.error(err.stack || err);
res.status(500).json({success: false, error: err.message || 'UNKNOWN'});
}
});
}
Loading