index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Guid, Server } from 'org.crazydoctor.expressts';
  2. import * as path from 'path';
  3. import os from 'os';
  4. import fs from 'fs';
  5. import { Sources } from './sources/Sources';
  6. import SHA256 from './util/SHA256';
  7. import CronSourcesUpdateTask from './util/CronSourcesUpdateTask';
  8. import { CommentsManager } from './comments/CommentsManager';
  9. import { WebSocket as WS } from 'ws';
  10. import WebSocketHandler from './websocket/WebSocket';
  11. import DBEvents from './db/DBEvents';
  12. class ServerApp {
  13. public static SourcesUpdating = false;
  14. public static Server: Server | null = null;
  15. public static WebSocketUrl: string = '/ws';
  16. public static GitHost: string = '';
  17. public static getWebSocketConnections(): Set<WS> | null {
  18. return ServerApp.Server?.getWsConnections(ServerApp.WebSocketUrl) || null;
  19. }
  20. public static async notifySocket(ws: WS, message: string): Promise<void> {
  21. return new Promise((resolve, reject) => {
  22. if(ws.readyState !== WS.OPEN)
  23. reject();
  24. ws.send(message, (err) => {
  25. if(err)
  26. return reject();
  27. return resolve();
  28. });
  29. });
  30. }
  31. public static async notifyAllSockets(message: string): Promise<void[]> {
  32. const promises: Promise<void>[] = [];
  33. ServerApp.getWebSocketConnections()?.forEach((connection) => {
  34. promises.push(ServerApp.notifySocket(connection, message));
  35. });
  36. return Promise.all(promises);
  37. }
  38. public static start(): void {
  39. const Config = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'config.json'), { encoding: 'utf-8' }).toString());
  40. const ServerConfig = Config.server;
  41. const SourcesConfig = Config.sources;
  42. const assetsDir = path.resolve(`${os.homedir()}/${SourcesConfig.assetsDir}`);
  43. if(!fs.existsSync(assetsDir))
  44. fs.mkdirSync(assetsDir);
  45. ServerApp.GitHost = ServerConfig.gitHost;
  46. CommentsManager.init(path.resolve(`${assetsDir}/comments`), ServerConfig.commentsRepoUrl);
  47. DBEvents.init(path.resolve(`${assetsDir}/events.db`));
  48. new Server({
  49. port: 9080,
  50. routesPath: path.resolve(__dirname, './routes'),
  51. middlewaresPath: path.resolve(__dirname, './middlewares'),
  52. viewsPath: path.resolve(__dirname, './views'),
  53. viewEngine: 'pug',
  54. wsHandlers: {[ServerApp.WebSocketUrl]: new WebSocketHandler()},
  55. options: {
  56. static: path.resolve(__dirname, './static'),
  57. sessionSecret: Guid.new(),
  58. adminPassword: SHA256.hash(ServerConfig.adminPassword),
  59. sessionMaxAge: 21600000
  60. }
  61. }).init().then((server) => {
  62. server.start(() => {
  63. ServerApp.SourcesUpdating = true;
  64. Sources.get(() => {
  65. ServerApp.SourcesUpdating = false;
  66. CronSourcesUpdateTask.start();
  67. });
  68. });
  69. ServerApp.Server = server;
  70. });
  71. }
  72. }
  73. ServerApp.start();
  74. export default ServerApp;