Skip to content

Configuration

SaschaPWittwer edited this page Nov 6, 2018 · 2 revisions

Structure

Our configurations are seperated in two sections, each holding information for the dedicated part of our application. We have the "Client" section, which contains all data used by the webclient. The second section is the "Server" which contains all information used by the backend.

The structure is hierarchical, so we can use object like configurations.

Configuration via appsettings

Configuration can be done via appsettings. The appsettings must be saved with the correct environment name to be loaded correctly. For example: In production the file must be name appsettings.production.json.

The section which is relevant for our project looks like this:

"Client": {
    "IceStunUrl": "YourValue",
    "IceTurnUrl": "YourValue",
    "IceUsername": "YourValue",
    "IceCredential": "YourValue"
  },
  "Server": {
    "DatabaseConnection": "YourValue",
    "DatabaseUser": "YourValue",
    "DatabasePassword": "YourValue"
  }

Configuration via environment variables

In some scenarios, for example in docker, it is easier to configure the app via environment variables. The app is built to include all environment variables which start with the prefix "MQTTALK_". this prefix will be cut out while the environment varialbes are loaded. To set hierarchical settings use "__" as the seperator instead of ":", because it will work on each plattform.

If you want to set the "IceStunUrl" in the "Client" section, you would need to set the environment variable "MQTTALK_Client__IceStunUrl".

Access configurations in code

Backend (.Net core)

private IConfiguration _config;

// Get the configuration via dependency injection
public YourClass(IConfiguration config)
{
    _config = config;
}


public void YourMethod()
{
    // Get the value from "IceStunUrl"
    var iceStunUrl = _config.GetSection("Client").GetValue<string>("IceStunUrl", "defaultValueForFallback");
}

Frontend (Angular)

The angular app contains a service which loads the client config section from the backend on boot initialize. You can then access this service via dependency injection through out the whole app

constructor(private config: ConfigurationService) {}

private getIceStunUrl(): string {
    return this.config.getConfig().iceStunUrl;
}