Sisk

Service Providers

Service Providers is a way to port your Sisk application to different environments with a portable configuration file. This feature allows you to change the server’s port, parameters, and other options without having to modify the application code for each environment. This module depends on the Sisk construction syntax and can be configured through the UsePortableConfiguration method.

A configuration provider is implemented with IConfigurationProvider, which provides a configuration reader and can receive any implementation. By default, Sisk provides a JSON configuration reader, but there is also a package for INI files. You can also create your own configuration provider and register it with:

C#
using var app = HttpServer.CreateBuilder()
    .UsePortableConfiguration(config =>
    {
        config.WithConfigReader<MyConfigurationReader>();
    })
    .Build();

As mentioned earlier, the default provider is a JSON file. By default, the file name searched for is service-config.json, and it is searched in the current directory of the running process, not the executable directory.

You can choose to change the file name, as well as where Sisk should look for the configuration file, with:

C#
using Sisk.Core.Http;
using Sisk.Core.Http.Hosting;

using var app = HttpServer.CreateBuilder()
    .UsePortableConfiguration(config =>
    {
        config.WithConfigFile("config.toml",
            createIfDontExists: true,
            lookupDirectories:
                ConfigurationFileLookupDirectory.CurrentDirectory |
                ConfigurationFileLookupDirectory.AppDirectory);
    })
    .Build();

The code above will look for the config.toml file in the current directory of the running process. If not found, it will then search in the directory where the executable is located. If the file does not exist, the createIfDontExists parameter is honored, creating the file, without any content, in the last tested path (based on lookupDirectories), and an error is thrown in the console, preventing the application from initializing.

Tip

You can look at the source code of the INI configuration reader and the JSON configuration reader to understand how an IConfigurationProvider is implemented.

Reading configurations from a JSON file #

By default, Sisk provides a configuration provider that reads configurations from a JSON file. This file follows a fixed structure and is composed of the following parameters:

JSON
{
    "Server": {
        "DefaultEncoding": "UTF-8",
        "ThrowExceptions": true,
        "IncludeRequestIdHeader": true
    },
    "ListeningHost": {
        "Label": "My sisk application",
        "Ports": [
            "http://localhost:80/",
            "https://localhost:443/",  // Configuration files also support comments
        ],
        "CrossOriginResourceSharingPolicy": {
            "AllowOrigin": "*",
            "AllowOrigins": [ "*" ],   // new on 0.14
            "AllowMethods": [ "*" ],
            "AllowHeaders": [ "*" ],
            "MaxAge": 3600
        },
        "Parameters": {
            "MySqlConnection": "server=localhost;user=root;"
        }
    }
}

The parameters created from a configuration file can be accessed in the server constructor:

C#
using var app = HttpServer.CreateBuilder()
    .UsePortableConfiguration(config =>
    {
        config.WithParameters(paramCollection =>
        {
            string databaseConnection = paramCollection.GetValueOrThrow("MySqlConnection");
        });
    })
    .Build();

Each configuration reader provides a way to read the server initialization parameters. Some properties are indicated to be in the process environment instead of being defined in the configuration file, such as sensitive API data, API keys, etc.

Configuration file structure #

The JSON configuration file is composed of the following properties:

PropertyMandatoryDescription
ServerRequiredRepresents the server itself with its settings.
Server.AccessLogsStreamOptionalDefault to console. Specifies the access log output stream. Can be a filename, null or console.
Server.ErrorsLogsStreamOptionalDefault to null. Specifies the error log output stream. Can be a filename, null or console.
Server.MaximumContentLengthOptional
Server.MaximumContentLengthOptionalDefault to 0. Specifies the maximum content length in bytes. Zero means infinite.
Server.IncludeRequestIdHeaderOptionalDefault to false. Specifies if the HTTP server should send the X-Request-Id header.
Server.ThrowExceptionsOptionalDefault to true. Specifies if unhandled exceptions should be thrown. Set to false when production and true when debugging.
ListeningHostRequiredRepresents the server listening host.
ListeningHost.LabelOptionalRepresents the application label.
ListeningHost.PortsRequiredRepresents an array of strings, matching the ListeningPort syntax.
ListeningHost.CrossOriginResourceSharingPolicyOptionalSetup the CORS headers for the application.
ListeningHost.CrossOriginResourceSharingPolicy.AllowCredentialsOptionalDefaults to false. Specifies the Allow-Credentials header.
ListeningHost.CrossOriginResourceSharingPolicy.ExposeHeadersOptionalDefaults to null. This property expects an array of strings. Specifies the Expose-Headers header.
ListeningHost.CrossOriginResourceSharingPolicy.AllowOriginOptionalDefaults to null. This property expects an string. Specifies the Allow-Origin header.
ListeningHost.CrossOriginResourceSharingPolicy.AllowOriginsOptionalDefaults to null. This property expects an array of strings. Specifies multiples Allow-Origin headers. See AllowOrigins for more information.
ListeningHost.CrossOriginResourceSharingPolicy.AllowMethodsOptionalDefaults to null. This property expects an array of strings. Specifies the Allow-Methods header.
ListeningHost.CrossOriginResourceSharingPolicy.AllowHeadersOptionalDefaults to null. This property expects an array of strings. Specifies the Allow-Headers header.
ListeningHost.CrossOriginResourceSharingPolicy.MaxAgeOptionalDefaults to null. This property expects an integer. Specifies the Max-Age header in seconds.
ListeningHost.ParametersOptionalSpecifies the properties provided to the application setup method.

Type to search the documentation and the API reference.