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
, asyncLocalStorage
) {
21 ignoreTrailingSlash: false,
23 this.asyncLocalStorage
= asyncLocalStorage
;
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));
55 this.on(['GET', 'HEAD'], '/admin/topic/:topicId/history.svg', this.handlerGetHistorySVG
.bind(this));
57 // Private data-editing endpoints
58 this.on(['PATCH', 'DELETE'], '/admin/topic/:topicId', this.handlerUpdateTopic
.bind(this));
59 this.on(['PATCH', 'DELETE'], '/admin/subscription/:subscriptionId', this.handlerUpdateSubscription
.bind(this));
61 // Private server-action endpoints
62 this.on('POST', '/admin/process', this.handlerPostAdminProcess
.bind(this));
65 this.on(['GET', 'HEAD'], '/admin/login', this.handlerGetAdminLogin
.bind(this));
66 this.on(['POST'], '/admin/login', this.handlerPostAdminLogin
.bind(this));
67 this.on(['GET'], '/admin/logout', this.handlerGetAdminLogout
.bind(this));
68 this.on(['GET'], '/admin/_ia', this.handlerGetAdminIA
.bind(this));
74 * Rearrange logging data.
75 * @param {http.ClientRequest} req
76 * @param {http.ServerResponse} res
79 async
preHandler(req
, res
, ctx
) {
80 await
super.preHandler(req
, res
, ctx
);
81 const logObject
= this.asyncLocalStorage
.getStore();
82 // FIXME: for some reason, returning from the super.preHandler loses async context?
83 // Workaround until cause and solution are found.
85 logObject
.requestId
= ctx
.requestId
;
92 * @param {http.ClientRequest} req
93 * @param {http.ServerResponse} res
96 async
handlerPostRoot(req
, res
, ctx
) {
97 const _scope
= _fileScope('handlerPostRoot');
98 this.logger
.debug(_scope
, 'called', { req
, ctx
});
100 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
101 await
this.ingestBody(req
, res
, ctx
);
103 await
this.manager
.postRoot(req
, res
, ctx
);
108 * @param {http.ClientRequest} req
109 * @param {http.ServerResponse} res
110 * @param {Object} ctx
112 async
handlerGetRoot(req
, res
, ctx
) {
113 const _scope
= _fileScope('handlerGetRoot');
114 const responseTypes
= [
115 Enum
.ContentType
.TextHTML
,
117 this.logger
.debug(_scope
, 'called', { req
, ctx
});
119 Dingus
.setHeadHandler(req
, res
, ctx
);
121 this.setResponseType(responseTypes
, req
, res
, ctx
);
123 await
this.authenticator
.sessionOptional(req
, res
, ctx
, this.loginPath
);
125 await
this.manager
.getRoot(req
, res
, ctx
);
130 * @param {http.ClientRequest} req
131 * @param {http.ServerResponse} res
132 * @param {Object} ctx
134 async
handlerGetHealthcheck(req
, res
, ctx
) {
135 const _scope
= _fileScope('handlerGetHealthcheck');
136 this.logger
.debug(_scope
, 'called', { req
, ctx
});
138 Dingus
.setHeadHandler(req
, res
, ctx
);
140 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
142 await
this.manager
.getHealthcheck(res
, ctx
);
147 * @param {http.ClientRequest} req
148 * @param {http.ServerResponse} res
149 * @param {Object} ctx
151 async
handlerGetInfo(req
, res
, ctx
) {
152 const _scope
= _fileScope('handlerGetInfo');
153 this.logger
.debug(_scope
, 'called', { req
, ctx
});
155 const responseTypes
= [...this.responseTypes
, Enum
.ContentType
.ImageSVG
];
157 Dingus
.setHeadHandler(req
, res
, ctx
);
159 this.setResponseType(responseTypes
, req
, res
, ctx
);
161 await
this.manager
.getInfo(res
, ctx
);
165 async
handlerGetHistorySVG(req
, res
, ctx
) {
166 const _scope
= _fileScope('handlerGetHist');
167 this.logger
.debug(_scope
, 'called', { req
, ctx
});
169 const responseTypes
= [Enum
.ContentType
.ImageSVG
];
171 Dingus
.setHeadHandler(req
, res
, ctx
);
173 this.setResponseType(responseTypes
, req
, res
, ctx
);
175 await
this.manager
.getHistorySVG(res
, ctx
);
180 * @param {http.ClientRequest} req
181 * @param {http.ServerResponse} res
182 * @param {Object} ctx
184 async
handlerGetAdminOverview(req
, res
, ctx
) {
185 const _scope
= _fileScope('handlerGetAdminOverview');
186 this.logger
.debug(_scope
, 'called', { req
, ctx
});
188 Dingus
.setHeadHandler(req
, res
, ctx
);
190 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
192 if (await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
)) {
193 await
this.manager
.getAdminOverview(res
, ctx
);
199 * @param {http.ClientRequest} req
200 * @param {http.ServerResponse} res
201 * @param {Object} ctx
203 async
handlerGetAdminTopicDetails(req
, res
, ctx
) {
204 const _scope
= _fileScope('handlerGetAdminTopicDetails');
205 this.logger
.debug(_scope
, 'called', { req
, ctx
});
207 Dingus
.setHeadHandler(req
, res
, ctx
);
209 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
211 if (await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
)) {
212 await
this.manager
.getTopicDetails(res
, ctx
);
218 * If no body was sent, do not parse (and thus avoid possible unsupported media type error).
219 * @param {http.ClientRequest} req
220 * @param {http.ServerResponse} res
221 * @param {Object} ctx
223 async
maybeIngestBody(req
, res
, ctx
) {
224 return super.ingestBody(req
, res
, ctx
, {
225 parseEmptyBody: false,
231 * @param {http.ClientRequest} req
232 * @param {http.ServerResponse} res
233 * @param {Object} ctx
235 async
handlerUpdateTopic(req
, res
, ctx
) {
236 const _scope
= _fileScope('handlerUpdateTopic');
237 this.logger
.debug(_scope
, 'called', { req
, ctx
});
239 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
241 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
243 await
this.maybeIngestBody(req
, res
, ctx
);
244 ctx
.method
= req
.method
;
245 await
this.manager
.updateTopic(res
, ctx
);
250 * @param {http.ClientRequest} req
251 * @param {http.ServerResponse} res
252 * @param {Object} ctx
254 async
handlerUpdateSubscription(req
, res
, ctx
) {
255 const _scope
= _fileScope('handlerUpdateSubscription');
256 this.logger
.debug(_scope
, 'called', { req
, ctx
});
258 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
260 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
262 await
this.maybeIngestBody(req
, res
, ctx
);
263 ctx
.method
= req
.method
;
264 await
this.manager
.updateSubscription(res
, ctx
);
269 * @param {http.ClientRequest} req
270 * @param {http.ServerResponse} res
271 * @param {Object} ctx
273 async
handlerPostAdminProcess(req
, res
, ctx
) {
274 const _scope
= _fileScope('handlerPostAdminProcess');
275 this.logger
.debug(_scope
, 'called', { req
, ctx
});
277 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
279 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
281 await
this.manager
.processTasks(res
, ctx
);
286 * Delegate login to authentication module.
287 * @param {http.ClientRequest} req
288 * @param {http.ServerResponse} res
289 * @param {Object} ctx
291 async
handlerGetAdminLogin(req
, res
, ctx
) {
292 const _scope
= _fileScope('handlerGetAdminLogin');
293 this.logger
.debug(_scope
, 'called', { req
, ctx
});
295 Dingus
.setHeadHandler(req
, res
, ctx
);
297 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
299 await
this.sessionManager
.getAdminLogin(res
, ctx
);
304 * Delegate login to authentication module.
305 * @param {http.ClientRequest} req
306 * @param {http.ServerResponse} res
307 * @param {Object} ctx
309 async
handlerPostAdminLogin(req
, res
, ctx
) {
310 const _scope
= _fileScope('handlerPostAdminLogin');
311 this.logger
.debug(_scope
, 'called', { req
, ctx
});
313 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
315 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
317 await
this.maybeIngestBody(req
, res
, ctx
);
319 await
this.sessionManager
.postAdminLogin(res
, ctx
);
324 * Delegate login to authentication module.
325 * @param {http.ClientRequest} req
326 * @param {http.ServerResponse} res
327 * @param {Object} ctx
329 async
handlerGetAdminLogout(req
, res
, ctx
) {
330 const _scope
= _fileScope('handlerGetAdminLogout');
331 this.logger
.debug(_scope
, 'called', { req
, ctx
});
333 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
335 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
337 await
this.sessionManager
.getAdminLogout(res
, ctx
);
342 * Delegate login to authentication module.
343 * @param {http.ClientRequest} req
344 * @param {http.ServerResponse} res
345 * @param {Object} ctx
347 async
handlerGetAdminIA(req
, res
, ctx
) {
348 const _scope
= _fileScope('handlerGetAdminIA');
349 this.logger
.debug(_scope
, 'called', { req
, ctx
});
351 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
353 // Special case here, to see cookie before session established
354 ctx
.cookie
= req
.getHeader(Enum
.Header
.Cookie
);
356 await
this.sessionManager
.getAdminIA(res
, ctx
);
361 module
.exports
= Service
;