Configuration

Introduction

Spark utilizes .env files to store and control most of its configurations. For example, your apps database credentials should be stored in this file, since Spark will look for them when starting the application.

This is helpful for when you have different configuration values based on the environment the application is running in.

What about Appsettings?

All of the configurations for the Spark framework are pulled from the appsettings.json file.

The majority of the appsettings variables are filled in by the .env file. Any value in the appsettings.json file that starts with ENV_ is replaced with the variable name that follows.

For example:

{
    "Spark": {
        "App": {
            "Name": "ENV_APP_NAME"
        },
    }
}

Spark will replace the App.Name value with your APP_NAME variable in your apps .env file.

This makes changing variables simple between environments. You don’t have to create multiple appsettings.json files. Instead, you just need to remake your .env file and it’s values.

If a appsettings.json variable is prefixed with ENV_, it is advised to not change it.

Instead, update the corresponding variable in your .env file.

Setup

Because all of your configuration values end up in the appsettings.json file, they can be accessed by dependency injecting the IConfiguration interface.

@page "/"
@inject IConfiguration Config
@code {
    protected override void OnInitialized()
    {
        var value = Config.GetValue<string>("Some:Variable");
    }
}

Retrieving Environment Variables

Once injected, you can access your config values.

Config.GetValue<string>("Spark:App:Name")!;

Environment variables can be accessed through the static Spark.Library.Environment.Env class.

using Spark.Library.Environment;

var appName = Env.Get("APP_NAME");

Security

The .env file should never be committed to your apps source control. They contain sensitive information like database credentials and should be created when setting up a new environment.

The .env.example file acts as an example of what your .env file should look like. This should be commited to source control with sensitive values removed. It acts as a guide for other developers when setting up your app.