X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=src%2Fservice.js;h=df56ba0ba79137fea5265b91008ef002ae53503c;hb=f92c90e92ded661ddd957580e7fbde8aaa24224d;hp=dfba6b8f9747a20694aa9f8f1c4a268857d5ee5e;hpb=9696c012e6b9a6c58904baa397ca0ebf78112316;p=websub-hub diff --git a/src/service.js b/src/service.js index dfba6b8..df56ba0 100644 --- a/src/service.js +++ b/src/service.js @@ -40,14 +40,14 @@ class Service extends Dingus { // These routes are intended for accessing static content during development. // In production, a proxy server would likely handle these first. - this.on(['GET', 'HEAD'], '/static', (req, res, ctx) => this.handlerRedirect(req, res, ctx, `${options.dingus.proxyPrefix}/static/`)); - this.on(['GET', 'HEAD'], '/static/', (req, res, ctx) => this.handlerGetStaticFile(req, res, ctx, 'index.html')); + this.on(['GET', 'HEAD'], '/static', this.handlerRedirect.bind(this), `${options.dingus.proxyPrefix}/static/`); + this.on(['GET', 'HEAD'], '/static/', this.handlerGetStaticFile.bind(this), 'index.html'); this.on(['GET', 'HEAD'], '/static/:file', this.handlerGetStaticFile.bind(this)); - this.on(['GET', 'HEAD'], '/favicon.ico', (req, res, ctx) => this.handlerGetStaticFile(req, res, ctx, 'favicon.ico')); - this.on(['GET', 'HEAD'], '/robots.txt', (req, res, ctx) => this.handlerGetStaticFile(req, res, ctx, 'robots.txt')); + this.on(['GET', 'HEAD'], '/favicon.ico', this.handlerGetStaticFile.bind(this), 'favicon.ico'); + this.on(['GET', 'HEAD'], '/robots.txt', this.handlerGetStaticFile.bind(this), 'robots.txt'); // Private informational endpoints - this.on(['GET', 'HEAD'], '/admin', (req, res, ctx) => this.handlerRedirect(req, res, ctx, `${options.dingus.proxyPrefix}/admin/`)); + this.on(['GET', 'HEAD'], '/admin', this.handlerRedirect.bind(this), `${options.dingus.proxyPrefix}/admin/`); this.on(['GET', 'HEAD'], '/admin/', this.handlerGetAdminOverview.bind(this)); this.on(['GET', 'HEAD'], '/admin/topic/:topicId', this.handlerGetAdminTopicDetails.bind(this)); @@ -60,22 +60,6 @@ class Service extends Dingus { } - /** - * @param {http.ClientRequest} req - * @param {http.ServerResponse} res - * @param {Object} ctx - * @param {String} newPath - */ - async handlerRedirect(req, res, ctx, newPath) { - const _scope = _fileScope('handlerRedirect'); - this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx }); - - res.setHeader(Enum.Header.Location, newPath); - res.statusCode = 307; // Temporary Redirect - res.end(); - } - - /** * @param {http.ClientRequest} req * @param {http.ServerResponse} res @@ -185,8 +169,9 @@ class Service extends Dingus { await this.manager.getTopicDetails(res, ctx); } + /** - * Same as super.ingestBody, but if no body was send, do not parse (and + * Same as super.ingestBody, but if no body was sent, do not parse (and * thus avoid possible unsupported media type error). * @param {http.ClientRequest} req * @param {http.ServerResponse} res @@ -206,7 +191,7 @@ class Service extends Dingus { * @param {http.ServerResponse} res * @param {Object} ctx */ - async handlerUpdateTopic(req, res, ctx) { + async handlerUpdateTopic(req, res, ctx) { const _scope = _fileScope('handlerUpdateTopic'); this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx }); @@ -226,37 +211,18 @@ class Service extends Dingus { * @param {Object} ctx */ async handlerUpdateSubscription(req, res, ctx) { - const _scope = _fileScope('handlerUpdateSubscription'); - this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx }); - - this.setResponseType(this.responseTypes, req, res, ctx); - - await this.authenticator.required(req, res, ctx); - - await this.maybeIngestBody(req, res, ctx); - ctx.method = req.method; - await this.manager.updateSubscription(res, ctx); -} - - - /** - * @param {http.ClientRequest} req - * @param {http.ServerResponse} res - * @param {object} ctx - */ - async handlerGetStaticFile(req, res, ctx, file) { - const _scope = _fileScope('handlerGetStaticFile'); - this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx, file }); - - Dingus.setHeadHandler(req, res, ctx); + const _scope = _fileScope('handlerUpdateSubscription'); + this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx }); - // Set a default response type to handle any errors; will be re-set to serve actual static content type. this.setResponseType(this.responseTypes, req, res, ctx); - await this.serveFile(req, res, ctx, this.staticPath, file || ctx.params.file); - this.logger.info(_scope, 'finished', { ctx }); - } + await this.authenticator.required(req, res, ctx); + await this.maybeIngestBody(req, res, ctx); + ctx.method = req.method; + await this.manager.updateSubscription(res, ctx); + } + /** * @param {http.ClientRequest} req