mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-06 13:58:05 +03:00
Refactor how NODE_ENV is used.
This commit is contained in:
Vendored
+1
-1
@@ -47,7 +47,7 @@ const logger = CreateLogCtx(__filename);
|
||||
let dbName = ServerConfig.MONGO_DATABASE_NAME;
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (process.env.NODE_ENV === "test") {
|
||||
if (Environment.nodeEnv === "test") {
|
||||
dbName = `testingdb`;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ function getTestTxt() {
|
||||
}
|
||||
|
||||
const ResetFileRoot = () => {
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
if (Environment.nodeEnv !== "test") {
|
||||
throw new Error(
|
||||
`Not in test, yet CDN.test.ts was triggered, which could rm -rf something important.`
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import { ServerConfig } from "lib/setup/config";
|
||||
import { Environment, ServerConfig } from "lib/setup/config";
|
||||
import nodemailer, { SentMessageInfo, Transporter } from "nodemailer";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
@@ -34,7 +34,7 @@ if (ServerConfig.EMAIL_CONFIG) {
|
||||
}
|
||||
|
||||
export function SendEmail(to: string, htmlContent: string): Promise<SentMessageInfo> | undefined {
|
||||
if (process.env.NODE_ENV === "test") {
|
||||
if (Environment.nodeEnv === "test") {
|
||||
logger.debug(`Stubbed out SendEmail as env was test.`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -169,10 +169,24 @@ if (!cdnRoot) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const nodeEnv = process.env.NODE_ENV;
|
||||
if (!nodeEnv) {
|
||||
logger.error(`No NODE_ENV specified in environment. Terminating.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!["dev", "production", "staging", "test"].includes(nodeEnv)) {
|
||||
logger.error(
|
||||
`Invalid NODE_ENV set in environment. Expected dev, production, test or staging. Got ${nodeEnv}.`
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export const Environment = {
|
||||
port,
|
||||
redisUrl,
|
||||
mongoUrl,
|
||||
// If node_env is test, force to ./test-cdn.
|
||||
cdnRoot: process.env.NODE_ENV === "test" ? "./test-cdn" : cdnRoot,
|
||||
cdnRoot: nodeEnv === "test" ? "./test-cdn" : cdnRoot,
|
||||
nodeEnv,
|
||||
};
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import { InitSequenceDocs } from "external/mongo/sequence-docs";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
logger.info(`Booting ${ServerTypeInfo.name} - ${FormatVersion()} [ENV: ${process.env.NODE_ENV}]`);
|
||||
logger.info(`Booting ${ServerTypeInfo.name} - ${FormatVersion()} [ENV: ${Environment.nodeEnv}]`);
|
||||
logger.info(`Log level is set to ${ServerConfig.LOG_LEVEL}.`);
|
||||
|
||||
logger.info(`Loading sequence documents...`);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import RateLimitRedis from "rate-limit-redis";
|
||||
import { ServerConfig } from "lib/setup/config";
|
||||
import { Environment, ServerConfig } from "lib/setup/config";
|
||||
import rateLimit from "express-rate-limit";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import { RedisClient } from "external/redis/redis";
|
||||
@@ -7,7 +7,7 @@ import { RedisClient } from "external/redis/redis";
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
const store =
|
||||
process.env.NODE_ENV === "production"
|
||||
Environment.nodeEnv === "production" || Environment.nodeEnv === "staging"
|
||||
? new RateLimitRedis({ prefix: `${ServerConfig.TYPE}-RL:`, client: RedisClient })
|
||||
: undefined; // undefined forces a default to an in-memory store
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import CreateLogCtx from "lib/logger/logger";
|
||||
import { FormatUserDoc } from "utils/user";
|
||||
import nodeFetch from "utils/fetch";
|
||||
import { Random20Hex } from "utils/misc";
|
||||
import { ServerConfig } from "lib/setup/config";
|
||||
import { Environment, ServerConfig } from "lib/setup/config";
|
||||
import { CreateURLWithParams } from "utils/url";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
@@ -173,5 +173,5 @@ export function MountAuthCookie(
|
||||
};
|
||||
|
||||
req.session.cookie.maxAge = 3.154e10;
|
||||
req.session.cookie.secure = process.env.NODE_ENV === "production";
|
||||
req.session.cookie.secure = Environment.nodeEnv === "production";
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import { DecrementCounterValue, GetNextCounterValue } from "utils/db";
|
||||
import { SendEmail } from "lib/email/client";
|
||||
import { EmailFormatResetPassword, EmailFormatVerifyEmail } from "lib/email/formats";
|
||||
import { Random20Hex } from "utils/misc";
|
||||
import { ServerConfig } from "lib/setup/config";
|
||||
import { Environment, ServerConfig } from "lib/setup/config";
|
||||
import { integer } from ".pnpm/tachi-common@0.2.36/node_modules/tachi-common";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
@@ -64,7 +64,7 @@ router.post(
|
||||
logger.verbose(`Recieved login request with username ${req.body.username} (${req.ip})`);
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
if (Environment.nodeEnv === "production" || Environment.nodeEnv === "staging") {
|
||||
logger.verbose("Validating captcha...");
|
||||
const validCaptcha = await ValidateCaptcha(
|
||||
req.body.recaptcha,
|
||||
@@ -177,7 +177,7 @@ router.post(
|
||||
logger.verbose(`Recieved register request with username ${req.body.username} (${req.ip})`);
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
if (Environment.nodeEnv === "production" || Environment.nodeEnv === "staging") {
|
||||
logger.verbose("Validating captcha...");
|
||||
const validCaptcha = await ValidateCaptcha(
|
||||
req.body.recaptcha,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { RequestHandler } from "express";
|
||||
import db from "external/mongo/db";
|
||||
import { SYMBOL_TachiData } from "lib/constants/tachi";
|
||||
import { Environment } from "lib/setup/config";
|
||||
import { OAuth2ApplicationDocument } from "tachi-common";
|
||||
import { AssignToReqTachiData } from "utils/req-tachi-data";
|
||||
|
||||
@@ -36,7 +37,7 @@ export const RequireOwnershipOfClient: RequestHandler = (req, res, next) => {
|
||||
// request. To hack around this for testing, we perform this hack.
|
||||
// There's an open issue for this here: https://github.com/i-like-robots/express-request-mock/issues/19
|
||||
/* istanbul ignore next */
|
||||
if (process.env.NODE_ENV === "test" && req.body.__terribleHackOauth2ClientDoc) {
|
||||
if (Environment.nodeEnv === "test" && req.body.__terribleHackOauth2ClientDoc) {
|
||||
// obviously a glaring hack and security flaw - this only applies
|
||||
// in testing.
|
||||
client = req.body.__terribleHackOauth2ClientDoc;
|
||||
|
||||
@@ -15,6 +15,7 @@ import { RequireSelfRequestFromUser } from "../../../middleware";
|
||||
import { RequireKamaitachi } from "server/middleware/type-require";
|
||||
import fetch from "utils/fetch";
|
||||
import { Random20Hex } from "utils/misc";
|
||||
import { Environment } from "lib/setup/config";
|
||||
|
||||
const router: Router = Router({ mergeParams: true });
|
||||
|
||||
@@ -71,7 +72,7 @@ router.post(
|
||||
const user = req[SYMBOL_TachiData]!.requestedUser!;
|
||||
const kaiType = req.params.kaiType.toUpperCase() as "FLO" | "EAG" | "MIN";
|
||||
|
||||
if (process.env.NODE_ENV === "dev") {
|
||||
if (Environment.nodeEnv === "dev") {
|
||||
await db["kai-auth-tokens"].update(
|
||||
{
|
||||
userID: user.id,
|
||||
|
||||
@@ -12,7 +12,7 @@ const logger = CreateLogCtx(__filename);
|
||||
|
||||
let store;
|
||||
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
if (Environment.nodeEnv !== "test") {
|
||||
logger.info("Connecting ExpressSession to Redis.");
|
||||
const RedisStore = connectRedis(expressSession);
|
||||
store = new RedisStore({
|
||||
@@ -27,20 +27,20 @@ const userSessionMiddleware = expressSession({
|
||||
// append node_env onto the end of the session name
|
||||
// so we can separate tokens under the same URL.
|
||||
// say for staging.kamaitachi.xyz
|
||||
name: `${ServerConfig.TYPE}_${process.env.NODE_ENV}_session`,
|
||||
name: `${ServerConfig.TYPE}_${Environment.nodeEnv}_session`,
|
||||
secret: ServerConfig.SESSION_SECRET,
|
||||
store,
|
||||
resave: true,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
secure: process.env.NODE_ENV === "production" || ServerConfig.ENABLE_SERVER_HTTPS,
|
||||
secure: Environment.nodeEnv === "production" || ServerConfig.ENABLE_SERVER_HTTPS,
|
||||
sameSite: "lax", // Very important. Without this, we're vulnerable to CSRF!
|
||||
},
|
||||
});
|
||||
|
||||
const app: Express = express();
|
||||
|
||||
if (process.env.NODE_ENV !== "production" && ServerConfig.CLIENT_DEV_SERVER) {
|
||||
if (Environment.nodeEnv !== "production" && ServerConfig.CLIENT_DEV_SERVER) {
|
||||
logger.warn(`Enabling CORS requests from ${ServerConfig.CLIENT_DEV_SERVER}.`);
|
||||
|
||||
// Allow CORS requests from another server (since we have our dev server hosted separately).
|
||||
@@ -60,7 +60,7 @@ if (process.env.NODE_ENV !== "production" && ServerConfig.CLIENT_DEV_SERVER) {
|
||||
app.options("*", (req, res) => res.send());
|
||||
}
|
||||
} else {
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
if (Environment.nodeEnv !== "test") {
|
||||
logger.info("Enabling Helmet, as no CLIENT_DEV_SERVER was set, or we are in production.");
|
||||
}
|
||||
app.use(helmet());
|
||||
@@ -108,11 +108,11 @@ app.use("/", mainRouter);
|
||||
// this is not the case (we have a dedicated nginx box for it running in a separate process).
|
||||
// In dev, this is a pain to setup, so we can just run it locally.
|
||||
if (ServerConfig.RUN_OWN_CDN) {
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
if (Environment.nodeEnv === "production") {
|
||||
logger.warn(
|
||||
`Running OWN_CDN in production. Consider making a separate process handle your CDN for performance.`
|
||||
);
|
||||
} else if (process.env.NODE_ENV !== "test") {
|
||||
} else if (Environment.nodeEnv !== "test") {
|
||||
logger.info(`Running own CDN at ${Environment.cdnRoot}.`);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Environment } from "lib/setup/config";
|
||||
import nodeFetch from "node-fetch";
|
||||
|
||||
const fetch =
|
||||
process.env.NODE_ENV === "test"
|
||||
Environment.nodeEnv === "test"
|
||||
? () => {
|
||||
throw new Error("Cannot use real fetch inside testing env!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user