79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import cors from 'cors';
|
|
import 'dotenv/config';
|
|
|
|
interface AppConfig {
|
|
readonly isProd: boolean;
|
|
readonly host: string;
|
|
readonly port: number;
|
|
readonly limit: { readonly limit: string };
|
|
readonly origin: string[];
|
|
readonly corsOptions: cors.CorsOptions;
|
|
readonly greeting: string;
|
|
readonly accessToken: TokenConfig;
|
|
readonly refreshToken: RefreshTokenConfig;
|
|
}
|
|
|
|
interface TokenConfig {
|
|
readonly secret: string;
|
|
readonly expiresIn: number;
|
|
}
|
|
|
|
interface RefreshTokenConfig extends TokenConfig {
|
|
readonly maxAge: number;
|
|
}
|
|
|
|
export function toAppConfig(): AppConfig {
|
|
const MODE = process.env.VITE_MODE || 'prod';
|
|
const isProd = MODE === 'prod';
|
|
|
|
const host = process.env.VITE_HOST || 'localhost';
|
|
const port = parseInt(process.env.VITE_PORT || '3000');
|
|
|
|
const hostRemote = process.env.VITE_HOST_REMOTE || 'localhost';
|
|
const domainName = process.env.VITE_DOMAIN_NAME || 'localhost';
|
|
|
|
const urlIp = `http://${hostRemote}:${port}`;
|
|
const urlDomain = `http://${domainName}:${port}`;
|
|
const urlRemote = `https://${host}:${port}`;
|
|
const urlLocal = host !== 'localhost' ? `https://localhost:${port}` : undefined;
|
|
|
|
const limit = { limit: '50mb' };
|
|
|
|
const origin = urlLocal != null ? [urlLocal, urlRemote, urlIp, urlDomain] : [urlRemote];
|
|
const corsOptions: cors.CorsOptions = {
|
|
origin,
|
|
credentials: true
|
|
};
|
|
|
|
const accessTokenExpiresIn = 24 * 60 * 60;
|
|
const refreshTokenExpiresIn = 15 * 24 * 60 * 60;
|
|
|
|
const accessTokenSecret = process.env.ACCESS_TOKEN_SECRET;
|
|
const refreshTokenSecret = process.env.REFRESH_TOKEN_SECRET;
|
|
if (accessTokenSecret == null || refreshTokenSecret == null) throw new Error('No secrets found');
|
|
|
|
const accessToken: TokenConfig = {
|
|
secret: accessTokenSecret,
|
|
expiresIn: accessTokenExpiresIn
|
|
};
|
|
|
|
const refreshToken: RefreshTokenConfig = {
|
|
secret: refreshTokenSecret,
|
|
expiresIn: refreshTokenExpiresIn,
|
|
maxAge: refreshTokenExpiresIn * 1000
|
|
};
|
|
|
|
const greeting = `\n\n\n\x1b[35m-server is listening at...-\x1b[0m\n\n${urlRemote}\n${urlLocal}\n\n\n`;
|
|
|
|
return {
|
|
isProd,
|
|
host,
|
|
port,
|
|
limit,
|
|
origin,
|
|
corsOptions,
|
|
accessToken,
|
|
refreshToken,
|
|
greeting
|
|
};
|
|
}
|