Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
docs: express redis
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Oct 5, 2024
1 parent ea345c7 commit c24898d
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 141 deletions.
2 changes: 1 addition & 1 deletion examples/aws-bun-elysia/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* curl http://localhost:3000/latest
* ```
*
* Finally, you can deploy it using `bun sst deploy`.
* Finally, you can deploy it using `bun sst deploy --stage production`.
*/
export default $config({
app(input) {
Expand Down
1 change: 1 addition & 0 deletions examples/aws-express-file-upload/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions examples/aws-express-file-upload/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# sst
.sst
10 changes: 10 additions & 0 deletions examples/aws-express-file-upload/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:18-bullseye-slim

WORKDIR /app/

COPY package.json /app
RUN npm install

COPY index.mjs /app

ENTRYPOINT ["node", "index.mjs"]
62 changes: 62 additions & 0 deletions examples/aws-express-file-upload/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import multer from "multer";
import express from "express";
import { Resource } from "sst";
import { Upload } from "@aws-sdk/lib-storage";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
S3Client,
GetObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";

const PORT = 80;

const app = express();
const s3 = new S3Client({});
const upload = multer({ storage: multer.memoryStorage() });

app.get("/", async (req, res) => {
res.send("Hello World!");
});

app.post("/", upload.single("file"), async (req, res) => {
const file = req.file;
const params = {
Bucket: Resource.MyBucket.name,
Key: file.originalname,
Body: file.buffer,
};

const upload = new Upload({
params,
client: s3,
});

await upload.done();

res.status(200).send("File uploaded successfully.");
});

app.get("/latest", async (req, res) => {
const objects = await s3.send(
new ListObjectsV2Command({
Bucket: Resource.MyBucket.name,
}),
);

const latestFile = objects.Contents.sort(
(a, b) => b.LastModified - a.LastModified,
)[0];

const command = new GetObjectCommand({
Key: latestFile.Key,
Bucket: Resource.MyBucket.name,
});
const url = await getSignedUrl(s3, command);

res.redirect(url);
});

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
20 changes: 20 additions & 0 deletions examples/aws-express-file-upload/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "aws-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-s3": "^3.556.0",
"@aws-sdk/lib-storage": "^3.556.0",
"@aws-sdk/s3-request-presigner": "^3.556.0",
"express": "^4.19.2",
"multer": "^1.4.5-lts.1",
"sst": "latest"
},
"devDependencies": {
"@types/aws-lambda": "8.10.137"
}
}
21 changes: 21 additions & 0 deletions examples/aws-express-file-upload/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"MyService": {
"service": string
"type": "sst.aws.Service"
"url": string
}
"MyVpc": {
"type": "sst.aws.Vpc"
}
}
}
68 changes: 68 additions & 0 deletions examples/aws-express-file-upload/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// <reference path="./.sst/platform/config.d.ts" />

/**
* ## AWS Express file upload
*
* Deploys an Express app to AWS.
*
* You can get started by running.
*
* ```bash
* mkdir aws-express && cd aws-express
* npm init -y
* npm install express
* npx sst@latest init
* ```
*
* Now you can add a service.
*
* ```ts title="sst.config.ts"
* cluster.addService("MyService", {
* public: {
* ports: [{ listen: "80/http", forward: "3000/http" }],
* },
* dev: {
* command: "node --watch index.mjs",
* },
* });
* ```
*
* Start your app locally.
*
* ```bash
* npx sst dev
* ```
*
* This example lets you upload a file to S3 and then download it.
*
* ```bash
* curl --F [email protected] http://localhost:3000/
* curl http://localhost:3000/latest
* ```
*
* Finally, you can deploy it using `npx sst deploy --stage production`.
*/
export default $config({
app(input) {
return {
name: "aws-express",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket");
const vpc = new sst.aws.Vpc("MyVpc");

const cluster = new sst.aws.Cluster("MyCluster", { vpc });
cluster.addService("MyService", {
public: {
ports: [{ listen: "80/http" }],
},
dev: {
command: "node --watch index.mjs",
},
link: [bucket],
});
},
});
1 change: 1 addition & 0 deletions examples/aws-express-file-upload/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions examples/aws-express/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules


# sst
.sst
64 changes: 15 additions & 49 deletions examples/aws-express/index.mjs
Original file line number Diff line number Diff line change
@@ -1,60 +1,26 @@
import multer from "multer";
import express from "express";
import { Resource } from "sst";
import { Upload } from "@aws-sdk/lib-storage";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
S3Client,
GetObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";
import { Cluster } from "ioredis";

const PORT = 80;

const app = express();
const s3 = new S3Client({});
const upload = multer({ storage: multer.memoryStorage() });

app.get("/", async (req, res) => {
res.send("Hello World!");
});

app.post("/", upload.single("file"), async (req, res) => {
const file = req.file;
const params = {
Bucket: Resource.MyBucket.name,
Key: file.originalname,
Body: file.buffer,
};
const redis = new Cluster(
[{ host: Resource.MyRedis.host, port: Resource.MyRedis.port }],
{
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: true,
username: Resource.MyRedis.username,
password: Resource.MyRedis.password,
},
}
);

const upload = new Upload({
params,
client: s3,
});

await upload.done();

res.status(200).send("File uploaded successfully.");
});

app.get("/latest", async (req, res) => {
const objects = await s3.send(
new ListObjectsV2Command({
Bucket: Resource.MyBucket.name,
}),
);

const latestFile = objects.Contents.sort(
(a, b) => b.LastModified - a.LastModified,
)[0];

const command = new GetObjectCommand({
Key: latestFile.Key,
Bucket: Resource.MyBucket.name,
});
const url = await getSignedUrl(s3, command);

res.redirect(url);
app.get("/", async (req, res) => {
const counter = await redis.incr("counter");
res.send(`Hit counter: ${counter}`);
});

app.listen(PORT, () => {
Expand Down
14 changes: 7 additions & 7 deletions examples/aws-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-s3": "^3.556.0",
"@aws-sdk/lib-storage": "^3.556.0",
"@aws-sdk/s3-request-presigner": "^3.556.0",
"express": "^4.19.2",
"multer": "^1.4.5-lts.1",
"sst": "latest"
"express": "^4.21.0",
"ioredis": "^5.4.1",
"sst": "3.1.65"
},
"devDependencies": {
"@types/aws-lambda": "8.10.137"
"@types/aws-lambda": "8.10.145"
}
}
10 changes: 7 additions & 3 deletions examples/aws-express/sst-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
"MyRedis": {
"host": string
"password": string
"port": number
"type": "sst.aws.Redis"
"username": string
}
"MyService": {
"service": string
"type": "sst.aws.Service"
"url": string
}
"MyVpc": {
"bastion": string
"type": "sst.aws.Vpc"
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/aws-express/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ export default $config({
};
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket");
const vpc = new sst.aws.Vpc("MyVpc");

const vpc = new sst.aws.Vpc("MyVpc", { bastion: true });
const redis = new sst.aws.Redis("MyRedis", { vpc });
const cluster = new sst.aws.Cluster("MyCluster", { vpc });

cluster.addService("MyService", {
link: [redis],
public: {
ports: [{ listen: "80/http" }],
},
dev: {
command: "node --watch index.mjs",
},
link: [bucket],
});
},
}
});
Loading

0 comments on commit c24898d

Please sign in to comment.