Skip to content

Commit

Permalink
Working whip-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Dec 31, 2021
1 parent c7dba13 commit cd7fd2e
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.DS_Store
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ services:
ports:
- $SMTP_PORT:25
- $MAILDEV_PORT:80

minio:
image: minio/minio
ports:
- $MINIO_PORT:9000
- $MINIO_CONSOLE_PORT:$MINIO_CONSOLE_PORT
entrypoint: bash -c 'mkdir -p /opt/minio/whip && minio server --console-address :$MINIO_CONSOLE_PORT /opt/minio'
environment:
MINIO_CONSOLE_PORT: $MINIO_CONSOLE_PORT
MINIO_ROOT_USER: $MINIO_ROOT_USER
MINIO_ROOT_PASSWORD: $MINIO_ROOT_PASSWORD
4 changes: 4 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ DB_PASS=wokeUpLikeThis
DATABASE_URL="postgresql://whip:wokeUpLikeThis@localhost:5432/whip?schema=public"
SMTP_PORT=25
MAILDEV_PORT=1080
MINIO_PORT=9000
MINIO_CONSOLE_PORT=9001
MINIO_ROOT_USER=whip
MINIO_ROOT_PASSWORD=dontWasteNoTime
9 changes: 9 additions & 0 deletions packages/whip-storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { S3Client } from '@aws-sdk/client-s3'

export default function storagePlugin (app, opts) {
app.storage = new S3Client(opts)
app.use(function storageMiddleware (req, res, next) {
req.storage = app.storage
next()
})
}
4 changes: 4 additions & 0 deletions packages/whip-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "@generates/whip-storage",
"version": "0.0.0",
"license": "UNLICENSED",
"type": "module",
"scripts": {
"example.app": "../../example.js ./tests/fixtures/app.js"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.45.0"
}
Expand Down
38 changes: 38 additions & 0 deletions packages/whip-storage/tests/fixtures/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { promises as fs } from 'fs'
import { nanoid } from 'nanoid'
import { create } from '@generates/whip'
import { PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3'
import storage from '../../index.js'

const Bucket = 'whip'
const fileUrl = new URL('josh-duncan-trunk-bay.jpg', import.meta.url)
const app = create()

app.add({
plugin: storage,
opts: {
region: 'us-east-1',
forcePathStyle: true,
endpoint: 'http://localhost:9000',
credentials: { accessKeyId: 'whip', secretAccessKey: 'dontWasteNoTime' }
}
})

let Key
app.get('/', async function download (req, res) {
if (Key) {
const out = await req.storage.send(new GetObjectCommand({ Bucket, Key }))
// TODO:
// req.logger.info('GetObjectCommand response', out)
res.set('Content-Type', 'image/jpeg').send(out.Body)
} else {
Key = nanoid()
const Body = await fs.readFile(fileUrl)
const command = new PutObjectCommand({ Bucket, Key, Body })
const out = await req.storage.send(command)
req.logger.info('PutObjectCommand response', out)
res.send('File uploaded')
}
})

export default app
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/whip-storage/tests/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '@ianwalter/bff'

test('', t => {

})
3 changes: 3 additions & 0 deletions packages/whip/lib/middleware/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default function responseMiddleware (req, res, next) {
if (typeof body === 'string') {
res.writeHead(statusCode, req.state.headers)
res.end(body)
} else if (typeof body?.pipe === 'function') {
res.writeHead(statusCode, req.state.headers)
body.pipe(res)
} else {
res
.set('Content-Type', 'application/json')
Expand Down

0 comments on commit cd7fd2e

Please sign in to comment.