Skip to content

Commit

Permalink
Add elistman create-subscribers-table
Browse files Browse the repository at this point in the history
This replaces bin/create-subscribers-table.sh. It uses db.DynamoDb to
create the table, which sets the schema, the indexes, and the Time To
Live for pending subscribers.

Updated the README to describe how to build the `elistman` CLI and run
it to create the subscribers table. Also updated the description of the
`deploy.env` file.
  • Loading branch information
mbland committed May 18, 2023
1 parent f11ecc9 commit 1fd71a8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,33 @@ $ aws apigatewayv2 get-domain-names
}
```

### Build the `elistman` CLI

Build the `elistman` command line interface program in the root directory via:

```sh
go build
```

Run the command and check the output to see if it was successful:

```sh
$ ./elistman

Mailing list system providing address validation and unsubscribe URIs

Usage:
elistman [command]

Available Commands:
...
```

### Create the DynamoDB table

Run the `bin/create-subscribers-table.sh` script to create the DynamoDB table,
using a table name of your choice. Then run `aws dynamodb list-tables` to
confirm that the new table is present.
Run `elistman create-subscribers-table <TABLE_NAME>` to create the DynamoDB
table, replacing `<TABLE_NAME>` with a table name of your choice. Then run `aws
dynamodb list-tables` to confirm that the new table is present.

## Deployment

Expand All @@ -189,10 +211,21 @@ Create a `deploy.env` file in the root directory of the following form

```sh
API_DOMAIN_NAME="api.mike-bland.com"
API_ROUTE_KEY="email"
API_MAPPING_KEY="email"
EMAIL_DOMAIN_NAME="mike-bland.com"
EMAIL_SITE_TITLE="Mike Bland"
SENDER_NAME="Mike Bland's blog"
SUBSCRIBERS_TABLE_NAME="TABLE_NAME"
SENDER_USER_NAME="posts"
UNSUBSCRIBE_USER_NAME="unsubscribe"
RECEIPT_RULE_SET_NAME="mike-bland.com"
SUBSCRIBERS_TABLE_NAME="<TABLE_NAME>"

INVALID_REQUEST_PATH="/subscribe/malformed.html"
ALREADY_SUBSCRIBED_PATH="/subscribe/already-subscribed.html"
VERIFY_LINK_SENT_PATH="/subscribe/confirm.html"
SUBSCRIBED_PATH="/subscribe/hello.html"
NOT_SUBSCRIBED_PATH="/unsubscribe/not-subscribed.html"
UNSUBSCRIBED_PATH="/unsubscribe/goodbye.html"
```

Then run `make deploy`.
Expand Down
14 changes: 0 additions & 14 deletions bin/create-subscribers-table.sh

This file was deleted.

60 changes: 60 additions & 0 deletions cmd/create-subscribers-table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2023 Mike Bland <mbland@acm.org>
// See LICENSE.txt for details.

package cmd

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/mbland/elistman/db"
"github.com/spf13/cobra"
)

var createSubscribersTableCmd = &cobra.Command{
Use: "create-subscribers-table",
Short: "Create a DynamoDB table for mailing list subscribers",
Long: `Creates a new DynamoDB table for mailing list subscriber information.
The new table will have an index for pending subscribers and an index for
verified subscribers. Pending subscribers will automatically expire after 24
hours, after which the DynamoDB Time To Live feature will remove them.
The command takes one argument, which is the name of the table to create. This
name will become the value of the SUBSCRIBERS_TABLE_NAME environment variable
used to configure and deploy the application.`,
Args: cobra.ExactArgs(1),
RunE: CreateSubscribersTable,
}

func init() {
rootCmd.AddCommand(createSubscribersTableCmd)
}

func CreateSubscribersTable(cmd *cobra.Command, args []string) (err error) {
tableName := args[0]
ctx := context.Background()
var cfg aws.Config

if cfg, err = config.LoadDefaultConfig(ctx); err != nil {
return
}

dbase := &db.DynamoDb{
Client: dynamodb.NewFromConfig(cfg), TableName: tableName,
}

if err = dbase.CreateTable(ctx); err != nil {
return
} else if err = dbase.WaitForTable(ctx, time.Minute); err != nil {
return
} else if _, err = dbase.UpdateTimeToLive(ctx); err != nil {
return
}
fmt.Printf("Successfully created DynamoDB table: %s\n", tableName)
return
}

0 comments on commit 1fd71a8

Please sign in to comment.