main.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/node
  2. const path = require('path');
  3. const fs = require('fs');
  4. const Router = require('node-simple-router');
  5. const http = require('http');
  6. const CONFIG = require('./src/config.js');
  7. const RouterUtils = require('./src/routerUtils.js').RouterUtils;
  8. const ClockWatch = require('./src/clockwatch.js').ClockWatch;
  9. function App() {
  10. this.router = new Router({ static_route: __dirname+"/static/" });
  11. const _app = this;
  12. this.routerUtils = new RouterUtils(this);
  13. this.databaseHelper = require('./src/databaseHelper.js').DatabaseHelper;
  14. this.dataDir = __dirname+'/data/';
  15. }
  16. App.prototype.getData = function(privId) {
  17. return path.join(this.dataDir, privId);
  18. }
  19. App.prototype.init = async function() {
  20. if (!fs.existsSync(this.dataDir))
  21. fs.mkdirSync(this.dataDir);
  22. require('./router/mdi.js').register(this);
  23. require('./router/input.js').register(this);
  24. require('./router/qrcode.js').register(this);
  25. await this.databaseHelper.init();
  26. }
  27. App.prototype.clock = async function() {
  28. await ClockWatch.tick(this);
  29. setTimeout(async () => await this.clock(), 10 *60 *1000); // 10 minutes
  30. }
  31. App.prototype.run = function() {
  32. this.clock();
  33. http.createServer(this.router).listen(CONFIG.port);
  34. }
  35. let app = new App();
  36. app.init().then(() => app.run());