about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
blob: 73b0d23f641bf878454f0a97a29451f7c90ae99d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using ArcaneLibs.Extensions;

namespace LibMatrix.HomeserverEmulator.Services;

public class HSEConfiguration {
    private static ILogger<HSEConfiguration> _logger;
    public static HSEConfiguration Current { get; set; }

    [RequiresUnreferencedCode("Uses reflection binding")]
    public HSEConfiguration(ILogger<HSEConfiguration> logger, IConfiguration config, HostBuilderContext host) {
        Current = this;
        _logger = logger;
        logger.LogInformation("Loading configuration for environment: {}...", host.HostingEnvironment.EnvironmentName);
        config.GetSection("HomeserverEmulator").Bind(this);
        if (StoreData) {
            DataStoragePath = ExpandPath(DataStoragePath ?? throw new NullReferenceException("DataStoragePath is not set"));
            CacheStoragePath = ExpandPath(CacheStoragePath ?? throw new NullReferenceException("CacheStoragePath is not set"));
        }

        _logger.LogInformation("Configuration loaded: {}", this.ToJson());
    }

    public string CacheStoragePath { get; set; }

    public string DataStoragePath { get; set; }

    public bool StoreData { get; set; } = true;
    
    public bool UnknownSyncTokenIsInitialSync { get; set; } = true;

    private static string ExpandPath(string path, bool retry = true) {
        _logger.LogInformation("Expanding path `{}`", path);

        if (path.StartsWith('~')) {
            path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path[1..]);
        }

        Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().OrderByDescending(x => x.Key.ToString()!.Length).ToList().ForEach(x => {
            path = path.Replace($"${x.Key}", x.Value.ToString());
        });

        _logger.LogInformation("Expanded path to `{}`", path);
        var tries = 0;
        while (retry && path.ContainsAnyOf("~$".Split())) {
            if (tries++ > 100)
                throw new Exception($"Path `{path}` contains unrecognised environment variables");
            path = ExpandPath(path, false);
        }
        
        if(path.StartsWith("./"))
            path = Path.Join(Directory.GetCurrentDirectory(), path[2..].Replace("/", Path.DirectorySeparatorChar.ToString()));

        return path;
    }
}