2022-03-16 18:05:19 +00:00
|
|
|
import cors from 'cors';
|
2023-04-14 02:44:21 +00:00
|
|
|
import express, { json } from 'express';
|
2022-03-16 18:05:19 +00:00
|
|
|
|
2023-04-19 17:04:24 +00:00
|
|
|
import { guardApi } from './lib/assertAuthentication';
|
2023-04-14 02:44:21 +00:00
|
|
|
import passport from './passport';
|
2022-03-25 02:03:58 +00:00
|
|
|
import routes from './routes';
|
2023-04-11 03:31:29 +00:00
|
|
|
import { rrouters } from './lib/rrouters';
|
2023-04-19 17:18:01 +00:00
|
|
|
import session from './session';
|
2023-04-29 00:09:19 +00:00
|
|
|
import { stdout } from './lib/shell';
|
2022-03-16 18:05:19 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
export default (async () => {
|
|
|
|
const app = express();
|
2022-03-16 18:05:19 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
app.use(json());
|
2023-04-14 02:44:21 +00:00
|
|
|
|
2023-04-29 00:09:19 +00:00
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: (requestOrigin, done) => {
|
|
|
|
stdout(`Request header: Origin=${requestOrigin}`);
|
|
|
|
|
|
|
|
done(null, requestOrigin);
|
|
|
|
},
|
|
|
|
credentials: true,
|
|
|
|
}),
|
|
|
|
);
|
2022-03-16 18:05:19 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
// Add session handler to the chain **after** adding other handlers that do
|
|
|
|
// not depend on session(s).
|
|
|
|
app.use(await session);
|
2023-04-14 02:44:21 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.authenticate('session'));
|
2023-04-14 02:44:21 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
rrouters(app, routes.private, {
|
|
|
|
assign: (router) => [guardApi, router],
|
|
|
|
route: '/api',
|
|
|
|
});
|
|
|
|
rrouters(app, routes.public, { route: '/api' });
|
2023-04-19 00:36:25 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
app.use(routes.static);
|
2022-03-16 18:05:19 +00:00
|
|
|
|
2023-04-26 02:53:27 +00:00
|
|
|
return app;
|
|
|
|
})();
|