smartuniverse/ts/smartuniverse.classes.universe.ts

96 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress';
import { UniverseManager } from './smartuniverse.classes.manager';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { UniverseStore } from './smartuniverse.classes.universestore';
import * as paths from './smartuniverse.paths';
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
export interface IServerGetMessagesRequestBody {
youngerThan: number;
}
export interface IServerPutMessageRequestBody {
2018-03-20 07:16:54 +00:00
message: string;
payload: any;
2018-03-13 05:15:40 +00:00
}
export class Universe {
// subinstances
public universeStore: UniverseStore;
public universeManager: UniverseManager;
// options
private options: ISmartUniverseConstructorOptions;
2018-03-20 07:16:54 +00:00
2018-03-13 05:15:40 +00:00
// Store version handling
private universeVersionStore: string;
private get universeVersion() {
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
}
private smartexpressServer: plugins.smartexpress.Server;
2018-03-20 07:16:54 +00:00
private smartsocket: plugins.smartsocket.Smartsocket;
2018-03-13 05:15:40 +00:00
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
this.universeManager = new UniverseManager();
}
/**
* initiates a server
*/
public async initServer(portArg: number | string) {
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
forceSsl: false,
port: portArg
});
2018-03-20 07:16:54 +00:00
this.smartsocket = new plugins.smartsocket.Smartsocket({
port: 12345 // fix this within smartsocket
});
this.smartsocket.setServer(this.smartexpressServer as any); // should work with express as well
this.smartsocket.startServer();
2018-03-13 05:15:40 +00:00
// route handling
2018-03-15 00:16:16 +00:00
// adds messages
2018-03-13 05:15:40 +00:00
const addMessageHandler = new Handler('PUT', request => {
const requestBody = request.body;
this.universeStore.addMessage(requestBody.message, requestBody.payload);
2018-03-15 00:16:16 +00:00
console.log(requestBody);
2018-03-13 05:15:40 +00:00
return true;
});
2018-03-15 00:16:16 +00:00
// gets messages
2018-03-13 05:15:40 +00:00
const readMessageHandler = new Handler('GET', request => {
const requestBody = request.body;
this.universeStore.readMessagesYoungerThan(requestBody.since);
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
const messageRoute = new Route(this.smartexpressServer, 'message');
messageRoute.addHandler(addMessageHandler);
messageRoute.addHandler(readMessageHandler);
await this.smartexpressServer.start();
}
2018-03-20 07:16:54 +00:00
public async stopServer() {
2018-03-13 05:15:40 +00:00
await this.smartexpressServer.stop();
}
}