2020-10-31 13:33:59 -07:00
|
|
|
import express from 'express';
|
|
|
|
import authenticate from '../src/authenticate';
|
|
|
|
import dbConnection from './db';
|
2020-11-02 10:31:05 -08:00
|
|
|
import session from 'express-session';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
|
|
import grant from "grant";
|
|
|
|
import router from "./routes/user";
|
2020-11-10 14:41:56 -08:00
|
|
|
import { loadSuml } from './loader';
|
2022-01-03 10:22:10 -08:00
|
|
|
import {isGranted} from "../src/helpers";
|
|
|
|
import buildLocaleList from "../src/buildLocaleList";
|
2021-08-27 16:19:20 -07:00
|
|
|
import cookieSettings from "../src/cookieSettings";
|
2021-08-28 14:44:30 -07:00
|
|
|
import SQL from "sql-template-strings";
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2020-11-28 07:52:48 -08:00
|
|
|
global.config = loadSuml('config');
|
2021-12-18 10:54:36 -08:00
|
|
|
global.translations = loadSuml('translations');
|
2020-11-28 07:52:48 -08:00
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const app = express()
|
2021-02-01 02:17:26 -08:00
|
|
|
app.enable('trust proxy')
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-08-06 15:09:57 -07:00
|
|
|
app.use(express.json({
|
|
|
|
verify: (req, res, buf) => {
|
|
|
|
if (buf.includes(Buffer.from('narodowcy.net', 'utf-8'))) {
|
|
|
|
req.socket.end();
|
|
|
|
throw 'fuck off';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2020-11-02 10:31:05 -08:00
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(session({
|
|
|
|
secret: process.env.SECRET,
|
2021-08-27 16:19:20 -07:00
|
|
|
cookie: cookieSettings,
|
2020-11-14 22:15:37 -08:00
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
2020-11-02 10:31:05 -08:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-17 16:43:17 -07:00
|
|
|
class LazyDatabase {
|
|
|
|
constructor() {
|
|
|
|
this.db = null;
|
|
|
|
}
|
|
|
|
|
2022-01-21 01:52:25 -08:00
|
|
|
async init() {
|
2021-06-17 16:43:17 -07:00
|
|
|
if (this.db === null) {
|
|
|
|
this.db = await dbConnection();
|
2022-01-26 23:47:06 -08:00
|
|
|
// await this.db.get('PRAGMA journal_mode = WAL;');
|
2022-02-12 05:21:40 -08:00
|
|
|
await this.db.get('PRAGMA busy_timeout = 5000;');
|
2022-01-26 23:47:06 -08:00
|
|
|
// await this.db.get('PRAGMA foreign_keys = ON;')
|
2021-06-17 16:43:17 -07:00
|
|
|
}
|
2022-01-21 01:52:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(...args) {
|
|
|
|
await this.init();
|
2021-06-17 16:43:17 -07:00
|
|
|
return this.db.get(...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
async all(...args) {
|
2022-01-21 01:52:25 -08:00
|
|
|
await this.init();
|
2021-06-17 16:43:17 -07:00
|
|
|
return this.db.all(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
if (this.db !== null) {
|
|
|
|
try {
|
|
|
|
await this.db.close();
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
app.use(async function (req, res, next) {
|
2021-06-09 09:13:18 -07:00
|
|
|
try {
|
|
|
|
req.rawUser = authenticate(req);
|
|
|
|
req.user = req.rawUser && req.rawUser.authenticated ? req.rawUser : null;
|
2021-08-11 03:23:29 -07:00
|
|
|
req.isGranted = (area = '', locale = global.config.locale) => req.user && isGranted(req.user, locale, area);
|
2021-12-28 03:35:50 -08:00
|
|
|
req.locales = buildLocaleList(global.config.locale, global.config.locale === '_');
|
2021-06-17 16:43:17 -07:00
|
|
|
req.db = new LazyDatabase();
|
2021-12-02 10:11:04 -08:00
|
|
|
req.isUserAllowedToPost = async () => {
|
|
|
|
const user = await req.db.get(SQL`SELECT bannedReason FROM users WHERE id = ${req.user.id}`);
|
|
|
|
return user && !user.bannedReason;
|
|
|
|
}
|
2021-06-09 09:13:18 -07:00
|
|
|
res.on('finish', async () => {
|
2021-06-17 16:43:17 -07:00
|
|
|
await req.db.close();
|
2021-06-09 09:13:18 -07:00
|
|
|
});
|
2021-07-17 07:37:04 -07:00
|
|
|
res.set('Access-Control-Allow-Origin', '*');
|
|
|
|
res.set('Access-Control-Allow-Headers', 'authorization,content-type');
|
2021-06-09 09:13:18 -07:00
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
2020-11-02 10:31:05 -08:00
|
|
|
});
|
|
|
|
|
2021-12-05 11:56:39 -08:00
|
|
|
app.use(require('./routes/grantOverrides').default);
|
2020-11-02 12:45:45 -08:00
|
|
|
router.use(grant.express()(require('./social').config));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-08-28 07:34:30 -07:00
|
|
|
app.use(require('./routes/home').default);
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
app.use(require('./routes/banner').default);
|
|
|
|
|
|
|
|
app.use(require('./routes/user').default);
|
|
|
|
app.use(require('./routes/profile').default);
|
|
|
|
app.use(require('./routes/admin').default);
|
2021-12-18 10:54:36 -08:00
|
|
|
app.use(require('./routes/mfa').default);
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2020-11-10 15:47:44 -08:00
|
|
|
app.use(require('./routes/pronouns').default);
|
2020-10-31 13:33:59 -07:00
|
|
|
app.use(require('./routes/sources').default);
|
2020-11-10 14:41:56 -08:00
|
|
|
app.use(require('./routes/nouns').default);
|
2020-11-17 10:21:49 -08:00
|
|
|
app.use(require('./routes/inclusive').default);
|
2020-12-18 08:32:18 -08:00
|
|
|
app.use(require('./routes/terms').default);
|
2020-11-28 07:52:48 -08:00
|
|
|
app.use(require('./routes/pronounce').default);
|
2020-12-18 02:34:58 -08:00
|
|
|
app.use(require('./routes/census').default);
|
2021-09-25 05:55:53 -07:00
|
|
|
app.use(require('./routes/names').default);
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-01-06 06:21:20 -08:00
|
|
|
app.use(require('./routes/images').default);
|
2021-06-23 10:25:56 -07:00
|
|
|
app.use(require('./routes/blog').default);
|
2021-10-26 14:09:02 -07:00
|
|
|
app.use(require('./routes/calendar').default);
|
2021-01-06 06:21:20 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
console.error(err.stack);
|
|
|
|
res.status(500).send('Unexpected server error');
|
2021-06-09 09:13:18 -07:00
|
|
|
req.db.close();
|
2021-06-09 05:47:08 -07:00
|
|
|
});
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
export default {
|
|
|
|
path: '/api',
|
|
|
|
handler: app,
|
|
|
|
}
|