add some fancy magic :kekw:

This commit is contained in:
2025-10-13 18:21:23 +02:00
parent 2e59c1f5e7
commit 22badaf535
15 changed files with 1328 additions and 786 deletions

134
server.js
View File

@@ -1,81 +1,97 @@
var http = require('http');
var url = require('url');
var fs = require('fs');
const fs = require('fs');
const path = require('path');
var express = require('express');
var logger = require('winston');
const express = require('express');
const helmet = require('helmet');
const winston = require('winston');
var DocumentHandler = require('./lib/document_handler.js');
var FileStorage = require('./lib/file_storage.js');
const DocumentHandler = require('./lib/document_handler.js');
const FileStorage = require('./lib/file_storage.js');
// load configuration
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
let config;
try {
config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
} catch (err) {
console.error('Error reading config.json:', err);
process.exit(1);
}
config.port = process.env.PORT || config.port || 8080;
config.host = process.env.HOST || config.host || 'localhost';
config.host = process.env.HOST || config.host || '0.0.0.0';
// logger-setup
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {colorize: true, level: 'verbose'});
logger.info('Welcome to Hastebin Plus!');
const logger = winston.createLogger({
level: 'verbose',
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
transports: [
new winston.transports.Console()
]
});
logger.info('Welcome to unknownBIN!');
// init file-storage
var fileStorage = new FileStorage(config.dataPath);
const fileStorage = new FileStorage({ path: config.dataPath, logger: logger });
// load static documents into file-storage
for (var name in config.documents) {
var path = config.documents[name];
var data = fs.readFileSync(path, 'utf8');
if (data) {
fileStorage.set(name, data, function(success) {});
logger.verbose('Created document: ' + name + " ==> " + path);
} else {
logger.warn('Unable to find document: ' + name + " ==> " + path);
}
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
var documentHandler = new DocumentHandler({
store: fileStorage,
maxLength: config.maxLength,
keyLength: config.keyLength,
createKey: config.createKey
const documentHandler = new DocumentHandler({
store: fileStorage,
maxLength: config.maxLength,
keyLength: config.keyLength,
createKey: config.createKey,
logger: logger
});
// compress static assets
var cssCompressor = require('clean-css');
var jsCompressor = require('uglify-js');
var files = fs.readdirSync(__dirname + '/static');
for (var i = 0; i < files.length; i++) {
var item = files[i];
var dest = "";
if ((item.indexOf('.css') === item.length - 4) && (item.indexOf('.min.css') === -1)) {
dest = item.substring(0, item.length - 4) + '.min.css';
fs.writeFileSync(__dirname + '/static/' + dest, new cssCompressor().minify(fs.readFileSync(__dirname + '/static/' + item, 'utf8')).styles, 'utf8');
logger.verbose('Compressed: ' + item + ' ==> ' + dest);
} else if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
dest = item.substring(0, item.length - 3) + '.min.js';
fs.writeFileSync(__dirname + '/static/' + dest, jsCompressor.minify(__dirname + '/static/' + item).code, 'utf8');
logger.verbose('Compressed: ' + item + ' ==> ' + dest);
}
}
// setup routes and request-handling
var app = express();
const app = express();
app.get('/raw/:id', function(req, res) {
return documentHandler.handleRawGet(req.params.id, res);
// Use helmet for basic security headers
app.use(helmet());
app.get('/raw/:id', (req, res) => {
return documentHandler.handleRawGet(req.params.id, res);
});
app.post('/documents', function(req, res) {
return documentHandler.handlePost(req, res);
app.post('/documents', (req, res) => {
return documentHandler.handlePost(req, res);
});
app.get('/documents/:id', function(req, res) {
return documentHandler.handleGet(req.params.id, res);
});
app.use(express.static('static'));
app.get('/:id', function(req, res, next) {
res.sendFile(__dirname + '/static/index.html');
app.get('/documents/:id', (req, res) => {
return documentHandler.handleGet(req.params.id, res);
});
app.listen(config.port, config.host);
logger.info('Listening on ' + config.host + ':' + config.port);
app.use(express.static(path.join(__dirname, 'static')));
app.get('/:id', (req, res, next) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
});
app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
});
app.listen(config.port, config.host, () => {
logger.info(`Listening on ${config.host}:${config.port}`);
});