简体   繁体   English

如何在配置阶段加载和使用与环境相关的值

[英]How to load and use environment-related values in config phase

I would like to deploy my web application to several environments. 我想将我的Web应用程序部署到多个环境中。 Using Continuous Integration I can run a task to generate a config.json for a particular environment. 使用持续集成,我可以运行一个任务来为特定环境生成config.json。 This file will contain, among others, the particular URLs to use for it. 除其他外,该文件将包含要使用的特定URL。

{
  "baseUrl": "http://www.myapp.es/",
  "baseApiUrl": "http://api.myapp.es/",
  "baseAuthUrl": "http://api.myapp.es/auth/"
}

The issue comes up when I try to set my different services through providers in the config phase. 当我尝试在配置阶段通过提供程序设置不同的服务时,就会出现问题。 Of course, services are not available yet in the phase so I cannot use $http to load that json file and set my providers correctly. 当然,该阶段尚不提供服务,因此我无法使用$ http加载该json文件并正确设置我的提供程序。

Basically I would like to do something like: 基本上我想做类似的事情:

  function config($authProvider) {
    $authProvider.baseUrl = config.baseAuthUrl;
  }

Is there a way to load those values on runtime from a file? 有没有办法在运行时从文件中加载这些值? The only thing I can think about is having that mentioned task altering this file straight away. 我唯一能想到的就是让提到的任务立即更改此文件。 However I have several modules and therefore, that would have to do in all of them which doesn´t seem right. 但是,我有几个模块,因此,在所有这些模块中都必须这样做,这似乎不正确。

You can create constants in the config of your main module: 您可以在主模块的配置中创建常量:

  1. Add $provide as a dependency in your config method 在配置方法中添加$ provide作为依赖项
  2. use the provider method to add all constants like this 使用provider方法添加所有这样的常量

    $provide.provider('BASE_API_URL', { $get: function () { return ' https://myexample.net/api/ '; } }); $ provide.provider( 'BASE_API_URL',{$得到:函数(){return '指令https://myexample.net/api/ ';}});

  3. You can use BASE_API_URL as a dependency in your services. 您可以将BASE_API_URL用作服务中的依赖项。

I hope this helps 我希望这有帮助

Optionally you can set the url depending of your environment: (可选)您可以根据您的环境设置网址:

$provide.provider('BASE_API_URL', {
                $get: function () {
                    if(window.location.hostname.toLowerCase() == 'myapp.myexample.net')
                    {
                        return 'https://myexample.net/api/' //pre-production

                    }else
                    {

                        return 'http://localhost:61132/'; //local

                    }
                }
            });

Regards! 问候!

Finally, the solution was generating an angular constants file using templating (gulp-template) through a gulp task. 最后,解决方案是通过gulp任务使用模板(gulp-template)生成一个角度常数文件。 At the end, I am using a yaml file instead a json one (which is the one generated my CI engine with the proper values for the environment I want to deploy to). 最后,我使用的是yaml文件而不是json文件(这是使用我要部署到的环境的适当值生成我的CI引擎的文件)。

Basically: 基本上:

config.yml 配置文件

  baseUrl: 'http://www.myapp.es/'
  baseApiUrl: 'http://api.myapp.es/'
  auth:
    url: 'auth/'

config.module.constants.template config.module.constants.template

(function () {
  'use strict';

  angular
    .module('app.config')
    .constant('env_variables', {
      baseUrl: '<%=baseUrl%>',
      baseApiUrl: '<%=baseApiUrl%>',
      authUrl: '<%=auth.url%>'
    });

}());

gulpfile.js gulpfile.js

gulp.task('splicing', function(done) {
  var yml = path.join(conf.paths.src, '../config/config.yml');
  var json = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
  var template = path.join(conf.paths.src, '../config/config.module.constants.template');
  var targetFile = path.join(conf.paths.src, '/app/config');

  return gulp.src(template)
    .pipe($.template(json))
    .pipe($.rename("config.module.constants.js"))
    .pipe(gulp.dest(targetFile), done);
});

Then you just inject it in the config phase you need: 然后,您只需将其注入所需的配置阶段即可:

 function config($authProvider, env_variables) {
    $authProvider.baseUrl = env_variables.baseApiUrl + env_variables.authUrl;
  }

One more benefit about using gulp for this need is that you can integrate the generation of these constants with your build, serve or watch tasks and literally, forget about doing any change from now on. 使用gulp满足此需求的另一个好处是,您可以将这些常量的生成与构建,服务或监视任务集成在一起,并且从字面上讲,从现在起就无需进行任何更改。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM