4 * Here we extend the base API server to define our routes and any route-specific
5 * behavior (middlewares) before handing off to the manager.
8 const { Dingus
} = require('@squeep/api-dingus');
9 const common
= require('./common');
10 const Enum
= require('./enum');
11 const Manager
= require('./manager');
12 const { Authenticator
, SessionManager
} = require('@squeep/authentication-module');
13 const path
= require('path');
15 const _fileScope
= common
.fileScope(__filename
);
17 class Service
extends Dingus
{
18 constructor(logger
, db
, options
) {
21 ignoreTrailingSlash: false,
24 this.manager
= new Manager(logger
, db
, options
);
25 this.authenticator
= new Authenticator(logger
, db
, options
);
26 this.sessionManager
= new SessionManager(logger
, this.authenticator
, options
);
27 this.staticPath
= path
.join(__dirname
, '..', 'static');
28 this.loginPath
= `${options.dingus.proxyPrefix}/admin/login`;
30 // Primary API endpoint
31 this.on('POST', '/', this.handlerPostRoot
.bind(this));
33 // Information page about service
34 this.on(['GET', 'HEAD'], '/', this.handlerGetRoot
.bind(this));
36 // Give load-balancers something to check
37 this.on(['GET', 'HEAD'], '/healthcheck', this.handlerGetHealthcheck
.bind(this));
39 // Public information about topics
40 this.on('GET', '/info', this.handlerGetInfo
.bind(this));
41 this.on('GET', '/info/', this.handlerGetInfo
.bind(this));
43 // These routes are intended for accessing static content during development.
44 // In production, a proxy server would likely handle these first.
45 this.on(['GET', 'HEAD'], '/static', this.handlerRedirect
.bind(this), `${options.dingus.proxyPrefix}/static/`);
46 this.on(['GET', 'HEAD'], '/static/', this.handlerGetStaticFile
.bind(this), 'index.html');
47 this.on(['GET', 'HEAD'], '/static/:file', this.handlerGetStaticFile
.bind(this));
48 this.on(['GET', 'HEAD'], '/favicon.ico', this.handlerGetStaticFile
.bind(this), 'favicon.ico');
49 this.on(['GET', 'HEAD'], '/robots.txt', this.handlerGetStaticFile
.bind(this), 'robots.txt');
51 // Private informational endpoints
52 this.on(['GET', 'HEAD'], '/admin', this.handlerRedirect
.bind(this), `${options.dingus.proxyPrefix}/admin/`);
53 this.on(['GET', 'HEAD'], '/admin/', this.handlerGetAdminOverview
.bind(this));
54 this.on(['GET', 'HEAD'], '/admin/topic/:topicId', this.handlerGetAdminTopicDetails
.bind(this));
56 // Private data-editing endpoints
57 this.on(['PATCH', 'DELETE'], '/admin/topic/:topicId', this.handlerUpdateTopic
.bind(this));
58 this.on(['PATCH', 'DELETE'], '/admin/subscription/:subscriptionId', this.handlerUpdateSubscription
.bind(this));
60 // Private server-action endpoints
61 this.on('POST', '/admin/process', this.handlerPostAdminProcess
.bind(this));
64 this.on(['GET', 'HEAD'], '/admin/login', this.handlerGetAdminLogin
.bind(this));
65 this.on(['POST'], '/admin/login', this.handlerPostAdminLogin
.bind(this));
66 this.on(['GET'], '/admin/logout', this.handlerGetAdminLogout
.bind(this));
67 this.on(['GET'], '/admin/_ia', this.handlerGetAdminIA
.bind(this));
72 * Wrap the Dingus head handler, to remove the response body from the context,
74 * @param {http.ClientRequest} req
75 * @param {http.ServerResponse} res
78 static setHeadHandler(req
, res
, ctx
) {
79 if (req
.method
=== 'HEAD') {
80 Dingus
.setHeadHandler(req
, res
, ctx
);
81 const origEnd
= res
.end
.bind(res
);
82 res
.end = function (data
, encoding
, ...rest
) {
83 const origResult
= origEnd(data
, encoding
, ...rest
);
84 delete ctx
.responseBody
;
91 * @param {http.ClientRequest} req
92 * @param {http.ServerResponse} res
95 async
handlerPostRoot(req
, res
, ctx
) {
96 const _scope
= _fileScope('handlerPostRoot');
97 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
99 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
100 await
this.ingestBody(req
, res
, ctx
);
102 await
this.manager
.postRoot(req
, res
, ctx
);
107 * @param {http.ClientRequest} req
108 * @param {http.ServerResponse} res
109 * @param {Object} ctx
111 async
handlerGetRoot(req
, res
, ctx
) {
112 const _scope
= _fileScope('handlerGetRoot');
113 const responseTypes
= [
114 Enum
.ContentType
.TextHTML
,
116 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
118 Service
.setHeadHandler(req
, res
, ctx
);
120 this.setResponseType(responseTypes
, req
, res
, ctx
);
122 await
this.authenticator
.sessionOptional(req
, res
, ctx
, this.loginPath
);
124 await
this.manager
.getRoot(req
, res
, ctx
);
129 * @param {http.ClientRequest} req
130 * @param {http.ServerResponse} res
131 * @param {Object} ctx
133 async
handlerGetHealthcheck(req
, res
, ctx
) {
134 const _scope
= _fileScope('handlerGetHealthcheck');
135 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
137 Service
.setHeadHandler(req
, res
, ctx
);
139 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
141 await
this.manager
.getHealthcheck(res
, ctx
);
146 * @param {http.ClientRequest} req
147 * @param {http.ServerResponse} res
148 * @param {Object} ctx
150 async
handlerGetInfo(req
, res
, ctx
) {
151 const _scope
= _fileScope('handlerGetInfo');
152 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
154 const responseTypes
= [...this.responseTypes
, Enum
.ContentType
.ImageSVG
];
156 Service
.setHeadHandler(req
, res
, ctx
);
158 this.setResponseType(responseTypes
, req
, res
, ctx
);
160 await
this.manager
.getInfo(res
, ctx
);
165 * @param {http.ClientRequest} req
166 * @param {http.ServerResponse} res
167 * @param {Object} ctx
169 async
handlerGetAdminOverview(req
, res
, ctx
) {
170 const _scope
= _fileScope('handlerGetAdminOverview');
171 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
173 Service
.setHeadHandler(req
, res
, ctx
);
175 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
177 await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
);
179 await
this.manager
.getAdminOverview(res
, ctx
);
184 * @param {http.ClientRequest} req
185 * @param {http.ServerResponse} res
186 * @param {Object} ctx
188 async
handlerGetAdminTopicDetails(req
, res
, ctx
) {
189 const _scope
= _fileScope('handlerGetAdminTopicDetails');
190 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
192 Service
.setHeadHandler(req
, res
, ctx
);
194 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
196 await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
);
198 await
this.manager
.getTopicDetails(res
, ctx
);
203 * Similar to super.ingestBody, but if no body was sent, do not parse (and
204 * thus avoid possible unsupported media type error).
205 * Also removes raw body from context, to simplify scrubbing sensitive data from logs.
206 * @param {http.ClientRequest} req
207 * @param {http.ServerResponse} res
208 * @param {Object} ctx
210 async
maybeIngestBody(req
, res
, ctx
) {
211 ctx
.rawBody
= await
this.bodyData(req
);
212 const contentType
= Dingus
.getRequestContentType(req
);
214 this.parseBody(contentType
, ctx
);
221 * @param {http.ClientRequest} req
222 * @param {http.ServerResponse} res
223 * @param {Object} ctx
225 async
handlerUpdateTopic(req
, res
, ctx
) {
226 const _scope
= _fileScope('handlerUpdateTopic');
227 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
229 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
231 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
233 await
this.maybeIngestBody(req
, res
, ctx
);
234 ctx
.method
= req
.method
;
235 await
this.manager
.updateTopic(res
, ctx
);
240 * @param {http.ClientRequest} req
241 * @param {http.ServerResponse} res
242 * @param {Object} ctx
244 async
handlerUpdateSubscription(req
, res
, ctx
) {
245 const _scope
= _fileScope('handlerUpdateSubscription');
246 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
248 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
250 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
252 await
this.maybeIngestBody(req
, res
, ctx
);
253 ctx
.method
= req
.method
;
254 await
this.manager
.updateSubscription(res
, ctx
);
259 * @param {http.ClientRequest} req
260 * @param {http.ServerResponse} res
261 * @param {Object} ctx
263 async
handlerPostAdminProcess(req
, res
, ctx
) {
264 const _scope
= _fileScope('handlerPostAdminProcess');
265 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
267 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
269 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
271 await
this.manager
.processTasks(res
, ctx
);
276 * Delegate login to authentication module.
277 * @param {http.ClientRequest} req
278 * @param {http.ServerResponse} res
279 * @param {Object} ctx
281 async
handlerGetAdminLogin(req
, res
, ctx
) {
282 const _scope
= _fileScope('handlerGetAdminLogin');
283 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
285 Service
.setHeadHandler(req
, res
, ctx
);
287 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
289 await
this.sessionManager
.getAdminLogin(res
, ctx
);
294 * Delegate login to authentication module.
295 * @param {http.ClientRequest} req
296 * @param {http.ServerResponse} res
297 * @param {Object} ctx
299 async
handlerPostAdminLogin(req
, res
, ctx
) {
300 const _scope
= _fileScope('handlerPostAdminLogin');
301 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
303 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
305 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
307 await
this.maybeIngestBody(req
, res
, ctx
);
309 await
this.sessionManager
.postAdminLogin(res
, ctx
);
314 * Delegate login to authentication module.
315 * @param {http.ClientRequest} req
316 * @param {http.ServerResponse} res
317 * @param {Object} ctx
319 async
handlerGetAdminLogout(req
, res
, ctx
) {
320 const _scope
= _fileScope('handlerGetAdminLogout');
321 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
323 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
325 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
327 await
this.sessionManager
.getAdminLogout(res
, ctx
);
332 * Delegate login to authentication module.
333 * @param {http.ClientRequest} req
334 * @param {http.ServerResponse} res
335 * @param {Object} ctx
337 async
handlerGetAdminIA(req
, res
, ctx
) {
338 const _scope
= _fileScope('handlerGetAdminIA');
339 this.logger
.debug(_scope
, 'called', { req: common
.requestLogData(req
), ctx
});
341 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
343 // Special case here, to see cookie before session established
344 ctx
.cookie
= req
.getHeader(Enum
.Header
.Cookie
);
346 await
this.sessionManager
.getAdminIA(res
, ctx
);
351 module
.exports
= Service
;