angular的配置文件_在NestJS应用中使用Angular风格的环境配置文件

angular的配置文件

Managing different environment configurations for a Web App is an essential task. NestJS provides an out-of-box config package based on .env files. However, my personal preference is to use the Angular style environment config files. Thus, this article will describe a simple environment config solution. The solution is illustrated in a NestJS App, but it can work in any NodeJs Apps.

为Web App管理不同的环境配置是一项基本任务。 NestJS提供了一个基于.env文件的现成配置包 。 但是,我个人偏爱使用Angular风格的环境配置文件。 因此,本文将介绍一个简单的环境配置解决方案。 该解决方案在NestJS应用中进行了说明,但可以在任何NodeJs应用中使用。

默认环境配置 (Environment specific configs with default)

The config files structure is shown below. The settings.json contains base configurations with default values, and a settings.[environment].json stores the environment specific configs which can overrides the default values.

配置文件结构如下所示。 settings.json包含具有默认值的基本配置和一个设置。[environment] .json存储可以覆盖默认值的特定于环境的配置。

└──Project/src/environments/
└──settings.json
└──settings.prod.json
└──settings.dev.json

The sample settings are listed below for illustration purpose.

下面列出了示例设置,以用于说明目的。

// settings.json
{
"apiUrl": "http://default-url",
"databaseName": "testDB",
"logLevel": "info"
}// settings.dev.json
{
"apiUrl": "http://test-url"
}// settings.prod.json
{
"apiUrl": "http://production-url",
"databaseName": "productionDB"
}

设定模型 (Setting Model)

A settingModel.ts class represents the configuration model.

settingModel.ts类表示配置模型。

export class SettingModel {
databaseName: string;
apiUrl: string;
logLevel: 'info' | 'debug' | 'warning' | 'error';
}

加载配置 (Load the Configurations)

The configurations are loaded with serverSetting.ts.

配置随serverSetting.ts一起加载。

import * as prodSettings from './environments/settings.prod.json';
import * as devSettings from './environments/settings.dev.json';
import * as defaultSettings from './environments/settings.json';

In getServerSettings method, we load the environment specific settings based on NODE_ENV global variable. Please note that Object.assign is used to merge default settings with environment specific settings object. It is sufficient for this example, but may not be enough if your settings contains a nested object.

getServerSettings方法中,我们基于NODE_ENV全局变量加载特定于环境的设置。 请注意, Object.assign用于将默认设置与特定于环境的设置对象合并。 此示例就足够了,但是如果您的设置包含嵌套对象,可能还不够。

Image for post

The serverSetting.ts service is designed to load the setting only once as singleton. The complete code is listed below.

serverSetting.ts服务旨在仅一次加载设置一次。 完整的代码在下面列出。

import * as prodSettings from './environments/settings.prod.json';
import * as devSettings from './environments/settings.dev.json';
import * as defaultSettings from './environments/settings.json';
import { SettingModel } from './settingModel';


export class ServerSetting {
    private static instance: ServerSetting;
    private settings: SettingModel;


    private constructor() {
        this.settings = this.getServerSettings();
    }


    public static getInstance(): ServerSetting {
        if(!this.instance){
            this.instance = new ServerSetting();
        }
        return this.instance;
    }


    get Settings(): SettingModel{
        return this.settings;
    }


    private getServerSettings(): SettingModel{
        const defaultSetting = Object.assign(new SettingModel(), defaultSettings);
        if(process.env.NODE_ENV === 'production'){
            return Object.assign(defaultSetting, prodSettings);
        }
        return Object.assign(defaultSetting, devSettings);
    }
}

使用配置 (Using the Configurations)

The configurations can be used in App simply as below.

这些配置可以在App中简单使用,如下所示。

const settings = ServerSetting.getInstance().Settings;

When we run the App in different environment, the result is as below.

当我们在不同的环境中运行该应用程序时,结果如下。

// When run in development mode
{
apiUrl: 'http://test-url',
databaseName: 'testDB',
logLevel: 'info'
}
// When run in production mode
{
apiUrl: 'http://production-url',
databaseName: 'productionDB',
logLevel: 'info'
}

摘要 (Summary)

In Node.js, it is a common practice to use the global variable process.env, which contains the state of the environment. However, I’m not a fan of merging the application specific configurations into the already big global variable object.

在Node.js中,使用包含环境状态的全局变量process.env是一种惯例。 但是,我不喜欢将应用程序特定的配置合并到已经很大的全局变量对象中。

This solution is an alternative to create a singleton service to manage App configurations. The settings which is represented in a strong type model class, is lazy loaded. Since all configurations are stored in json files, it’s easy to transform/override the sensitive production settings via CI/CD, and the production setting file can contain placeholder only.

此解决方案是创建单例服务以管理App配置的替代方法。 在强类型模型类中表示的设置是延迟加载的。 由于所有配置都存储在json文件中,因此很容易通过CI / CD转换/覆盖敏感的生产设置,并且生产设置文件只能包含占位符。

Happy programming!

编程愉快!

翻译自: https://medium.com/javascript-in-plain-english/use-angular-style-environment-config-file-in-nestjs-app-d29aafda989c

angular的配置文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值