Skip to content

Commit cd7fd2e

Browse files
author
Ian Walter
committed
Working whip-storage
1 parent c7dba13 commit cd7fd2e

File tree

9 files changed

+75
-0
lines changed

9 files changed

+75
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.env
3+
.DS_Store

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ services:
2222
ports:
2323
- $SMTP_PORT:25
2424
- $MAILDEV_PORT:80
25+
26+
minio:
27+
image: minio/minio
28+
ports:
29+
- $MINIO_PORT:9000
30+
- $MINIO_CONSOLE_PORT:$MINIO_CONSOLE_PORT
31+
entrypoint: bash -c 'mkdir -p /opt/minio/whip && minio server --console-address :$MINIO_CONSOLE_PORT /opt/minio'
32+
environment:
33+
MINIO_CONSOLE_PORT: $MINIO_CONSOLE_PORT
34+
MINIO_ROOT_USER: $MINIO_ROOT_USER
35+
MINIO_ROOT_PASSWORD: $MINIO_ROOT_PASSWORD

example.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ DB_PASS=wokeUpLikeThis
66
DATABASE_URL="postgresql://whip:wokeUpLikeThis@localhost:5432/whip?schema=public"
77
SMTP_PORT=25
88
MAILDEV_PORT=1080
9+
MINIO_PORT=9000
10+
MINIO_CONSOLE_PORT=9001
11+
MINIO_ROOT_USER=whip
12+
MINIO_ROOT_PASSWORD=dontWasteNoTime

packages/whip-storage/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { S3Client } from '@aws-sdk/client-s3'
2+
3+
export default function storagePlugin (app, opts) {
4+
app.storage = new S3Client(opts)
5+
app.use(function storageMiddleware (req, res, next) {
6+
req.storage = app.storage
7+
next()
8+
})
9+
}

packages/whip-storage/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"name": "@generates/whip-storage",
33
"version": "0.0.0",
44
"license": "UNLICENSED",
5+
"type": "module",
6+
"scripts": {
7+
"example.app": "../../example.js ./tests/fixtures/app.js"
8+
},
59
"dependencies": {
610
"@aws-sdk/client-s3": "^3.45.0"
711
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { promises as fs } from 'fs'
2+
import { nanoid } from 'nanoid'
3+
import { create } from '@generates/whip'
4+
import { PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3'
5+
import storage from '../../index.js'
6+
7+
const Bucket = 'whip'
8+
const fileUrl = new URL('josh-duncan-trunk-bay.jpg', import.meta.url)
9+
const app = create()
10+
11+
app.add({
12+
plugin: storage,
13+
opts: {
14+
region: 'us-east-1',
15+
forcePathStyle: true,
16+
endpoint: 'http://localhost:9000',
17+
credentials: { accessKeyId: 'whip', secretAccessKey: 'dontWasteNoTime' }
18+
}
19+
})
20+
21+
let Key
22+
app.get('/', async function download (req, res) {
23+
if (Key) {
24+
const out = await req.storage.send(new GetObjectCommand({ Bucket, Key }))
25+
// TODO:
26+
// req.logger.info('GetObjectCommand response', out)
27+
res.set('Content-Type', 'image/jpeg').send(out.Body)
28+
} else {
29+
Key = nanoid()
30+
const Body = await fs.readFile(fileUrl)
31+
const command = new PutObjectCommand({ Bucket, Key, Body })
32+
const out = await req.storage.send(command)
33+
req.logger.info('PutObjectCommand response', out)
34+
res.send('File uploaded')
35+
}
36+
})
37+
38+
export default app
74.9 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '@ianwalter/bff'
2+
3+
test('', t => {
4+
5+
})

packages/whip/lib/middleware/response.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export default function responseMiddleware (req, res, next) {
1515
if (typeof body === 'string') {
1616
res.writeHead(statusCode, req.state.headers)
1717
res.end(body)
18+
} else if (typeof body?.pipe === 'function') {
19+
res.writeHead(statusCode, req.state.headers)
20+
body.pipe(res)
1821
} else {
1922
res
2023
.set('Content-Type', 'application/json')

0 commit comments

Comments
 (0)