app.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * 全局定义
  3. * @param app
  4. */
  5. 'use strict';
  6. global.CODE = require('./app/const/statusCode');
  7. const fs = require('fs');
  8. const path = require('path');
  9. const lowdb = require('lowdb');
  10. const FileSync = require('lowdb/adapters/FileSync');
  11. const utils = require('./app/utils/utils');
  12. class AppBootHook {
  13. constructor(app) {
  14. this.app = app;
  15. global.OS_PLATFORM = process.platform;
  16. global.IS_WIN = /^win/.test(process.platform);
  17. }
  18. configWillLoad() {
  19. // Ready to call configDidLoad,
  20. // Config, plugin files are referred,
  21. // this is the last chance to modify the config.
  22. }
  23. configDidLoad() {
  24. // Config, plugin files have been loaded.
  25. }
  26. async didLoad() {
  27. // All files have loaded, start plugin here.
  28. }
  29. async willReady() {
  30. // All plugins have started, can do some thing before app ready
  31. }
  32. async didReady() {
  33. // Worker is ready, can do some things
  34. // don't need to block the app boot.
  35. // 数据库
  36. const storageDir = path.normalize('./storage/');
  37. if (!fs.existsSync(storageDir)) {
  38. utils.mkdir(storageDir);
  39. utils.chmodPath(storageDir, '777');
  40. }
  41. const file = storageDir + 'db.json';
  42. const adapter = new FileSync(file);
  43. const db = lowdb(adapter);
  44. if (!db.has('default').value()) {
  45. db.set('default', {}).write();
  46. }
  47. }
  48. async serverDidReady() {
  49. // Server is listening.
  50. // const storageFile = './storage';
  51. // utils.chmodPath(storageFile, '777');
  52. }
  53. async beforeClose() {
  54. // Do some thing before app close.
  55. }
  56. }
  57. module.exports = AppBootHook;