mirror of
https://dev.s-ul.net/Galexion/MaiMaiDXNet.git
synced 2024-11-24 04:40:11 +01:00
113 lines
3.3 KiB
JavaScript
113 lines
3.3 KiB
JavaScript
const chokidar = require('chokidar');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
var createError = require('http-errors');
|
|
var express = require('express');
|
|
var cookieParser = require('cookie-parser');
|
|
var logger = require('morgan');
|
|
var indexRouter = require('./routes/index');
|
|
var apiRouter = require('./routes/api');
|
|
var config = require("./config.json");
|
|
|
|
if (config.serverType === 0) { // Only run the image Importer with aqua or when given Artemis Settings for it)
|
|
const watcher = chokidar.watch(config.aquaSrvDir, {
|
|
persistent: true
|
|
});
|
|
|
|
watcher
|
|
.on('add', (filePath) => {
|
|
const isImage = /\.(jpe?g|png|gif)$/i.test(filePath);
|
|
if (isImage) {
|
|
const fileName = path.basename(filePath);
|
|
const destPath = path.join(config.imageFolder, fileName);
|
|
fs.access(destPath, (err) => {
|
|
if (err) {
|
|
fs.copyFile(filePath, destPath, (err) => {
|
|
if (err) throw err;
|
|
console.log(`${fileName} Imported.`);
|
|
});
|
|
} else {
|
|
}
|
|
});
|
|
}
|
|
})
|
|
.on('error', (error) => {
|
|
console.error(`Watcher error: ${error}`);
|
|
});
|
|
} else if (config.ArtImageDirectory && config.ArtImageImport === true){
|
|
const watcher = chokidar.watch(config.ArtImageDirectory, {
|
|
persistent: true
|
|
});
|
|
|
|
watcher
|
|
.on('add', (filePath) => {
|
|
const isImage = /\.(jpe?g|png|gif)$/i.test(filePath);
|
|
if (isImage) {
|
|
const fileName = path.basename(filePath);
|
|
const destPath = path.join(config.imageFolder, fileName);
|
|
fs.access(destPath, (err) => {
|
|
if (err) {
|
|
fs.copyFile(filePath, destPath, (err) => {
|
|
if (err) throw err;
|
|
console.log(`${fileName} Imported.`);
|
|
});
|
|
} else {
|
|
}
|
|
});
|
|
}
|
|
})
|
|
.on('error', (error) => {
|
|
console.error(`Watcher error: ${error}`);
|
|
});
|
|
} else {
|
|
console.log("Image Export Disabled. Check readme.md for details.")
|
|
}
|
|
if(config.serverType === 1 && !config.ArtConnSettings.connectionLimit) {
|
|
console.error("You are Missing the `ConnectionLimit` setting. Please add and set it in config.json.");
|
|
process.exit(1);
|
|
}
|
|
|
|
// readFileSync function must use __dirname get current directory
|
|
// require use ./ refer to current directory.
|
|
|
|
const options = {};
|
|
var app = express()
|
|
|
|
// Create HTTPs server.
|
|
|
|
//var httpsServer = https.createServer(options, app);
|
|
|
|
// view engine setup
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'ejs');
|
|
app.use(express.static(__dirname + '/public'));
|
|
app.use(logger('dev'));
|
|
app.use(express.urlencoded({ extended: false }));
|
|
app.use(cookieParser());
|
|
app.use(express.json({ extended: false }));
|
|
|
|
// define routes
|
|
app.use('/', indexRouter);
|
|
app.use('/api/', apiRouter);
|
|
|
|
// catch 404 and forward to error handler
|
|
app.use(function (req, res, next) {
|
|
next(createError(404));
|
|
});
|
|
|
|
var PORT = config.hostPort || 8000
|
|
|
|
// error handler
|
|
app.use(function (err, req, res, next) {
|
|
// set locals, only providing error in development
|
|
res.locals.message = err.message;
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
// render the error page
|
|
res.status(err.status || 500);
|
|
res.render('error');
|
|
});
|
|
|
|
app.listen(PORT, () => console.log(`Server is running in port ${PORT}, Server type ${config.serverType}`));
|
|
|
|
module.exports = app; |