chore: Centralize environment detection
This commit is contained in:
@@ -711,6 +711,27 @@ export class Environment {
|
|||||||
].includes(this.URL);
|
].includes(this.URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the current installation is running in production.
|
||||||
|
*/
|
||||||
|
public get isProduction() {
|
||||||
|
return this.ENVIRONMENT === "production";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the current installation is running in the development environment.
|
||||||
|
*/
|
||||||
|
public get isDevelopment() {
|
||||||
|
return this.ENVIRONMENT === "development";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the current installation is running in a test environment.
|
||||||
|
*/
|
||||||
|
public get isTest() {
|
||||||
|
return this.ENVIRONMENT === "test";
|
||||||
|
}
|
||||||
|
|
||||||
private toOptionalString(value: string | undefined) {
|
private toOptionalString(value: string | undefined) {
|
||||||
return value ? value : undefined;
|
return value ? value : undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import Sentry from "@server/logging/sentry";
|
|||||||
import ShutdownHelper from "@server/utils/ShutdownHelper";
|
import ShutdownHelper from "@server/utils/ShutdownHelper";
|
||||||
import * as Tracing from "./tracer";
|
import * as Tracing from "./tracer";
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
|
|
||||||
type LogCategory =
|
type LogCategory =
|
||||||
| "lifecycle"
|
| "lifecycle"
|
||||||
| "authentication"
|
| "authentication"
|
||||||
@@ -53,7 +51,7 @@ class Logger {
|
|||||||
});
|
});
|
||||||
this.output.add(
|
this.output.add(
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
format: isProduction
|
format: env.isProduction
|
||||||
? winston.format.json()
|
? winston.format.json()
|
||||||
: winston.format.combine(
|
: winston.format.combine(
|
||||||
winston.format.colorize(),
|
winston.format.colorize(),
|
||||||
@@ -109,7 +107,7 @@ class Logger {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isProduction) {
|
if (env.isProduction) {
|
||||||
this.output.warn(message, this.sanitize(extra));
|
this.output.warn(message, this.sanitize(extra));
|
||||||
} else if (extra) {
|
} else if (extra) {
|
||||||
console.warn(message, extra);
|
console.warn(message, extra);
|
||||||
@@ -155,7 +153,7 @@ class Logger {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isProduction) {
|
if (env.isProduction) {
|
||||||
this.output.error(message, {
|
this.output.error(message, {
|
||||||
error: error.message,
|
error: error.message,
|
||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
@@ -190,7 +188,7 @@ class Logger {
|
|||||||
*/
|
*/
|
||||||
private sanitize<T>(input: T): T {
|
private sanitize<T>(input: T): T {
|
||||||
// Short circuit if we're not in production to enable easier debugging
|
// Short circuit if we're not in production to enable easier debugging
|
||||||
if (!isProduction) {
|
if (!env.isProduction) {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,8 @@ import { Share } from "@server/models";
|
|||||||
export default function shareDomains() {
|
export default function shareDomains() {
|
||||||
return async function shareDomainsMiddleware(ctx: Context, next: Next) {
|
return async function shareDomainsMiddleware(ctx: Context, next: Next) {
|
||||||
const isCustomDomain = parseDomain(ctx.host).custom;
|
const isCustomDomain = parseDomain(ctx.host).custom;
|
||||||
const isDevelopment = env.ENVIRONMENT === "development";
|
|
||||||
|
|
||||||
if (isDevelopment || (isCustomDomain && env.isCloudHosted)) {
|
if (env.isDevelopment || (isCustomDomain && env.isCloudHosted)) {
|
||||||
const share = await Share.unscoped().findOne({
|
const share = await Share.unscoped().findOne({
|
||||||
where: {
|
where: {
|
||||||
domain: ctx.hostname,
|
domain: ctx.hostname,
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ import { getTeamFromContext } from "@server/utils/passport";
|
|||||||
import prefetchTags from "@server/utils/prefetchTags";
|
import prefetchTags from "@server/utils/prefetchTags";
|
||||||
import readManifestFile from "@server/utils/readManifestFile";
|
import readManifestFile from "@server/utils/readManifestFile";
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
const isDevelopment = env.ENVIRONMENT === "development";
|
|
||||||
const isTest = env.ENVIRONMENT === "test";
|
|
||||||
|
|
||||||
const readFile = util.promisify(fs.readFile);
|
const readFile = util.promisify(fs.readFile);
|
||||||
const entry = "app/index.tsx";
|
const entry = "app/index.tsx";
|
||||||
const viteHost = env.URL.replace(`:${env.PORT}`, ":3001");
|
const viteHost = env.URL.replace(`:${env.PORT}`, ":3001");
|
||||||
@@ -25,17 +21,17 @@ const viteHost = env.URL.replace(`:${env.PORT}`, ":3001");
|
|||||||
let indexHtmlCache: Buffer | undefined;
|
let indexHtmlCache: Buffer | undefined;
|
||||||
|
|
||||||
const readIndexFile = async (): Promise<Buffer> => {
|
const readIndexFile = async (): Promise<Buffer> => {
|
||||||
if (isProduction || isTest) {
|
if (env.isProduction || env.isTest) {
|
||||||
if (indexHtmlCache) {
|
if (indexHtmlCache) {
|
||||||
return indexHtmlCache;
|
return indexHtmlCache;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isTest) {
|
if (env.isTest) {
|
||||||
return await readFile(path.join(__dirname, "../static/index.html"));
|
return await readFile(path.join(__dirname, "../static/index.html"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDevelopment) {
|
if (env.isDevelopment) {
|
||||||
return await readFile(
|
return await readFile(
|
||||||
path.join(__dirname, "../../../server/static/index.html")
|
path.join(__dirname, "../../../server/static/index.html")
|
||||||
);
|
);
|
||||||
@@ -77,7 +73,7 @@ export const renderApp = async (
|
|||||||
</script>
|
</script>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const scriptTags = isProduction
|
const scriptTags = env.isProduction
|
||||||
? `<script type="module" nonce="${ctx.state.cspNonce}" src="${
|
? `<script type="module" nonce="${ctx.state.cspNonce}" src="${
|
||||||
env.CDN_URL || ""
|
env.CDN_URL || ""
|
||||||
}/static/${readManifestFile()[entry]["file"]}"></script>`
|
}/static/${readManifestFile()[entry]["file"]}"></script>`
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import apexRedirect from "../middlewares/apexRedirect";
|
|||||||
import { renderApp, renderShare } from "./app";
|
import { renderApp, renderShare } from "./app";
|
||||||
import errors from "./errors";
|
import errors from "./errors";
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
const koa = new Koa();
|
const koa = new Koa();
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
@@ -59,7 +58,7 @@ router.use(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isProduction) {
|
if (env.isProduction) {
|
||||||
router.get("/static/*", async (ctx) => {
|
router.get("/static/*", async (ctx) => {
|
||||||
try {
|
try {
|
||||||
const pathname = ctx.path.substring(8);
|
const pathname = ctx.path.substring(8);
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ import routes from "../routes";
|
|||||||
import api from "../routes/api";
|
import api from "../routes/api";
|
||||||
import auth from "../routes/auth";
|
import auth from "../routes/auth";
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
|
|
||||||
// Construct scripts CSP based on services in use by this installation
|
// Construct scripts CSP based on services in use by this installation
|
||||||
const defaultSrc = ["'self'"];
|
const defaultSrc = ["'self'"];
|
||||||
const scriptSrc = ["'self'", "gist.github.com", "www.googletagmanager.com"];
|
const scriptSrc = ["'self'", "gist.github.com", "www.googletagmanager.com"];
|
||||||
@@ -36,7 +34,7 @@ if (env.isCloudHosted) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allow to load assets from Vite
|
// Allow to load assets from Vite
|
||||||
if (!isProduction) {
|
if (!env.isProduction) {
|
||||||
scriptSrc.push(env.URL.replace(`:${env.PORT}`, ":3001"));
|
scriptSrc.push(env.URL.replace(`:${env.PORT}`, ":3001"));
|
||||||
scriptSrc.push("localhost:3001");
|
scriptSrc.push("localhost:3001");
|
||||||
}
|
}
|
||||||
@@ -54,7 +52,7 @@ if (env.CDN_URL) {
|
|||||||
export default function init(app: Koa = new Koa(), server?: Server) {
|
export default function init(app: Koa = new Koa(), server?: Server) {
|
||||||
void initI18n();
|
void initI18n();
|
||||||
|
|
||||||
if (isProduction) {
|
if (env.isProduction) {
|
||||||
// Force redirect to HTTPS protocol unless explicitly disabled
|
// Force redirect to HTTPS protocol unless explicitly disabled
|
||||||
if (env.FORCE_HTTPS) {
|
if (env.FORCE_HTTPS) {
|
||||||
app.use(
|
app.use(
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import env from "@server/env";
|
|||||||
import Logger from "../logging/Logger";
|
import Logger from "../logging/Logger";
|
||||||
import * as models from "../models";
|
import * as models from "../models";
|
||||||
|
|
||||||
const isDevelopment = env.ENVIRONMENT === "development";
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
const isSSLDisabled = env.PGSSLMODE === "disable";
|
const isSSLDisabled = env.PGSSLMODE === "disable";
|
||||||
const poolMax = env.DATABASE_CONNECTION_POOL_MAX ?? 5;
|
const poolMax = env.DATABASE_CONNECTION_POOL_MAX ?? 5;
|
||||||
const poolMin = env.DATABASE_CONNECTION_POOL_MIN ?? 0;
|
const poolMin = env.DATABASE_CONNECTION_POOL_MIN ?? 0;
|
||||||
@@ -19,10 +17,10 @@ export const sequelize = new Sequelize(url, {
|
|||||||
logging: (msg) =>
|
logging: (msg) =>
|
||||||
process.env.DEBUG?.includes("database") && Logger.debug("database", msg),
|
process.env.DEBUG?.includes("database") && Logger.debug("database", msg),
|
||||||
typeValidation: true,
|
typeValidation: true,
|
||||||
logQueryParameters: isDevelopment,
|
logQueryParameters: env.isDevelopment,
|
||||||
dialectOptions: {
|
dialectOptions: {
|
||||||
ssl:
|
ssl:
|
||||||
isProduction && !isSSLDisabled
|
env.isProduction && !isSSLDisabled
|
||||||
? {
|
? {
|
||||||
// Ref.: https://github.com/brianc/node-postgres/issues/2009
|
// Ref.: https://github.com/brianc/node-postgres/issues/2009
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import ReactDOMServer from "react-dom/server";
|
|||||||
import env from "@server/env";
|
import env from "@server/env";
|
||||||
import readManifestFile, { ManifestStructure } from "./readManifestFile";
|
import readManifestFile, { ManifestStructure } from "./readManifestFile";
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
|
|
||||||
const prefetchTags = [];
|
const prefetchTags = [];
|
||||||
|
|
||||||
if (env.AWS_S3_ACCELERATE_URL) {
|
if (env.AWS_S3_ACCELERATE_URL) {
|
||||||
@@ -30,7 +28,7 @@ if (env.CDN_URL) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isProduction) {
|
if (env.isProduction) {
|
||||||
const manifest = readManifestFile();
|
const manifest = readManifestFile();
|
||||||
|
|
||||||
const returnFileAndImportsFromManifest = (
|
const returnFileAndImportsFromManifest = (
|
||||||
|
|||||||
@@ -33,11 +33,10 @@ export async function checkDataMigrations() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isProduction = env.ENVIRONMENT === "production";
|
|
||||||
const teams = await Team.count();
|
const teams = await Team.count();
|
||||||
const providers = await AuthenticationProvider.count();
|
const providers = await AuthenticationProvider.count();
|
||||||
|
|
||||||
if (isProduction && teams && !providers) {
|
if (env.isProduction && teams && !providers) {
|
||||||
Logger.warn(`
|
Logger.warn(`
|
||||||
This version of Outline cannot start until a data migration is complete.
|
This version of Outline cannot start until a data migration is complete.
|
||||||
Backup your database, run the database migrations and the following script:
|
Backup your database, run the database migrations and the following script:
|
||||||
|
|||||||
Reference in New Issue
Block a user