server.cjs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const fs = require('node:fs')
  2. const path = require('node:path')
  3. const crypto = require('node:crypto')
  4. require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env') })
  5. const app = require('express')()
  6. const companion = require('../../packages/@uppy/companion')
  7. /**
  8. * Environment variables:
  9. *
  10. * - COMPANION_AWS_REGION - Your space region, eg "ams3"
  11. * - COMPANION_AWS_KEY - Your access key ID
  12. * - COMPANION_AWS_SECRET - Your secret access key
  13. * - COMPANION_AWS_BUCKET - Your space's name.
  14. * - COMPANION_AWS_FORCE_PATH_STYLE - Indicates if s3ForcePathStyle should be used rather than subdomain for S3 buckets.
  15. */
  16. if (!process.env.COMPANION_AWS_REGION) throw new Error('Missing Space region, please set the COMPANION_AWS_REGION environment variable (eg. "COMPANION_AWS_REGION=ams3")')
  17. if (!process.env.COMPANION_AWS_KEY) throw new Error('Missing access key, please set the COMPANION_AWS_KEY environment variable')
  18. if (!process.env.COMPANION_AWS_SECRET) throw new Error('Missing secret key, please set the COMPANION_AWS_SECRET environment variable')
  19. if (!process.env.COMPANION_AWS_BUCKET) throw new Error('Missing Space name, please set the COMPANION_AWS_BUCKET environment variable')
  20. // Prepare the server.
  21. const PORT = process.env.PORT || 3452
  22. const host = `localhost:${PORT}`
  23. const DATA_DIR = path.join(__dirname, 'tmp')
  24. fs.mkdirSync(DATA_DIR, { recursive: true })
  25. // Set up the /params endpoint that will create signed URLs for us.
  26. app.use(require('cors')())
  27. app.use(require('body-parser').json())
  28. const { app: companionApp } = companion.app({
  29. s3: {
  30. // This is the crucial part; set an endpoint template for the service you want to use.
  31. endpoint: 'https://{region}.digitaloceanspaces.com',
  32. getKey: ({ filename }) => `${crypto.randomUUID()}-${filename}`,
  33. key: process.env.COMPANION_AWS_KEY,
  34. secret: process.env.COMPANION_AWS_SECRET,
  35. bucket: process.env.COMPANION_AWS_BUCKET,
  36. region: process.env.COMPANION_AWS_REGION,
  37. forcePathStyle: process.env.COMPANION_AWS_FORCE_PATH_STYLE === 'true',
  38. },
  39. server: { host },
  40. filePath: DATA_DIR,
  41. secret: 'blah blah',
  42. debug: true,
  43. })
  44. app.use('/companion', companionApp)
  45. require('vite').createServer({ clearScreen: false, server:{ middlewareMode: true } }).then(({ middlewares }) => {
  46. app.use(middlewares)
  47. app.listen(PORT, () => {
  48. console.log(`Listening on http://localhost:${PORT}/...`)
  49. })
  50. })