The DTP Sites web app development engine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
2.0 KiB

// home.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
'use strict';
const DTP_COMPONENT_NAME = 'home';
const path = require('path');
const express = require('express');
const { SiteController, SiteError } = require('../../lib/site-lib');
class HomeController extends SiteController {
constructor (dtp) {
super(dtp, DTP_COMPONENT_NAME);
}
async start ( ) {
const { dtp } = this;
const { limiter: limiterService } = dtp.services;
const router = express.Router();
dtp.app.use('/', router);
router.use(this.dtp.services.gabTV.channelMiddleware());
router.use(async (req, res, next) => {
res.locals.currentView = 'home';
return next();
});
router.param('policyDocument', this.populatePolicyDocument.bind(this));
router.get('/policy/:policyDocument', this.getPolicyDocument.bind(this));
router.get('/',
limiterService.create(limiterService.config.home.getHome),
this.getHome.bind(this),
);
}
async populatePolicyDocument (req, res, next, policyDocument) {
const { markdown: markdownService } = this.dtp.services;
try {
const basePath = path.join(this.dtp.config.root, 'app', 'views', 'policy');
switch (policyDocument) {
case 'terms-of-service':
res.locals.policyDocument = await markdownService.renderFile(path.join(basePath, 'terms-of-service.md'));
break;
case 'privacy':
res.locals.policyDocument = await markdownService.renderFile(path.join(basePath, 'privacy.md'));
break;
default:
throw new SiteError(404, 'Document not found');
}
return next();
} catch (error) {
return next(error);
}
}
async getPolicyDocument (req, res) {
res.render('policy/view');
}
async getHome (req, res) {
res.render('index');
}
}
module.exports = {
slug: 'home',
name: 'home',
isHome: true,
create: async (dtp) => {
let controller = new HomeController(dtp);
return controller;
},
};