index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __generator = (this && this.__generator) || function (thisArg, body) {
  35. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  36. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  37. function verb(n) { return function (v) { return step([n, v]); }; }
  38. function step(op) {
  39. if (f) throw new TypeError("Generator is already executing.");
  40. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  41. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  42. if (y = 0, t) op = [op[0] & 2, t.value];
  43. switch (op[0]) {
  44. case 0: case 1: t = op; break;
  45. case 4: _.label++; return { value: op[1], done: false };
  46. case 5: _.label++; y = op[1]; op = [0]; continue;
  47. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  48. default:
  49. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  50. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  51. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  52. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  53. if (t[2]) _.ops.pop();
  54. _.trys.pop(); continue;
  55. }
  56. op = body.call(thisArg, _);
  57. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  58. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  59. }
  60. };
  61. var __importDefault = (this && this.__importDefault) || function (mod) {
  62. return (mod && mod.__esModule) ? mod : { "default": mod };
  63. };
  64. Object.defineProperty(exports, "__esModule", { value: true });
  65. var express_1 = __importDefault(require("express"));
  66. var path = __importStar(require("path"));
  67. var coolkit_api_1 = __importDefault(require("coolkit-api"));
  68. var cors_1 = __importDefault(require("cors"));
  69. var user_1 = __importDefault(require("./route/user"));
  70. var devices_1 = __importDefault(require("./route/devices"));
  71. var language_1 = __importDefault(require("./route/language"));
  72. var stream_1 = __importDefault(require("./route/stream"));
  73. var ha_devices_1 = __importDefault(require("./route/ha-devices"));
  74. var util_1 = __importDefault(require("./route/util"));
  75. var initMdns_1 = __importDefault(require("./utils/initMdns"));
  76. var initCkWs_1 = __importDefault(require("./utils/initCkWs"));
  77. var initHaSocket_1 = __importDefault(require("./utils/initHaSocket"));
  78. var initCkApi_1 = __importDefault(require("./utils/initCkApi"));
  79. var app_1 = require("./config/app");
  80. var config_1 = require("./config/config");
  81. var redirectToAuth_1 = __importDefault(require("./middleware/redirectToAuth"));
  82. var AuthClass_1 = __importDefault(require("./class/AuthClass"));
  83. var eventBus_1 = __importDefault(require("./utils/eventBus"));
  84. var init_1 = require("./lib-ha/init");
  85. var process_1 = __importDefault(require("process"));
  86. var logger_1 = require("./utils/logger");
  87. coolkit_api_1.default.init({
  88. useTestEnv: process_1.default.env.CK_API_ENV === 'test',
  89. appId: app_1.appId,
  90. appSecret: app_1.appSecret,
  91. });
  92. (function () { return __awaiter(void 0, void 0, void 0, function () {
  93. var res;
  94. return __generator(this, function (_a) {
  95. switch (_a.label) {
  96. case 0:
  97. (0, initMdns_1.default)();
  98. return [4, AuthClass_1.default.init()];
  99. case 1:
  100. res = _a.sent();
  101. if (!AuthClass_1.default.curAuth) return [3, 2];
  102. eventBus_1.default.emit('init-ha-socket');
  103. return [3, 4];
  104. case 2: return [4, (0, initHaSocket_1.default)()];
  105. case 3:
  106. _a.sent();
  107. _a.label = 4;
  108. case 4: return [4, (0, initCkApi_1.default)()];
  109. case 5:
  110. _a.sent();
  111. return [4, (0, initCkWs_1.default)()];
  112. case 6:
  113. _a.sent();
  114. return [4, (0, init_1.init)()];
  115. case 7:
  116. _a.sent();
  117. return [2];
  118. }
  119. });
  120. }); })();
  121. var app = (0, express_1.default)();
  122. var port = 3000;
  123. var apiPrefix = '/api';
  124. if (config_1.debugMode) {
  125. app.use((0, cors_1.default)());
  126. }
  127. app.use(express_1.default.json());
  128. app.use(express_1.default.urlencoded({ extended: false }));
  129. app.use("".concat(apiPrefix, "/user"), user_1.default);
  130. app.use('/', express_1.default.static(path.join(__dirname, '/pages')));
  131. app.use(redirectToAuth_1.default);
  132. app.use("".concat(apiPrefix, "/language"), language_1.default);
  133. app.use("".concat(apiPrefix, "/devices"), devices_1.default);
  134. app.use("".concat(apiPrefix, "/stream"), stream_1.default);
  135. app.use("".concat(apiPrefix, "/util"), util_1.default);
  136. app.use("".concat(apiPrefix, "/ha-devices"), ha_devices_1.default);
  137. app.use('/', function (req, res) {
  138. res.type('.html');
  139. res.sendFile(path.join(__dirname, '/pages/index.html'));
  140. });
  141. app.listen(port, function () {
  142. logger_1.logger.info("server is running at ".concat(port));
  143. });