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 sometimes loses async context?
83 // Workaround until cause and solution are found.
85 logObject
.requestId
= ctx
.requestId
;
88 this.logger
.debug(_fileScope('preHandler'), 'lost async context', { req
, ctx
});
94 * @param {http.ClientRequest} req
95 * @param {http.ServerResponse} res
98 async
handlerPostRoot(req
, res
, ctx
) {
99 const _scope
= _fileScope('handlerPostRoot');
100 this.logger
.debug(_scope
, 'called', { req
, ctx
});
102 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
103 await
this.ingestBody(req
, res
, ctx
);
105 await
this.manager
.postRoot(req
, res
, ctx
);
110 * @param {http.ClientRequest} req
111 * @param {http.ServerResponse} res
112 * @param {Object} ctx
114 async
handlerGetRoot(req
, res
, ctx
) {
115 const _scope
= _fileScope('handlerGetRoot');
116 const responseTypes
= [
117 Enum
.ContentType
.TextHTML
,
119 this.logger
.debug(_scope
, 'called', { req
, ctx
});
121 Dingus
.setHeadHandler(req
, res
, ctx
);
123 this.setResponseType(responseTypes
, req
, res
, ctx
);
125 await
this.authenticator
.sessionOptional(req
, res
, ctx
, this.loginPath
);
127 await
this.manager
.getRoot(req
, res
, ctx
);
132 * @param {http.ClientRequest} req
133 * @param {http.ServerResponse} res
134 * @param {Object} ctx
136 async
handlerGetHealthcheck(req
, res
, ctx
) {
137 const _scope
= _fileScope('handlerGetHealthcheck');
138 this.logger
.debug(_scope
, 'called', { req
, ctx
});
140 Dingus
.setHeadHandler(req
, res
, ctx
);
142 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
144 await
this.manager
.getHealthcheck(res
, ctx
);
149 * @param {http.ClientRequest} req
150 * @param {http.ServerResponse} res
151 * @param {Object} ctx
153 async
handlerGetInfo(req
, res
, ctx
) {
154 const _scope
= _fileScope('handlerGetInfo');
155 this.logger
.debug(_scope
, 'called', { req
, ctx
});
157 const responseTypes
= [...this.responseTypes
, Enum
.ContentType
.ImageSVG
];
159 Dingus
.setHeadHandler(req
, res
, ctx
);
161 this.setResponseType(responseTypes
, req
, res
, ctx
);
163 await
this.manager
.getInfo(res
, ctx
);
167 async
handlerGetHistorySVG(req
, res
, ctx
) {
168 const _scope
= _fileScope('handlerGetHist');
169 this.logger
.debug(_scope
, 'called', { req
, ctx
});
171 const responseTypes
= [Enum
.ContentType
.ImageSVG
];
173 Dingus
.setHeadHandler(req
, res
, ctx
);
175 this.setResponseType(responseTypes
, req
, res
, ctx
);
177 await
this.manager
.getHistorySVG(res
, ctx
);
182 * @param {http.ClientRequest} req
183 * @param {http.ServerResponse} res
184 * @param {Object} ctx
186 async
handlerGetAdminOverview(req
, res
, ctx
) {
187 const _scope
= _fileScope('handlerGetAdminOverview');
188 this.logger
.debug(_scope
, 'called', { req
, ctx
});
190 Dingus
.setHeadHandler(req
, res
, ctx
);
192 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
194 if (await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
)) {
195 await
this.manager
.getAdminOverview(res
, ctx
);
201 * @param {http.ClientRequest} req
202 * @param {http.ServerResponse} res
203 * @param {Object} ctx
205 async
handlerGetAdminTopicDetails(req
, res
, ctx
) {
206 const _scope
= _fileScope('handlerGetAdminTopicDetails');
207 this.logger
.debug(_scope
, 'called', { req
, ctx
});
209 Dingus
.setHeadHandler(req
, res
, ctx
);
211 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
213 if (await
this.authenticator
.sessionRequired(req
, res
, ctx
, this.loginPath
)) {
214 await
this.manager
.getTopicDetails(res
, ctx
);
220 * If no body was sent, do not parse (and thus avoid possible unsupported media type error).
221 * @param {http.ClientRequest} req
222 * @param {http.ServerResponse} res
223 * @param {Object} ctx
225 async
maybeIngestBody(req
, res
, ctx
) {
226 return super.ingestBody(req
, res
, ctx
, {
227 parseEmptyBody: false,
233 * @param {http.ClientRequest} req
234 * @param {http.ServerResponse} res
235 * @param {Object} ctx
237 async
handlerUpdateTopic(req
, res
, ctx
) {
238 const _scope
= _fileScope('handlerUpdateTopic');
239 this.logger
.debug(_scope
, 'called', { req
, ctx
});
241 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
243 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
245 await
this.maybeIngestBody(req
, res
, ctx
);
246 ctx
.method
= req
.method
;
247 await
this.manager
.updateTopic(res
, ctx
);
252 * @param {http.ClientRequest} req
253 * @param {http.ServerResponse} res
254 * @param {Object} ctx
256 async
handlerUpdateSubscription(req
, res
, ctx
) {
257 const _scope
= _fileScope('handlerUpdateSubscription');
258 this.logger
.debug(_scope
, 'called', { req
, ctx
});
260 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
262 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
264 await
this.maybeIngestBody(req
, res
, ctx
);
265 ctx
.method
= req
.method
;
266 await
this.manager
.updateSubscription(res
, ctx
);
271 * @param {http.ClientRequest} req
272 * @param {http.ServerResponse} res
273 * @param {Object} ctx
275 async
handlerPostAdminProcess(req
, res
, ctx
) {
276 const _scope
= _fileScope('handlerPostAdminProcess');
277 this.logger
.debug(_scope
, 'called', { req
, ctx
});
279 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
281 await
this.authenticator
.apiRequiredLocal(req
, res
, ctx
);
283 await
this.manager
.processTasks(res
, ctx
);
288 * Delegate login to authentication module.
289 * @param {http.ClientRequest} req
290 * @param {http.ServerResponse} res
291 * @param {Object} ctx
293 async
handlerGetAdminLogin(req
, res
, ctx
) {
294 const _scope
= _fileScope('handlerGetAdminLogin');
295 this.logger
.debug(_scope
, 'called', { req
, ctx
});
297 Dingus
.setHeadHandler(req
, res
, ctx
);
299 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
301 await
this.sessionManager
.getAdminLogin(res
, ctx
);
306 * Delegate login to authentication module.
307 * @param {http.ClientRequest} req
308 * @param {http.ServerResponse} res
309 * @param {Object} ctx
311 async
handlerPostAdminLogin(req
, res
, ctx
) {
312 const _scope
= _fileScope('handlerPostAdminLogin');
313 this.logger
.debug(_scope
, 'called', { req
, ctx
});
315 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
317 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
319 await
this.maybeIngestBody(req
, res
, ctx
);
321 await
this.sessionManager
.postAdminLogin(res
, ctx
);
326 * Delegate login to authentication module.
327 * @param {http.ClientRequest} req
328 * @param {http.ServerResponse} res
329 * @param {Object} ctx
331 async
handlerGetAdminLogout(req
, res
, ctx
) {
332 const _scope
= _fileScope('handlerGetAdminLogout');
333 this.logger
.debug(_scope
, 'called', { req
, ctx
});
335 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
337 await
this.authenticator
.sessionOptionalLocal(req
, res
, ctx
);
339 await
this.sessionManager
.getAdminLogout(res
, ctx
);
344 * Delegate login to authentication module.
345 * @param {http.ClientRequest} req
346 * @param {http.ServerResponse} res
347 * @param {Object} ctx
349 async
handlerGetAdminIA(req
, res
, ctx
) {
350 const _scope
= _fileScope('handlerGetAdminIA');
351 this.logger
.debug(_scope
, 'called', { req
, ctx
});
353 this.setResponseType(this.responseTypes
, req
, res
, ctx
);
355 // Special case here, to see cookie before session established
356 ctx
.cookie
= req
.getHeader(Enum
.Header
.Cookie
);
358 await
this.sessionManager
.getAdminIA(res
, ctx
);
363 module
.exports
= Service
;