2020-09-02 12:48:41 +00:00
|
|
|
import * as express from 'express';
|
|
|
|
import * as mongoose from 'mongoose';
|
|
|
|
import * as fs from 'fs';
|
2020-09-02 13:12:05 +00:00
|
|
|
import * as winston from 'winston';
|
2020-09-02 16:03:49 +00:00
|
|
|
import * as fileUpload from 'express-fileupload';
|
2020-10-12 00:27:47 +00:00
|
|
|
import * as session from 'express-session';
|
|
|
|
const MongoStore = require('connect-mongo')(session);
|
2020-09-02 13:12:05 +00:00
|
|
|
|
|
|
|
import * as format from './lib/format';
|
2020-10-12 00:27:47 +00:00
|
|
|
import { File, User } from './schema';
|
2020-09-02 12:48:41 +00:00
|
|
|
|
2020-09-02 16:03:49 +00:00
|
|
|
import * as upload from './upload';
|
2020-09-03 07:28:22 +00:00
|
|
|
import * as auth from './auth';
|
|
|
|
|
|
|
|
// .env stuff
|
|
|
|
require('dotenv').config();
|
2020-09-02 16:03:49 +00:00
|
|
|
|
2020-09-02 12:48:41 +00:00
|
|
|
const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'}));
|
|
|
|
|
2020-09-02 13:19:29 +00:00
|
|
|
const db = mongoose.connect(`${config.dbconnectionURL}/${config.dbname}`, {
|
2020-09-02 13:17:26 +00:00
|
|
|
useNewUrlParser: true, // idfk what any of this does i just copied an example
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
useFindAndModify: false,
|
|
|
|
useCreateIndex: true
|
2020-09-02 12:48:41 +00:00
|
|
|
});
|
|
|
|
|
2020-09-02 13:12:05 +00:00
|
|
|
const logger = winston.createLogger({
|
|
|
|
level: 'info',
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.timestamp(),
|
|
|
|
winston.format.printf(log => `${format.formatTime(new Date(log.timestamp))} | ${log.message}`)
|
|
|
|
),
|
|
|
|
transports: [
|
|
|
|
new winston.transports.File({filename: `${config.name}-error.log`, level: 'error'}),
|
|
|
|
new winston.transports.File({filename: `${config.name}.log`}),
|
|
|
|
new winston.transports.Console({
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.colorize(),
|
|
|
|
winston.format.timestamp(),
|
|
|
|
winston.format.printf(log =>
|
|
|
|
`${format.formatTime(new Date(log.timestamp))} - [${log.level}] ${log.message}`
|
|
|
|
)
|
|
|
|
),
|
|
|
|
level: process.env.DEBUG === 'true' ? 'silly' : 'info'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.info('connecting to mongodb database');
|
2020-09-02 13:19:29 +00:00
|
|
|
db.then(() => {
|
2020-09-02 13:12:05 +00:00
|
|
|
logger.info('connected to database!');
|
2020-09-02 12:48:41 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2020-09-02 14:44:44 +00:00
|
|
|
// @ts-ignore
|
|
|
|
app.use(express.urlencoded({extended: true}));
|
2020-09-02 16:03:49 +00:00
|
|
|
app.use(fileUpload({limits: { fileSize: 50 * 1024 * 1024 }}));
|
2020-09-03 22:18:43 +00:00
|
|
|
app.use(express.static('public', {extensions: ['html', 'htm']}));
|
2020-10-11 22:21:32 +00:00
|
|
|
app.use(express.static('storage', {extensions: ['zip']}));
|
2020-10-12 00:27:47 +00:00
|
|
|
app.use(session({
|
|
|
|
name: 'funnyuserdata',
|
|
|
|
secret: 'wenis',
|
|
|
|
store: new MongoStore({ mongooseConnection: mongoose.connection }),
|
|
|
|
cookie: {
|
|
|
|
maxAge: 1000 * 60 * 60 * 24 * 365 * 10, // 10 years
|
|
|
|
httpOnly: true,
|
|
|
|
sameSite: 'lax',
|
|
|
|
},
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true
|
|
|
|
}));
|
2020-09-03 22:18:43 +00:00
|
|
|
app.use('/assets', express.static('assets'));
|
2020-09-02 12:48:41 +00:00
|
|
|
|
2020-09-02 13:19:29 +00:00
|
|
|
app.set('db', db);
|
2020-09-02 12:48:41 +00:00
|
|
|
app.set('config', config);
|
2020-09-02 13:12:05 +00:00
|
|
|
app.set('logger', logger);
|
2020-09-02 12:48:41 +00:00
|
|
|
|
2020-09-02 16:03:49 +00:00
|
|
|
upload.run(app);
|
2020-09-03 07:28:22 +00:00
|
|
|
auth.run(app);
|
2020-09-02 14:13:26 +00:00
|
|
|
|
2020-09-03 22:18:43 +00:00
|
|
|
app.get('/api/list', async (req, res) => { // only for testing
|
2020-10-12 00:27:47 +00:00
|
|
|
const files = await File.find({});
|
|
|
|
|
|
|
|
const docs = [];
|
|
|
|
for (const doc of files) {
|
|
|
|
const d = doc.toJSON();
|
|
|
|
const user = await User.find({uuid: d.uploader});
|
|
|
|
d.uploaderJSON = user[0].toJSON(); // this is built upon 20 layers of metajank and i despise it
|
|
|
|
docs.push(d);
|
|
|
|
}
|
|
|
|
|
2020-09-03 22:18:43 +00:00
|
|
|
// TODO: filter out _id and __v? possibly more
|
|
|
|
res.send(docs);
|
2020-09-02 14:13:26 +00:00
|
|
|
});
|
|
|
|
|
2020-09-02 12:48:41 +00:00
|
|
|
app.get('*', (req, res) => {
|
|
|
|
res.status(404).send('404');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(config.port, () => {
|
2020-09-02 13:12:05 +00:00
|
|
|
logger.info(`expressjs server launched on port ${config.port}, should now function properly`);
|
2020-09-02 12:48:41 +00:00
|
|
|
});
|
|
|
|
});
|