mirror of
https://github.com/sct/overseerr.git
synced 2025-09-17 17:24:35 +02:00

* feat(frontend): user Context / useUser hook Adds a UserContext to wrap the app and load/cache the user when the website renders. Also adds the useUser hook to pull in user data anywhere its needed on the site. This commit also adds redirection to the login page for users who are not signed in * fix(frontend): use process.env.PORT for user request on server side (defaults to 3000) * docs(frontend): added documentation/notes for how the user context/login works
97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import express, { Request, Response, NextFunction } from 'express';
|
|
import next from 'next';
|
|
import path from 'path';
|
|
import { createConnection, getRepository } from 'typeorm';
|
|
import routes from './routes';
|
|
import bodyParser from 'body-parser';
|
|
import cookieParser from 'cookie-parser';
|
|
import session from 'express-session';
|
|
import { TypeormStore } from 'connect-typeorm/out';
|
|
import YAML from 'yamljs';
|
|
import swaggerUi from 'swagger-ui-express';
|
|
import { OpenApiValidator } from 'express-openapi-validator';
|
|
import { Session } from './entity/Session';
|
|
import { getSettings } from './lib/settings';
|
|
|
|
const API_SPEC_PATH = path.join(__dirname, 'overseerr-api.yml');
|
|
|
|
const dev = process.env.NODE_ENV !== 'production';
|
|
const app = next({ dev });
|
|
const handle = app.getRequestHandler();
|
|
|
|
createConnection();
|
|
|
|
app
|
|
.prepare()
|
|
.then(async () => {
|
|
// Load Settings
|
|
getSettings().load();
|
|
|
|
const server = express();
|
|
server.use(cookieParser());
|
|
server.use(bodyParser.json());
|
|
server.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
// Setup sessions
|
|
const sessionRespository = getRepository(Session);
|
|
server.use(
|
|
'/api',
|
|
session({
|
|
secret: 'verysecret',
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
store: new TypeormStore({
|
|
cleanupLimit: 2,
|
|
ttl: 86400,
|
|
}).connect(sessionRespository),
|
|
})
|
|
);
|
|
const apiDocs = YAML.load(API_SPEC_PATH);
|
|
server.use('/api-docs', swaggerUi.serve, swaggerUi.setup(apiDocs));
|
|
await new OpenApiValidator({
|
|
apiSpec: API_SPEC_PATH,
|
|
validateRequests: true,
|
|
validateResponses: true,
|
|
}).install(server);
|
|
/**
|
|
* This is a workaround to convert dates to strings before they are validated by
|
|
* OpenAPI validator. Otherwise, they are treated as objects instead of strings
|
|
* and response validation will fail
|
|
*/
|
|
server.use((req, res, next) => {
|
|
const original = res.json;
|
|
res.json = function jsonp(json) {
|
|
return original.call(this, JSON.parse(JSON.stringify(json)));
|
|
};
|
|
next();
|
|
});
|
|
server.use('/api/v1', routes);
|
|
server.get('*', (req, res) => handle(req, res));
|
|
server.use(
|
|
(
|
|
err: { status: number; message: string; errors: string[] },
|
|
req: Request,
|
|
res: Response,
|
|
_next: NextFunction
|
|
) => {
|
|
// format error
|
|
res.status(err.status || 500).json({
|
|
message: err.message,
|
|
errors: err.errors,
|
|
});
|
|
}
|
|
);
|
|
|
|
const port = Number(process.env.PORT) || 3000;
|
|
server.listen(port, (err) => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
console.log(`Ready to do stuff http://localhost:${port}`);
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.error(err.stack);
|
|
process.exit(1);
|
|
});
|