Update gestione script LUA:
- cartella con script - conf x scelta - gestione script letti 1 sola volta all'avvio
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using MP.Core.Conf;
|
||||
|
||||
namespace MP.IOC.Services
|
||||
{
|
||||
public sealed class LuaScriptProvider
|
||||
{
|
||||
private readonly Dictionary<string, string> _scripts;
|
||||
|
||||
public IReadOnlyDictionary<string, string> Scripts => _scripts;
|
||||
|
||||
public LuaScriptProvider(
|
||||
IOptions<RedisScriptsConfig> cfg,
|
||||
IWebHostEnvironment env)
|
||||
{
|
||||
_scripts = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var kv in cfg.Value.Scripts)
|
||||
{
|
||||
var name = kv.Key;
|
||||
var relativePath = kv.Value;
|
||||
|
||||
var fullPath = Path.Combine(env.ContentRootPath, relativePath);
|
||||
|
||||
if (!File.Exists(fullPath))
|
||||
throw new FileNotFoundException($"Script Lua non trovato: {fullPath}");
|
||||
|
||||
var content = File.ReadAllText(fullPath);
|
||||
|
||||
_scripts[name] = content;
|
||||
}
|
||||
}
|
||||
|
||||
public string Get(string name)
|
||||
{
|
||||
if (!_scripts.TryGetValue(name, out var script))
|
||||
throw new KeyNotFoundException($"Script Lua '{name}' non trovato.");
|
||||
|
||||
return script;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user