refactor(env): centralize app environment detection
This commit is contained in:
@@ -4,8 +4,9 @@
|
|||||||
* 通过环境变量配置后端服务地址与超时时间。
|
* 通过环境变量配置后端服务地址与超时时间。
|
||||||
* 原始 Dart: lib/core/net/service_manager.dart
|
* 原始 Dart: lib/core/net/service_manager.dart
|
||||||
*/
|
*/
|
||||||
|
import { AppEnvUtil, type AppEnv } from "@/utils/app-env";
|
||||||
|
|
||||||
export type AppEnv = "development" | "test" | "production";
|
export type { AppEnv };
|
||||||
|
|
||||||
export interface ApiConfig {
|
export interface ApiConfig {
|
||||||
/** 后端 API 基础 URL(含协议) */
|
/** 后端 API 基础 URL(含协议) */
|
||||||
@@ -33,9 +34,7 @@ export class TimeoutConstants {
|
|||||||
* 解析当前环境
|
* 解析当前环境
|
||||||
*/
|
*/
|
||||||
export function getAppEnv(): AppEnv {
|
export function getAppEnv(): AppEnv {
|
||||||
const env = process.env.NEXT_PUBLIC_APP_ENV;
|
return AppEnvUtil.current;
|
||||||
if (env === "production" || env === "test") return env;
|
|
||||||
return "development";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
type GuestChatQuotaInput,
|
type GuestChatQuotaInput,
|
||||||
type GuestChatQuotaData,
|
type GuestChatQuotaData,
|
||||||
} from "@/data/schemas/chat/guest_chat_quota";
|
} from "@/data/schemas/chat/guest_chat_quota";
|
||||||
import { isProduction } from "@/utils/app-env";
|
import { AppEnvUtil } from "@/utils/app-env";
|
||||||
|
|
||||||
export class GuestChatQuota {
|
export class GuestChatQuota {
|
||||||
// 静态常量
|
// 静态常量
|
||||||
@@ -45,7 +45,7 @@ export class GuestChatQuota {
|
|||||||
* 获取当前环境的每日最大配额
|
* 获取当前环境的每日最大配额
|
||||||
*/
|
*/
|
||||||
static get threshold(): number {
|
static get threshold(): number {
|
||||||
return isProduction()
|
return AppEnvUtil.isProduction()
|
||||||
? GuestChatQuota.maxQuotaPerDay
|
? GuestChatQuota.maxQuotaPerDay
|
||||||
: GuestChatQuota.maxQuotaPerDayTest;
|
: GuestChatQuota.maxQuotaPerDayTest;
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ export class GuestChatQuota {
|
|||||||
* 获取当前环境的总配额默认值
|
* 获取当前环境的总配额默认值
|
||||||
*/
|
*/
|
||||||
static get totalQuotaDefault(): number {
|
static get totalQuotaDefault(): number {
|
||||||
return isProduction()
|
return AppEnvUtil.isProduction()
|
||||||
? GuestChatQuota.defaultTotalQuota
|
? GuestChatQuota.defaultTotalQuota
|
||||||
: GuestChatQuota.defaultTotalQuotaTest;
|
: GuestChatQuota.defaultTotalQuotaTest;
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-7
@@ -3,13 +3,34 @@
|
|||||||
*
|
*
|
||||||
* 原始 Dart: lib/utils/app_env_util.dart
|
* 原始 Dart: lib/utils/app_env_util.dart
|
||||||
*
|
*
|
||||||
* 替代 `AppEnvUtil.isDevelopment`:基于 Next.js 注入的 `process.env.NODE_ENV`。
|
* 基于 `.env*` 中的 `NEXT_PUBLIC_APP_ENV` 判断当前业务环境。
|
||||||
|
*
|
||||||
|
* 注意:该变量带 `NEXT_PUBLIC_` 前缀,可同时用于服务端和客户端代码。
|
||||||
*/
|
*/
|
||||||
export const isDevelopment = (): boolean =>
|
export type AppEnv = "development" | "test" | "production";
|
||||||
process.env.NODE_ENV !== "production";
|
|
||||||
|
|
||||||
export const isProduction = (): boolean =>
|
export class AppEnvUtil {
|
||||||
process.env.NODE_ENV === "production";
|
private constructor() {}
|
||||||
|
|
||||||
export const isTest = (): boolean =>
|
static get current(): AppEnv {
|
||||||
process.env.NODE_ENV === "test";
|
const env = process.env.NEXT_PUBLIC_APP_ENV;
|
||||||
|
if (env === "production" || env === "test") return env;
|
||||||
|
return "development";
|
||||||
|
}
|
||||||
|
|
||||||
|
static isDevelopment(): boolean {
|
||||||
|
return AppEnvUtil.current === "development";
|
||||||
|
}
|
||||||
|
|
||||||
|
static isProduction(): boolean {
|
||||||
|
return AppEnvUtil.current === "production";
|
||||||
|
}
|
||||||
|
|
||||||
|
static isTest(): boolean {
|
||||||
|
return AppEnvUtil.current === "test";
|
||||||
|
}
|
||||||
|
|
||||||
|
static canOutputLogs(): boolean {
|
||||||
|
return !AppEnvUtil.isProduction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+6
-6
@@ -1,22 +1,22 @@
|
|||||||
import pino, { type Logger as PinoLogger, type LoggerOptions } from "pino";
|
import pino, { type Logger as PinoLogger, type LoggerOptions } from "pino";
|
||||||
|
|
||||||
import { isProduction, isTest } from "./app-env";
|
import { AppEnvUtil } from "./app-env";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局 pino root logger(单例,进程级复用)。
|
* 全局 pino root logger(单例,进程级复用)。
|
||||||
*
|
*
|
||||||
* 配置驱动(基于 `app-env.ts` 的 `isProduction()` / `isTest()`):
|
* 配置驱动(基于 `app-env.ts` 的 `AppEnvUtil`):
|
||||||
* - production: info 级别 + JSON(机器可解析,接入日志平台)
|
* - production: silent(不输出日志)
|
||||||
* - test: warn 级别(减少 vitest 噪音)
|
* - test: warn 级别(减少 vitest 噪音)
|
||||||
* - development: debug 级别 + ISO 时间戳(人眼友好)
|
* - development: debug 级别 + ISO 时间戳(人眼友好)
|
||||||
*/
|
*/
|
||||||
const rootLogger: PinoLogger = pino(createLoggerOptions());
|
const rootLogger: PinoLogger = pino(createLoggerOptions());
|
||||||
|
|
||||||
function createLoggerOptions(): LoggerOptions {
|
function createLoggerOptions(): LoggerOptions {
|
||||||
if (isProduction()) {
|
if (!AppEnvUtil.canOutputLogs()) {
|
||||||
return { level: "info" };
|
return { level: "silent" };
|
||||||
}
|
}
|
||||||
if (isTest()) {
|
if (AppEnvUtil.isTest()) {
|
||||||
return { level: "warn" };
|
return { level: "warn" };
|
||||||
}
|
}
|
||||||
// development
|
// development
|
||||||
|
|||||||
Reference in New Issue
Block a user