Skip to content

Commit

Permalink
Merge pull request #250 from karthik2804/add_examples
Browse files Browse the repository at this point in the history
  • Loading branch information
radu-matei authored Jul 4, 2024
2 parents 536aa10 + fe11981 commit f178468
Show file tree
Hide file tree
Showing 73 changed files with 23,356 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/typescript/aws/s3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
target
.spin/
dist.js
53 changes: 53 additions & 0 deletions examples/typescript/aws/s3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# AWS S3 Integration

This example showcases how to connect to and send messages using Amazon S3 with the AWS SDK.

## Prerequisites
- `spin >=2.6.0`
-

## Install Dependencies
Install the necessary npm packages:

```bash
npm install
```

## Setup the Example

1. **Create an AWS Account**
- If you don't have an AWS account, create one at [AWS](https://aws.amazon.com/).

2. **Create an S3 bucket**
- Go to the [Amazon S3 Console](https://console.aws.amazon.com/s3/).
- Create a new bucket and add files to it. Note down the bucket name.

3. **Get AWS Credentials**
- Create or obtain your AWS credentials (Access Key ID, Secret Access Key, and a Session Token).

4. **Configure the Code**
- Copy the region, access key ID, secret access key, session token, and bucket name into the code at `src/index.ts`.

```typescript
const client = new SQSClient({
region: "<region>",
credentials: {
accessKeyId: "<accessKeyId>",
secretAccessKey: "<secretAccessKey>",
sessionToken: "<sessionToken>"
},
});

const params = {
Bucket: "<bucketName>"
};
```

## Building and Running the Example

```bash
spin build
spin up
```

Use e.g. curl -v http://127.0.0.1:3000/ to test the endpoint.
3,697 changes: 3,697 additions & 0 deletions examples/typescript/aws/s3/package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions examples/typescript/aws/s3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "s3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/s3.wasm",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"mkdirp": "^3.0.1",
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.600.0",
"@fermyon/spin-sdk": "^2.0.0-alpha.2"
}
}
19 changes: 19 additions & 0 deletions examples/typescript/aws/s3/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
spin_manifest_version = 2

[application]
authors = ["karthik2804 <[email protected]>"]
description = ""
name = "s3"
version = "0.1.0"

[[trigger.http]]
route = "/..."
component = "s3"

[component.s3]
source = "target/s3.wasm"
exclude_files = ["**/node_modules"]
allowed_outbound_hosts = ["https://*:*"]
[component.s3.build]
command = "npm run build"
watch = ["src/**/*.ts", "package.json"]
28 changes: 28 additions & 0 deletions examples/typescript/aws/s3/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ResponseBuilder } from "@fermyon/spin-sdk";
const { S3Client, ListObjectsV2Command } = require("@aws-sdk/client-s3");

const client = new S3Client({
region: "<>",
credentials: {
accessKeyId: "<>>",
secretAccessKey: "<>",
sessionToken: "<>"
},
});

const params = {
Bucket: "<>"
};

const command = new ListObjectsV2Command(params);

export async function handler(req: Request, res: ResponseBuilder) {
let data
try {
data = await client.send(command);
res.send(JSON.stringify(data.Contents, null, 2));
} catch (e: any) {
res.status(500)
res.send(`error : ${e.message}`)
}
}
28 changes: 28 additions & 0 deletions examples/typescript/aws/s3/src/spin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ResponseBuilder } from "@fermyon/spin-sdk";
import { handler } from ".";

//@ts-ignore
addEventListener('fetch', (event: FetchEvent) => {
handleEvent(event);
});

async function handleEvent(event: FetchEvent) {

let resolve: any, reject: any;
let responsePromise = new Promise(async (res, rej) => {
resolve = res;
reject = rej;
});
//@ts-ignore
event.respondWith(responsePromise);

let res = new ResponseBuilder(resolve);

await handler(event.request, res)
}

// Keep wizer happy during pre-init. Should go away
// oncehttps://github.com/bytecodealliance/ComponentizeJS/issues/114 is resolved
export const incomingHandler = {
handle() { }
}
18 changes: 18 additions & 0 deletions examples/typescript/aws/s3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es2020",
"jsx": "react",
"skipLibCheck": true,
"lib": [
"ES2015",
"WebWorker"
],
"allowJs": true,
"strict": true,
"noImplicitReturns": true,
"moduleResolution": "node"
}
}
35 changes: 35 additions & 0 deletions examples/typescript/aws/s3/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")

module.exports = {
entry: './src/spin.ts',
experiments: {
outputModule: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.resolve(__dirname, './'),
filename: 'dist.js',
module: true,
library: {
type: "module",
}
},
plugins: [
new SpinSdkPlugin()
],
optimization: {
minimize: false
},
};
5 changes: 5 additions & 0 deletions examples/typescript/aws/sqs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
target
.spin/
dist.js
53 changes: 53 additions & 0 deletions examples/typescript/aws/sqs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# AWS SQS Integration

This example showcases how to connect to and send messages using Amazon SQS with the AWS SDK.

## Prerequisites
- `spin >=2.6.0`
-

## Install Dependencies
Install the necessary npm packages:

```bash
npm install
```

## Setup the Example

1. **Create an AWS Account**
- If you don't have an AWS account, create one at [AWS](https://aws.amazon.com/).

2. **Create an SQS Queue**
- Go to the [Amazon SQS Console](https://console.aws.amazon.com/sqs/).
- Create a new SQS queue and note down the Queue URL.

3. **Get AWS Credentials**
- Create or obtain your AWS credentials (Access Key ID, Secret Access Key, and optionally, a Session Token).

4. **Configure the Code**
- Copy the region, access key ID, secret access key, session token, and queue URL into the code at `src/index.ts`.

```typescript
const client = new SQSClient({
region: "<region>",
credentials: {
accessKeyId: "<accessKeyId>",
secretAccessKey: "<secretAccessKey>",
sessionToken: "<sessionToken>"
},
});

const params = {
MessageBody: 'This is a test message with SQSClient.',
QueueUrl: '<queueUrl>'
};

## Building and Running the Example

```bash
spin build
spin up
```

Use e.g. curl -v http://127.0.0.1:3000/ to test the endpoint.
Loading

0 comments on commit f178468

Please sign in to comment.