server.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { flags } from '@oclif/command'
  2. import { createApp } from '../services/webApi/app'
  3. import ApiCommandBase from '../command-base/ApiCommandBase'
  4. import logger from '../services/logger'
  5. export default class Server extends ApiCommandBase {
  6. static description = 'Starts the storage node server.'
  7. static flags = {
  8. worker: flags.integer({
  9. char: 'w',
  10. required: true,
  11. description: 'Storage provider worker ID',
  12. }),
  13. uploads: flags.string({
  14. char: 'd',
  15. required: true,
  16. description: 'Data uploading directory (absolute path).',
  17. }),
  18. port: flags.integer({
  19. char: 'o',
  20. required: true,
  21. description: 'Server port.',
  22. }),
  23. ...ApiCommandBase.flags,
  24. }
  25. static args = [{ name: 'file' }]
  26. async run(): Promise<void> {
  27. const { flags } = this.parse(Server)
  28. if (flags.dev) {
  29. await this.ensureDevelopmentChain()
  30. }
  31. const account = this.getAccount(flags)
  32. const api = await this.getApi()
  33. try {
  34. const port = flags.port
  35. const workerId = flags.worker ?? 0
  36. const app = await createApp(api, account, workerId, flags.uploads)
  37. logger.info(`Listening on http://localhost:${port}`)
  38. app.listen(port)
  39. } catch (err) {
  40. logger.error(`Error: ${err}`)
  41. }
  42. }
  43. // Override exiting.
  44. /* eslint-disable @typescript-eslint/no-empty-function */
  45. async finally(): Promise<void> {}
  46. }