"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const plugins = require("./smartuniverse.plugins"); const smartexpress_1 = require("smartexpress"); const smartuniverse_classes_manager_1 = require("./smartuniverse.classes.manager"); const smartuniverse_classes_universestore_1 = require("./smartuniverse.classes.universestore"); const paths = require("./smartuniverse.paths"); class Universe { get universeVersion() { if (this.universeVersionStore) { return this.universeVersionStore; } else { const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson); this.universeVersionStore = packageJson.version; return this.universeVersionStore; } } constructor(optionsArg) { this.options = optionsArg; this.universeStore = new smartuniverse_classes_universestore_1.UniverseStore(this.options.messageExpiryInMilliseconds); this.universeManager = new smartuniverse_classes_manager_1.UniverseManager(); } /** * initiates a server */ initServer(portArg) { return __awaiter(this, void 0, void 0, function* () { this.smartexpressServer = new plugins.smartexpress.Server({ cors: true, defaultAnswer: `smartuniverse server ${this.universeVersion}`, forceSsl: false, port: portArg }); // route handling // adds messages const addMessageHandler = new smartexpress_1.Handler('PUT', request => { const requestBody = request.body; this.universeStore.addMessage(requestBody.message, requestBody.payload); console.log(requestBody); return true; }); // gets messages const readMessageHandler = new smartexpress_1.Handler('GET', request => { const requestBody = request.body; this.universeStore.readMessagesYoungerThan(requestBody.since); }); const messageRoute = new smartexpress_1.Route(this.smartexpressServer, 'message'); messageRoute.addHandler(addMessageHandler); messageRoute.addHandler(readMessageHandler); yield this.smartexpressServer.start(); }); } stopServer() { return __awaiter(this, void 0, void 0, function* () { yield this.smartexpressServer.stop(); }); } } exports.Universe = Universe; //# sourceMappingURL=data:application/json;base64,eyJ2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