server.cjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const fs = require('node:fs')
  2. const path = require('node:path')
  3. const crypto = require('node:crypto')
  4. const companion = require('@uppy/companion')
  5. require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env') })
  6. const app = require('express')()
  7. const DATA_DIR = path.join(__dirname, 'tmp')
  8. app.use(require('cors')({
  9. origin: 'http://localhost:3000',
  10. methods: ['GET', 'POST', 'OPTIONS'],
  11. credentials: true,
  12. }))
  13. app.use(require('cookie-parser')())
  14. app.use(require('body-parser').json())
  15. app.use(require('express-session')({
  16. secret: 'hello planet',
  17. saveUninitialized: false,
  18. resave: false,
  19. }))
  20. const options = {
  21. providerOptions: {
  22. drive: {
  23. key: process.env.COMPANION_GOOGLE_KEY,
  24. secret: process.env.COMPANION_GOOGLE_SECRET,
  25. },
  26. },
  27. s3: {
  28. getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
  29. key: process.env.COMPANION_AWS_KEY,
  30. secret: process.env.COMPANION_AWS_SECRET,
  31. bucket: process.env.COMPANION_AWS_BUCKET,
  32. region: process.env.COMPANION_AWS_REGION,
  33. endpoint: process.env.COMPANION_AWS_ENDPOINT,
  34. },
  35. server: { host: 'localhost:3020' },
  36. filePath: DATA_DIR,
  37. secret: 'blah blah',
  38. debug: true,
  39. }
  40. // Create the data directory here for the sake of the example.
  41. try {
  42. fs.accessSync(DATA_DIR)
  43. } catch (err) {
  44. fs.mkdirSync(DATA_DIR)
  45. }
  46. process.on('exit', () => {
  47. fs.rmSync(DATA_DIR, { recursive: true, force: true })
  48. })
  49. const { app: companionApp } = companion.app(options)
  50. app.use(companionApp)
  51. const server = app.listen(3020, () => {
  52. console.log('listening on port 3020')
  53. })
  54. companion.socket(server)