add project
This commit is contained in:
108
Models/Config.cs
Normal file
108
Models/Config.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace RedisManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents configuration for a single Redis instance, including connection details.
|
||||
/// </summary>
|
||||
public class InstanceConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom Redis configuration parameters to be applied at startup.
|
||||
/// </summary>
|
||||
public Dictionary<string, string> CustomConfig { get; set; } = new Dictionary<string, string>();
|
||||
/// <summary>
|
||||
/// Optional: Path to the redis/valkey server binary for this instance.
|
||||
/// If not set, the global default will be used.
|
||||
/// </summary>
|
||||
public string ServerBinaryPath { get; set; }
|
||||
/// <summary>
|
||||
/// The name of the Redis instance.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// The host address of the Redis instance.
|
||||
/// </summary>
|
||||
public string Host { get; set; }
|
||||
/// <summary>
|
||||
/// The port number for the Redis instance (default 6379).
|
||||
/// </summary>
|
||||
public int Port { get; set; } = 6379;
|
||||
/// <summary>
|
||||
/// The password for authenticating with the Redis instance.
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the configuration containing all Redis instances.
|
||||
/// </summary>
|
||||
public class Config
|
||||
{
|
||||
/// <summary>
|
||||
/// Optional: Default path to the redis/valkey server binary for all instances.
|
||||
/// </summary>
|
||||
public string ServerBinaryPath { get; set; }
|
||||
/// <summary>
|
||||
/// The list of Redis instance configurations.
|
||||
/// </summary>
|
||||
public List<InstanceConfig> Instances { get; set; } = new List<InstanceConfig>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides methods to load and save RedisManager configuration from disk.
|
||||
/// </summary>
|
||||
public static class ConfigManager
|
||||
{
|
||||
public static string ConfigPath = "redismanager.json";
|
||||
|
||||
/// <summary>
|
||||
/// Loads the configuration from disk, or creates a default if not present.
|
||||
/// </summary>
|
||||
/// <returns>The loaded or default Config object.</returns>
|
||||
public static Config LoadConfig(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
var defaultConfig = new Config
|
||||
{
|
||||
Instances = new List<InstanceConfig>
|
||||
{
|
||||
new InstanceConfig
|
||||
{
|
||||
Name = "default",
|
||||
Host = "localhost",
|
||||
Port = 6379
|
||||
}
|
||||
}
|
||||
};
|
||||
SaveConfig(defaultConfig, path);
|
||||
return defaultConfig;
|
||||
}
|
||||
|
||||
var json = File.ReadAllText(path);
|
||||
return JsonSerializer.Deserialize<Config>(json) ?? new Config();
|
||||
}
|
||||
|
||||
public static Config LoadConfig()
|
||||
{
|
||||
return LoadConfig(ConfigPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the provided configuration to disk as JSON.
|
||||
/// </summary>
|
||||
/// <param name="config">The configuration to save.</param>
|
||||
public static void SaveConfig(Config config, string path)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
|
||||
File.WriteAllText(path, json);
|
||||
}
|
||||
public static void SaveConfig(Config config)
|
||||
{
|
||||
SaveConfig(config, ConfigPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user