add dynamic seo settings

This commit is contained in:
2025-10-13 18:40:05 +02:00
parent 0d826e8fe2
commit 78af7b2906
4 changed files with 127 additions and 54 deletions

View File

@@ -4,6 +4,7 @@ const path = require('path');
const express = require('express');
const helmet = require('helmet');
const winston = require('winston');
const ejs = require('ejs');
const DocumentHandler = require('./lib/document_handler.js');
const FileStorage = require('./lib/file_storage.js');
@@ -37,25 +38,6 @@ logger.info('Welcome to unknownBIN!');
// init file-storage
const fileStorage = new FileStorage({ path: config.dataPath, logger: logger });
// load static documents into file-storage
if (config.documents) {
for (const name in config.documents) {
const docPath = config.documents[name];
try {
const data = fs.readFileSync(docPath, 'utf8');
fileStorage.set(name, data, (success) => {
if (success) {
logger.verbose(`Loaded document: ${name} from ${docPath}`);
} else {
logger.warn(`Failed to store document: ${name}`);
}
});
} catch (err) {
logger.warn(`Unable to find or read document: ${name} at ${docPath}`);
}
}
}
// configure the document handler
const documentHandler = new DocumentHandler({
store: fileStorage,
@@ -68,9 +50,14 @@ const documentHandler = new DocumentHandler({
// setup routes and request-handling
const app = express();
// Configure EJS as the view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Use helmet for basic security headers
app.use(helmet());
// API routes
app.get('/raw/:id', (req, res) => {
return documentHandler.handleRawGet(req.params.id, res);
});
@@ -81,17 +68,47 @@ app.get('/documents/:id', (req, res) => {
return documentHandler.handleGet(req.params.id, res);
});
// Static files
app.use(express.static(path.join(__dirname, 'static')));
app.get('/:id', (req, res, next) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
// Frontend routes (SSR)
app.get('/:id', (req, res) => {
const key = req.params.id;
// Don't treat static files as paste IDs
if (key.includes('.')) {
return res.status(404).send('Not Found');
}
fileStorage.get(key, (data) => {
if (data) {
// Paste found, render it with dynamic SEO tags
const title = `Paste ${key} - unknownBIN`;
const description = data.substring(0, 160).replace(/\n/g, ' ');
res.render('index', {
paste: {
key: key,
data: data,
title: title,
description: description
}
});
} else {
// Paste not found, render the homepage
res.render('index', {
paste: null
});
}
});
});
app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
app.get('/', (req, res) => {
// Render the homepage
res.render('index', {
paste: null
});
});
app.listen(config.port, config.host, () => {
logger.info(`Listening on ${config.host}:${config.port}`);
});