server.cjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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: ({ 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. forcePathStyle: process.env.COMPANION_AWS_FORCE_PATH_STYLE === 'true',
  35. },
  36. server: { host: 'localhost:3020' },
  37. filePath: DATA_DIR,
  38. secret: 'blah blah',
  39. debug: true,
  40. }
  41. // Create the data directory here for the sake of the example.
  42. try {
  43. fs.accessSync(DATA_DIR)
  44. } catch (err) {
  45. fs.mkdirSync(DATA_DIR)
  46. }
  47. process.on('exit', () => {
  48. fs.rmSync(DATA_DIR, { recursive: true, force: true })
  49. })
  50. const { app: companionApp } = companion.app(options)
  51. app.use(companionApp)
  52. const server = app.listen(3020, () => {
  53. console.log('listening on port 3020')
  54. })
  55. companion.socket(server)