server.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const fs = require('node:fs')
  2. const path = require('node:path')
  3. const budo = require('budo')
  4. const router = require('router')
  5. const crypto = require('node:crypto')
  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 app = router()
  22. // Set up the /params endpoint that will create signed URLs for us.
  23. app.use(require('cors')())
  24. app.use(require('body-parser').json())
  25. const { app: companionApp } = companion.app({
  26. providerOptions: {
  27. s3: {
  28. // This is the crucial part; set an endpoint template for the service you want to use.
  29. endpoint: 'https://{region}.digitaloceanspaces.com',
  30. getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
  31. key: process.env.COMPANION_AWS_KEY,
  32. secret: process.env.COMPANION_AWS_SECRET,
  33. bucket: process.env.COMPANION_AWS_BUCKET,
  34. region: process.env.COMPANION_AWS_REGION,
  35. },
  36. },
  37. server: { serverUrl: `localhost:${PORT}` },
  38. })
  39. app.use('/companion', companionApp)
  40. // Serve the built CSS file.
  41. app.get('/uppy.min.css', (req, res) => {
  42. res.setHeader('content-type', 'text/css')
  43. fs.createReadStream(path.join('../../packages/uppy/dist/uppy.min.css')).pipe(res)
  44. })
  45. // Start the development server, budo.
  46. budo(path.join(__dirname, 'main.js'), {
  47. live: true,
  48. stream: process.stdout,
  49. port: PORT,
  50. middleware: app,
  51. browserify: {
  52. transform: ['babelify'],
  53. },
  54. })