index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const RootPage = require('./RootPage');
  2. const AuthorPage = require('./AuthorPage');
  3. const BookPage = require('./BookPage');
  4. module.exports = function(app, config) {
  5. const opdsRoot = '/opds';
  6. config.opdsRoot = opdsRoot;
  7. const root = new RootPage(config);
  8. const author = new AuthorPage(config);
  9. const book = new BookPage(config);
  10. const routes = [
  11. ['', root],
  12. ['/root', root],
  13. ['/author', author],
  14. ['/book', book],
  15. ];
  16. const pages = new Map();
  17. for (const r of routes) {
  18. pages.set(`${opdsRoot}${r[0]}`, r[1]);
  19. }
  20. const opds = async(req, res, next) => {
  21. try {
  22. const page = pages.get(req.path);
  23. if (page) {
  24. res.set('Content-Type', 'application/atom+xml; charset=utf-8');
  25. const result = await page.body(req, res);
  26. if (result !== false)
  27. res.send(result);
  28. } else {
  29. next();
  30. }
  31. } catch (e) {
  32. res.status(500).send({error: e.message});
  33. if (config.branch == 'development') {
  34. console.error({error: e.message, url: req.originalUrl});
  35. }
  36. }
  37. };
  38. app.get([opdsRoot, `${opdsRoot}/*`], opds);
  39. };