mirror of
https://github.com/MrUnknownDE/unknownbin.git
synced 2026-04-06 00:32:08 +02:00
97 lines
2.6 KiB
JavaScript
97 lines
2.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const express = require('express');
|
|
const helmet = require('helmet');
|
|
const winston = require('winston');
|
|
|
|
const DocumentHandler = require('./lib/document_handler.js');
|
|
const FileStorage = require('./lib/file_storage.js');
|
|
|
|
// load configuration
|
|
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 || '0.0.0.0';
|
|
|
|
// logger-setup
|
|
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
|
|
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,
|
|
maxLength: config.maxLength,
|
|
keyLength: config.keyLength,
|
|
createKey: config.createKey,
|
|
logger: logger
|
|
});
|
|
|
|
// setup routes and request-handling
|
|
const app = express();
|
|
|
|
// 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', (req, res) => {
|
|
return documentHandler.handlePost(req, res);
|
|
});
|
|
app.get('/documents/:id', (req, res) => {
|
|
return documentHandler.handleGet(req.params.id, res);
|
|
});
|
|
|
|
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}`);
|
|
}); |