# INI 設定プロバイダー

Source: https://docs.sisk-framework.org/ja/docs/extensions/ini-configuration.html

Sisk には JSON 以外の起動設定を取得する方法があります。実際、[IConfigurationReader](https://docs.sisk-framework.org/api/Sisk.Core.Http.Hosting.IConfigurationReader.md) を実装したパイプラインであれば、[PortableConfigurationBuilder.WithConfigurationPipeline](https://docs.sisk-framework.org/api/Sisk.Core.Http.Hosting.PortableConfigurationBuilder.md) と組み合わせて使用でき、任意のファイル形式からサーバー設定を読み取ることができます。

[Sisk.IniConfiguration](https://www.nuget.org/packages/Sisk.IniConfiguration/) パッケージは、一般的な構文エラーで例外をスローせず、シンプルな設定構文を持つストリームベースの INI ファイルリーダーを提供します。このパッケージは Sisk フレームワークの外部でも使用でき、効率的な INI ドキュメントリーダーが必要なプロジェクトに柔軟性を提供します。

## インストール

パッケージをインストールするには、次のコマンドから始めます。

```bash
$ dotnet add package Sisk.IniConfiguration
```

コアパッケージもインストールできます。このパッケージは INI の [IConfigurationReader](https://docs.sisk-framework.org/api/Sisk.Core.Http.Hosting.IConfigurationReader) や Sisk の依存関係を含まず、INI シリアライザーだけが含まれます:

```bash
$ dotnet add package Sisk.IniConfiguration.Core
```

メインパッケージを使用すると、以下の例のようにコード内で利用できます。

```cs
class Program
{
    static HttpServerHostContext Host = null!;

    static void Main(string[] args)
    {
        Host = HttpServer.CreateBuilder()
            .UsePortableConfiguration(config =>
            {
                config.WithConfigFile("app.ini", createIfDontExists: true);
                
                // IniConfigurationReader 設定リーダーを使用します
                config.WithConfigurationPipeline<IniConfigurationReader>();
            })
            .UseRouter(r =>
            {
                r.MapGet("/", SayHello);
            })
            .Build();
        
        Host.Start();
    }

    static HttpResponse SayHello(HttpRequest request)
    {
        string? name = Host.Parameters["name"] ?? "world";
        return new HttpResponse($"Hello, {name}!");
    }
}
```

上記のコードは、プロセスのカレントディレクトリ (CurrentDirectory) にある app.ini ファイルを探します。INI ファイルの例は以下の通りです:

```ini
[Server]
# 複数のリッスンアドレスをサポートしています
Listen = http://localhost:5552/
Listen = http://localhost:5553/
ThrowExceptions = false
AccessLogsStream = console

[Cors]
AllowMethods = GET, POST
AllowHeaders = Content-Type, Authorization
AllowOrigin = *

[Parameters]
Name = "Kanye West"
```

## INI の方言と構文

現在の実装の方針:

- プロパティ名とセクション名は **大文字小文字を区別しません**。
- プロパティ名と値は **トリム** されます（値が引用符で囲まれている場合を除く）。
- 値はシングルクオートまたはダブルクオートで囲むことができます。クオート内に改行を含めることも可能です。
- コメントは `#` と `;` で記述できます。また、**行末コメントも許可されています**。
- プロパティは複数の値を持つことができます。

詳細については、Sisk で使用されている INI パーサーの「方針」ドキュメントが [このドキュメント](https://github.com/sisk-http/archive/blob/master/ext/ini-reader-syntax.md) にあります。

以下の INI コードを例として使用します:

```ini
One = 1
Value = this is an value
Another value = "this value
    has an line break on it"

; 以下のコードは色を示しています
[some section]
Color = Red
Color = Blue
Color = Yellow ; yellow は使用しないでください
```

次のように解析します:

```csharp
// 文字列から INI テキストを解析する
IniDocument doc = IniDocument.FromString(iniText);

// 1つの値を取得する
string? one = doc.Global.GetOne("one");
string? anotherValue = doc.Global.GetOne("another value");

// 複数の値を取得する
string[]? colors = doc.GetSection("some section")?.GetMany("color");
```

## 設定パラメータ

| セクションと名前 | 複数値を許可 | 説明 |
| ---------------- | ------------ | ---- |
| `Server.Listen` | はい | サーバーがリッスンするアドレス/ポート。 |
| `Server.Encoding` | いいえ | サーバーのデフォルトエンコーディング。 |
| `Server.MaximumContentLength` | いいえ | サーバーの最大コンテンツ長サイズ（バイト単位）。省略した場合、サーバーのデフォルト（Sisk 1.7 以降は 100 MB）が使用されます。制限なしにするには `0` を使用してください。 |
| `Server.IncludeRequestIdHeader` | いいえ | HTTP サーバーが X-Request-Id ヘッダーを送信すべきかを指定します。 |
| `Server.ThrowExceptions` | いいえ | 未処理例外をスローすべきかを指定します。 |
| `Server.AccessLogsStream` | いいえ | アクセスログの出力ストリームを指定します。 |
| `Server.ErrorsLogsStream` | いいえ | エラーログの出力ストリームを指定します。 |
| `Cors.AllowMethods` | いいえ | CORS の Allow-Methods ヘッダー値を指定します。 |
| `Cors.AllowHeaders` | いいえ | CORS の Allow-Headers ヘッダー値を指定します。 |
| `Cors.AllowOrigins` | いいえ | カンマ区切りで複数の Allow-Origin ヘッダーを指定します。詳細は [AllowOrigins](https://docs.sisk-framework.org/api/Sisk.Core.Entity.CrossOriginResourceSharingHeaders.AllowOrigins.md) を参照してください。 |
| `Cors.AllowOrigin` | いいえ | 単一の Allow-Origin ヘッダーを指定します。 |
| `Cors.ExposeHeaders` | いいえ | CORS の Expose-Headers ヘッダー値を指定します。 |
| `Cors.AllowCredentials` | いいえ | CORS の Allow-Credentials ヘッダー値を指定します。 |
| `Cors.MaxAge` | いいえ | CORS の Max-Age ヘッダー値を指定します。 |
