283 lines
9.9 KiB
JavaScript
283 lines
9.9 KiB
JavaScript
const express = require('express');
|
|
const axios = require('axios');
|
|
const app = express();
|
|
const serve = require('./vercel-serve');
|
|
const path = require('path');
|
|
const glob = require('glob');
|
|
const matter = require('gray-matter');
|
|
const ejs = require('ejs');
|
|
const helpers = require('../views/helpers/functions');
|
|
const search = require('../routes/search');
|
|
const fs = require('fs');
|
|
const dotenv = require('dotenv');
|
|
const dotenvExpand = require('dotenv-expand');
|
|
dotenv.config();
|
|
dotenvExpand.expand(process.env);
|
|
// const advancedSearch = require('../routes/advanced-search');
|
|
|
|
// Port number for HTTP server
|
|
const port = process.env.PORT||3000;
|
|
|
|
// Set EJS as the view engine
|
|
app.set('view engine', 'ejs');
|
|
|
|
// Specify the views directory
|
|
app.set('views', path.join(__dirname, '..', 'views'));
|
|
|
|
// Middleware to parse JSON request body
|
|
app.use(express.json());
|
|
|
|
// Middleware to rewrite requests
|
|
//app.use(rewriter);
|
|
|
|
// // Serve static files (CSS, JavaScript, images, etc.)
|
|
// app.use(serve('../public', {
|
|
// dotfiles: 'ignore',
|
|
// index: false,
|
|
// }));
|
|
|
|
// app.get('/', (req, res) => {
|
|
// res.send('Hello World!');
|
|
// })
|
|
|
|
console.log("Setting route for /ads.txt");
|
|
app.get('/ads.txt', (req, res) => {
|
|
res.setHeader("Content-Type", "text/plain");
|
|
res.setHeader("Cache-Control", "no-cache");
|
|
res.send(`google.com, pub-8937572456576531, DIRECT, f08c47fec0942fa0`);
|
|
});
|
|
|
|
console.log("Setting route for /robots.txt");
|
|
app.get('/robots.txt', (req, res) => {
|
|
res.setHeader("Content-Type", "text/plain");
|
|
res.setHeader("Cache-Control", "no-cache");
|
|
// TODO: Implement Site Map feature and provide sitemap url in robots.txt
|
|
res.send(
|
|
`User-agent: *
|
|
Allow: /
|
|
|
|
# TODO: Implement Site Map feature and provide sitemap url in robots.txt
|
|
#sitemap: https://no-moss-3-carbo-landfill-library.online/sitemap.xml`
|
|
);//end of res.send() for robots.txt
|
|
});
|
|
|
|
// Search endpoints
|
|
console.log("Setting routes for /search");
|
|
app.use('/search', search.router);
|
|
// app.use('/advanced-search', advancedSearch.router);
|
|
|
|
// Endpoints for all the site's pages.
|
|
console.log("Scanning for pages to create routes");
|
|
glob.globSync('pages/**/*.md', {
|
|
cwd: path.join(__dirname, '..'),
|
|
matchBase: true,
|
|
follow: true,
|
|
}).forEach((filePath) => {
|
|
const expressRoutePathFromFilePath = (filePath) => {
|
|
return filePath.substring('pages'.length, filePath.length - path.extname(filePath).length).replaceAll(path.sep, path.posix.sep);
|
|
};
|
|
const route = expressRoutePathFromFilePath(filePath);
|
|
const fullFilePath = path.join(__dirname, '..', filePath);
|
|
let paths = route.split(path.posix.sep);
|
|
paths[0] = 'public';
|
|
console.log(`Setting route for ${route}`);
|
|
app.get(route, async (req, res) => {
|
|
const fm = matter.read(fullFilePath);
|
|
const fmData = { fm: fm.data, excerpt: fm.excerpt };
|
|
const content = helpers.md.render(fm.content, fmData );
|
|
const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
|
|
res.render("page", { h: helpers, ...renderData });
|
|
});
|
|
});
|
|
|
|
// console.log("Scanning for documents to create routes");
|
|
// glob.globSync('**/*{.pdf,.docx,.xlsx,.pptx,.doc,.xls,.ppt}', {
|
|
// cwd: path.join(__dirname, '..', 'public'),
|
|
// matchBase: true,
|
|
// follow: true,
|
|
// }).forEach((filePath) => {
|
|
// const expressRoutePathFromFilePath = (filePath) => {
|
|
// return filePath.substring(0, filePath.length - path.extname(filePath).length).replaceAll(path.sep, path.posix.sep);
|
|
// };
|
|
// const route = expressRoutePathFromFilePath(filePath);
|
|
// const fullFilePath = path.join(__dirname, '..', 'public', filePath);
|
|
// let paths = route.split(path.posix.sep);
|
|
// paths[0] = 'public';
|
|
// console.log(`Setting route for ${route}`);
|
|
// app.get(route, async (req, res) => {
|
|
// const fm = matter.read(fullFilePath);
|
|
// const fmData = { fm: fm.data, excerpt: fm.excerpt };
|
|
// const content = helpers.md.render(fm.content, fmData );
|
|
// const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
|
|
// res.render("page", { h: helpers, ...renderData });
|
|
// });
|
|
// });
|
|
|
|
console.log("Scanning for web archive HTML documents to create routes");
|
|
glob.globSync('Web_Site_Archives/**/*{.htm,.html}', {
|
|
cwd: path.join(__dirname, '..', 'public'),
|
|
matchBase: true,
|
|
follow: true,
|
|
}).forEach((filePath) => {
|
|
const expressRoutePathFromFilePath = (filePath) => {
|
|
return '/' + filePath.replaceAll(path.sep, path.posix.sep);
|
|
};
|
|
const route = expressRoutePathFromFilePath(filePath);
|
|
const fullFilePath = path.join(__dirname, '..', 'public', filePath);
|
|
let paths = route.split(path.posix.sep);
|
|
paths[0] = 'public';
|
|
console.log(`Setting route for ${route}`);
|
|
app.get(route, async (req, res) => {
|
|
const html = fs.readFileSync(fullFilePath).toString();
|
|
const renderData = { route, filePath, fullFilePath, req, paths, html };
|
|
res.render("archive", { h: helpers, ...renderData });
|
|
});
|
|
});
|
|
|
|
|
|
// Endpoints for all the site's YouTube videos.
|
|
console.log("Scanning for archived videos to create routes");
|
|
glob.globSync(['Russell_County/Board_of_Supervisors/YouTube_Archive/**/*.info.json', 'Virginia_Energy/YouTube_Archive/**/*.info.json', 'Virginia_Governor/**/*.info.json'], {
|
|
cwd: path.join(__dirname, '..', 'public'),
|
|
matchBase: true,
|
|
follow: true,
|
|
}).forEach((filePath) => {
|
|
const expressRoutePathFromFilePath = (filePath) => {
|
|
return path.posix.sep+filePath.substring(0, filePath.lastIndexOf(path.sep)).replaceAll(path.sep, path.posix.sep);
|
|
};
|
|
const dirFromFilePath = (filePath) => {
|
|
return filePath.substring(0, filePath.lastIndexOf(path.sep));
|
|
}
|
|
const directory = dirFromFilePath(filePath);
|
|
let videoURL = ""+glob.globSync("*.{mpg,mpeg,mp4,mkv,webm}", {
|
|
cwd: path.join(__dirname, '..', 'public', directory),
|
|
matchBase: true,
|
|
follow: true,
|
|
}).pop();
|
|
let subtitleURL = ""+glob.globSync("*.en.vtt", {
|
|
cwd: path.join(__dirname, '..', 'public', directory),
|
|
matchBase: true,
|
|
follow: true,
|
|
}).pop();
|
|
let subtitleFile = path.join(__dirname, '..', 'public', directory, subtitleURL);
|
|
const route = encodeURI(expressRoutePathFromFilePath(filePath));
|
|
let paths = filePath.substring(0, filePath.lastIndexOf(path.sep) > 0 ? filePath.lastIndexOf(path.sep) : filePath.length-1).split(path.sep);
|
|
paths = paths.map((name, idx, aPaths) => {
|
|
let url = aPaths.slice(0, idx+1).join(path.posix.sep);
|
|
return {
|
|
name,
|
|
url,
|
|
};
|
|
});
|
|
const fullFilePath = path.join(__dirname, '..', 'public', filePath);
|
|
console.log(`Setting route for ${route}`);
|
|
app.get(route, async (req, res) => {
|
|
if (!req.path.endsWith('/')) {
|
|
res.redirect(req.path + '/');
|
|
}
|
|
else {
|
|
let info = require(fullFilePath);
|
|
let subtitleVTT = fs.existsSync(subtitleFile)?fs.readFileSync(subtitleFile, 'utf8'):undefined;
|
|
const renderData = { route, filePath, fullFilePath, req, paths, directory: path.join('public', directory), videoURL, subtitleURL, subtitleVTT, info };
|
|
res.render("video-player", { h: helpers, require, ...renderData });
|
|
}
|
|
});
|
|
});
|
|
|
|
//app.get('/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding**', rewriter.rewrite('/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding/$1'));
|
|
|
|
console.log(`Setting routes for /vendor/**/*`);;
|
|
app.get('/vendor/**/*', async (req, res) => {
|
|
await serve(req, res, {
|
|
public: path.join(__dirname, '..', 'static'),
|
|
symlinks: true,
|
|
trailingSlash: true,
|
|
cleanUrls: false,
|
|
renderSingle: false,
|
|
unlisted: [
|
|
".DS_Store",
|
|
".git",
|
|
"Thumbs.db",
|
|
"README*",
|
|
],
|
|
});
|
|
});
|
|
|
|
console.log(`Setting routes for /css/*.css`);;
|
|
app.get('/css/*.css', async (req, res) => {
|
|
await serve(req, res, {
|
|
public: path.join(__dirname, '..', 'static'),
|
|
symlinks: true,
|
|
trailingSlash: true,
|
|
cleanUrls: false,
|
|
renderSingle: false,
|
|
unlisted: [
|
|
".DS_Store",
|
|
".git",
|
|
"Thumbs.db",
|
|
"README*",
|
|
],
|
|
});
|
|
});
|
|
|
|
console.log(`Setting routes for /svg/*.svg`);;
|
|
app.get('/svg/*.svg', async (req, res) => {
|
|
await serve(req, res, {
|
|
public: path.join(__dirname, '..', 'static'),
|
|
symlinks: true,
|
|
trailingSlash: true,
|
|
cleanUrls: false,
|
|
renderSingle: false,
|
|
unlisted: [
|
|
".DS_Store",
|
|
".git",
|
|
"Thumbs.db",
|
|
"README*",
|
|
],
|
|
});
|
|
});
|
|
|
|
console.log(`Setting route for *`);
|
|
app.get('*', async (req, res) => {
|
|
await serve(req, res, {
|
|
public: path.join(__dirname, '..', 'public'),
|
|
symlinks: true,
|
|
trailingSlash: true,
|
|
cleanUrls: false,
|
|
renderSingle: false,
|
|
unlisted: [
|
|
".DS_Store",
|
|
".git",
|
|
"Thumbs.db",
|
|
"README*",
|
|
],
|
|
redirects: [
|
|
{
|
|
source: "/:year(\d{4})-:mo(\d{2})-:dd(\d{2})_:hh(\d{2})_:mm(\d{2})/",
|
|
destination: "/Web_Site_Archives/Russell_County_Web_Site-:year-:mo-:dd_:hh_:mm/"
|
|
},
|
|
{
|
|
source: "/OCR-Encoded-PDFs",
|
|
destination: "/Web_Site_Archives"
|
|
},
|
|
{
|
|
source: "/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding.zip",
|
|
destination: "/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding.zip"
|
|
},
|
|
{
|
|
source: "/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding/:u(.*)",
|
|
destination: "/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding:u"
|
|
},
|
|
{ source: '/YouTube Channel', destination: '/Russell_County/Board_of_Supervisors/YouTube_Archive/@russellcountyvirginia8228' },
|
|
// { source: '/YouTube Channel.zip', destination: '/Russell_County_BOS/YouTube_Channel.zip' },
|
|
// { source: '/YouTube Channel/:u?', destination: '/Russell_County_BOS/YouTube_Channel/:u' },
|
|
{ source: '/Project Reclaim [WI19KR9Ogwg].mkv', destination: '/YouTube_Archives/@VADMME/Project Reclaim [WI19KR9Ogwg].mkv' },
|
|
]
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
app.listen(port, () => {
|
|
console.log(`no-moss-3-carbo-landfill-library.online app listening on port ${port}`);
|
|
});
|