server.cjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. */
  15. 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")')
  16. if (!process.env.COMPANION_AWS_KEY) throw new Error('Missing access key, please set the COMPANION_AWS_KEY environment variable')
  17. if (!process.env.COMPANION_AWS_SECRET) throw new Error('Missing secret key, please set the COMPANION_AWS_SECRET environment variable')
  18. if (!process.env.COMPANION_AWS_BUCKET) throw new Error('Missing Space name, please set the COMPANION_AWS_BUCKET environment variable')
  19. // Prepare the server.
  20. const PORT = process.env.PORT || 3452
  21. const host = `localhost:${PORT}`
  22. const DATA_DIR = path.join(__dirname, 'tmp')
  23. fs.mkdirSync(DATA_DIR, { recursive: true })
  24. // Set up the /params endpoint that will create signed URLs for us.
  25. app.use(require('cors')())
  26. app.use(require('body-parser').json())
  27. const { app: companionApp } = companion.app({
  28. s3: {
  29. // This is the crucial part; set an endpoint template for the service you want to use.
  30. endpoint: 'https://{region}.digitaloceanspaces.com',
  31. getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
  32. key: process.env.COMPANION_AWS_KEY,
  33. secret: process.env.COMPANION_AWS_SECRET,
  34. bucket: process.env.COMPANION_AWS_BUCKET,
  35. region: process.env.COMPANION_AWS_REGION,
  36. },
  37. server: { host },
  38. filePath: DATA_DIR,
  39. secret: 'blah blah',
  40. debug: true,
  41. })
  42. app.use('/companion', companionApp)
  43. require('vite').createServer({ clearScreen: false, server:{ middlewareMode: true } }).then(({ middlewares }) => {
  44. app.use(middlewares)
  45. app.listen(PORT, () => {
  46. console.log(`Listening on http://localhost:${PORT}/...`)
  47. })
  48. })