wildflock/src/app.ts

100 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2023-08-06 06:21:17 +00:00
import { randomUUID } from 'crypto';
import { feathers } from '@feathersjs/feathers';
import express, { rest, json, urlencoded, cors, serveStatic, notFound, errorHandler } from '@feathersjs/express';
import configuration from '@feathersjs/configuration';
import socketio from '@feathersjs/socketio';
import session from 'express-session';
import cookieParser from 'cookie-parser';
import RedisStore from 'connect-redis';
import { createClient } from 'redis';
import config from 'config';
import type { Application } from './declarations';
import { logger } from './logger';
import { logError } from './hooks/log-error';
import { services } from './services/index';
import { channels } from './channels';
import { Env, getEnv } from './helpers/get-env';
2023-07-29 18:10:00 +00:00
2023-07-29 21:50:42 +00:00
const app: Application = express(feathers());
2023-08-12 07:56:16 +00:00
let sessionStore;
2023-07-29 18:10:00 +00:00
// Load app configuration
2023-07-29 21:50:42 +00:00
app.configure(configuration());
app.use(cors());
app.use(
2023-08-06 06:21:17 +00:00
json({
limit: '20mb',
})
2023-07-29 21:50:42 +00:00
);
2023-07-29 18:10:00 +00:00
app.use(cookieParser());
2023-08-12 07:56:16 +00:00
if (getEnv() === Env.prod) {
const redisClient = createClient({
url: config.get('redis.url'),
});
sessionStore = new RedisStore({
prefix: 'walias:',
client: redisClient,
});
redisClient.connect().catch(console.error);
}
2023-07-29 21:50:42 +00:00
app.use(
2023-08-06 06:21:17 +00:00
session({
store: sessionStore,
2023-08-13 15:46:56 +00:00
secret: config.get('sessionSecret') || randomUUID(),
2023-08-06 06:21:17 +00:00
resave: false,
saveUninitialized: false,
cookie: { secure: false },
})
2023-07-29 21:50:42 +00:00
);
2023-07-29 18:10:00 +00:00
// Propagate session to request.params in feathers services
app.use(function (req, _res, next) {
2023-08-06 06:21:17 +00:00
req.feathers = {
...req.feathers,
session: req.session,
};
next();
2023-07-29 18:10:00 +00:00
});
2023-07-29 21:50:42 +00:00
app.use(urlencoded({ extended: true }));
2023-07-29 18:10:00 +00:00
// Host the public folder
2023-08-06 06:21:17 +00:00
app.use('/', serveStatic(app.get('public')));
2023-07-29 18:10:00 +00:00
// Configure services and real-time functionality
2023-07-29 21:50:42 +00:00
app.configure(rest());
2023-07-29 18:10:00 +00:00
app.configure(
2023-08-06 06:21:17 +00:00
socketio({
cors: {
origin: app.get('origins'),
},
})
2023-07-29 21:50:42 +00:00
);
app.configure(services);
app.configure(channels);
2023-07-29 18:10:00 +00:00
// Configure a middleware for 404s and the error handler
2023-07-29 21:50:42 +00:00
app.use(notFound());
app.use(errorHandler({ logger }));
2023-07-29 18:10:00 +00:00
// Register hooks that run on all service methods
app.hooks({
2023-08-06 06:21:17 +00:00
around: {
all: [logError],
},
before: {},
after: {},
error: {},
2023-07-29 21:50:42 +00:00
});
2023-07-29 18:10:00 +00:00
// Register application setup and teardown hooks here
app.hooks({
2023-08-06 06:21:17 +00:00
setup: [],
teardown: [],
2023-07-29 21:50:42 +00:00
});
2023-07-29 18:10:00 +00:00
2023-07-29 21:50:42 +00:00
export { app };