UNPKG

@donmahallem/trapeze-client-desktop

Version:

An electron app to be used with trapeze endpoints

108 lines 3.94 kB
"use strict"; /*! * Source https://github.com/donmahallem/trapeze */ Object.defineProperty(exports, "__esModule", { value: true }); const trapeze_api_proxy_router_1 = require("@donmahallem/trapeze-api-proxy-router"); const express = require("express"); const helmet = require("helmet"); const path_1 = require("path"); const server_error_request_handler_1 = require("./api-server/server-error-request-handler"); exports.api404Handler = (req, res, next) => { res.status(404).json({ statusCode: 404, }); }; /** * Api Server */ class ApiServer { /** * Api Server for the Trapeze Api Wrapper * @param config Config to be used to start the server */ constructor(config) { this.config = config; this.isRunning = false; this.ngModulePath = path_1.resolve(__dirname + './../node_modules/@donmahallem/trapeze-client-ng/dist/trapeze-client-ng'); this.app = express(); this.app.use(this.createAuthMiddleware(this.config.secret)); this.app.use(helmet.contentSecurityPolicy({ directives: { connectSrc: ['\'self\'', 'https://c.tile.openstreetmap.org', 'https://b.tile.openstreetmap.org', 'https://a.tile.openstreetmap.org'], defaultSrc: ['\'self\''], imgSrc: ['\'self\'', 'https://c.tile.openstreetmap.org', 'https://b.tile.openstreetmap.org', 'https://a.tile.openstreetmap.org', 'data:'], scriptSrc: ['\'self\'', '\'unsafe-inline\''], styleSrc: ['\'self\'', '\'unsafe-inline\''], }, })); this.app.use('/api', trapeze_api_proxy_router_1.createTrapezeApiProxyRouter(this.config.endpoint)); this.app.use('/api', exports.api404Handler); this.app.use(express.static(this.ngModulePath)); this.app.get('/*', (req, res) => { // tslint:disable-next-line:no-console console.info('Not found', req.path); res.status(404).sendFile(this.ngModulePath + '/index.html'); }); this.app.use(server_error_request_handler_1.createErrorRequestHandler()); } /** * Checks the Auth Header for the Api Token * @param secret Secret to be checked in the auth header */ createAuthMiddleware(secret) { return (req, res, next) => { // checks if the Authorization Header is set if (req.headers.authorization) { const splits = req.headers.authorization.split(' '); if (splits.length !== 2) { next(new Error('Bearer token request expected')); return; } if (splits[0] === 'Bearer' && splits[1] === this.config.secret) { next(); return; } } next(new Error('No Authorization Header provided')); }; } /** * Starts the app server */ start() { if (this.isRunning) { return Promise.resolve(); } return new Promise((resolve, reject) => { this.server = this.app.listen(this.config.port, (...args) => { if (args.length > 0) { reject(args[0]); } else { this.isRunning = true; resolve(); } }); }); } /** * Stops the app server */ stop() { this.server.close((err) => { // tslint:disable-next-line:no-console console.log('Server closed', err); }); } } exports.ApiServer = ApiServer; //# sourceMappingURL=api-server.js.map