in-the-database-2/src/index.ts

87 lines
2.3 KiB
TypeScript
Raw Normal View History

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-09-02 13:12:05 +00:00
import * as format from './lib/format';
2020-09-02 14:13:26 +00:00
import { File } from './schema';
2020-09-02 16:03:49 +00:00
import * as upload from './upload';
import * as auth from './auth';
// .env stuff
require('dotenv').config();
2020-09-02 16:03:49 +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}`, {
useNewUrlParser: true, // idfk what any of this does i just copied an example
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
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!');
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-02 13:19:29 +00:00
app.set('db', db);
app.set('config', config);
2020-09-02 13:12:05 +00:00
app.set('logger', logger);
2020-09-02 16:03:49 +00:00
upload.run(app);
auth.run(app);
2020-09-02 14:13:26 +00:00
app.get('/list', async (req, res) => { // only for testing
2020-09-02 14:44:44 +00:00
const docs = await File.find({});
res.send(docs.map((doc: any) =>
`${doc.artist} - ${doc.title} by ${doc.credit}<br>` +
doc.charts.map(ch =>
`- ${ch.difficulty} ${ch.rating}: ${ch.name}`
).join('<br>')
).join('<br><br>'));
2020-09-02 14:13:26 +00:00
});
app.get('/', (req, res) => {
2020-09-02 14:13:26 +00:00
res.send('wip');
});
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`);
});
});