This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/index.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

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';
2020-12-30 15:03:30 -08:00
import {buildLocaleList, isGranted} from "../src/helpers";
2020-10-31 13:33:59 -07:00
2020-11-28 07:52:48 -08:00
global.config = loadSuml('config');
2020-10-31 13:33:59 -07:00
const app = express()
app.enable('trust proxy')
2020-10-31 13:33:59 -07:00
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
2020-11-02 10:31:05 -08:00
app.use(cookieParser());
app.use(session({
secret: process.env.SECRET,
2020-11-02 11:16:08 -08:00
cookie: {},
resave: false,
saveUninitialized: false,
2020-11-02 10:31:05 -08:00
}));
2020-10-31 13:33:59 -07:00
app.use(async function (req, res, next) {
2020-11-28 07:52:48 -08:00
req.config = global.config;
2020-12-21 11:59:18 -08:00
req.locales = buildLocaleList(global.config.locale);
2020-10-31 13:33:59 -07:00
req.rawUser = authenticate(req);
req.user = req.rawUser && req.rawUser.authenticated ? req.rawUser : null;
2020-12-30 15:03:30 -08:00
req.isGranted = (area, locale = global.config.locale) => req.user && isGranted(req.user, locale, area);
2020-10-31 13:33:59 -07:00
req.db = await dbConnection();
2021-05-18 09:23:52 -07:00
res.on('finish', async () => {
await req.db.close();
});
2020-10-31 13:33:59 -07:00
next();
2020-11-02 10:31:05 -08:00
});
2020-11-02 12:45:45 -08:00
router.use(grant.express()(require('./social').config));
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);
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);
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);
2020-10-31 13:33:59 -07:00
2021-01-06 06:21:20 -08:00
app.use(require('./routes/images').default);
2021-05-18 09:23:52 -07:00
app.use(async function (req, res, next) {
// await next();
console.log(res);
await req.db.close();
console.log('closed');
});
2020-10-31 13:33:59 -07:00
export default {
path: '/api',
handler: app,
}