UNPKG

rhamt-vscode-extension

Version:

RHAMT VSCode extension

75 lines (67 loc) 2.84 kB
import * as express from 'express'; import * as path from 'path'; import * as bodyParser from 'body-parser'; import * as http from 'http'; import * as io from 'socket.io'; import { RhamtModelService } from 'raas-core'; import { ServerController } from './serverController'; import { Endpoint } from './endpoint'; import { SocketDelegate } from './socketDelegate'; import { EditorService } from './editorService'; import { RaasClient } from 'raas-client'; export class Server { public app: express.Application; private server: http.Server; private endpoint: Endpoint; private controller: ServerController; private modelService: RhamtModelService; private editorService: EditorService; constructor( endpoint: Endpoint, controller: ServerController, modelService: RhamtModelService, editorService: EditorService) { this.endpoint = endpoint; this.controller = controller; this.modelService = modelService; this.editorService = editorService; } public start(): void { this.app = express(); this.server = this.app.listen(this.endpoint.port); const listener = io.listen(this.server); listener.sockets.on('connection', this.connectClient.bind(this)); this.configServer(); this.routes(); } connectClient(s: io.Socket) { const config = this.modelService.getConfiguration(s.handshake.query.id); if (config) { this.editorService.connect(config, new SocketDelegate(s), new RaasClient()); } } private configServer() { this.app.use(bodyParser.json()); this.app.use(bodyParser.urlencoded({ extended: true })); this.app.set('views', path.join(__dirname, '..', '..', 'resources', 'views')); this.app.set('view engine', 'jade'); this.app.use(express.static(path.join(__dirname, '..', '..', 'resources'))); this.app.use('/node_modules', express.static(path.join(__dirname, '..', '..', 'node_modules'))); this.app.use('/out', express.static(path.join(__dirname, '..', '..', 'out'))); this.app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) { err.status = 404; next(err); }); } private routes() { const router = express.Router(); router.get('/:id', this.controller.get.bind(this.controller)); router.get('/configuration/:id', this.controller.configuration.bind(this.controller)); router.post('/save', this.controller.save.bind(this.controller)); router.post('/configuration/:id/option', this.controller.updateOption.bind(this.controller)); this.app.use(router); } public dispose(): void { this.server.close(); } }