From 29c9c10a53e064c15011927b1a39ff771ab06ce0 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 29 May 2024 03:43:08 +0300 Subject: [PATCH] feat: add endpoint for cache --- .../Configuration/SetupController.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 43fb6c3..ed8512a 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -14,6 +14,7 @@ using Mirea.Api.Endpoint.Configuration.General; using Mirea.Api.Endpoint.Configuration.General.Settings; using MySqlConnector; using Npgsql; +using StackExchange.Redis; using System; using System.Data; @@ -139,6 +140,52 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur return SetDatabase(connectionString, DbSettings.DatabaseEnum.Sqlite); } + + [HttpPost("SetRedis")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult SetRedis([FromBody] CacheRequest request) + { + string connectionString = $"{request.Server}:{request.Port},ssl=false"; + if (request.Password != null) + connectionString += $",password={request.Password}"; + + try + { + var redis = ConnectionMultiplexer.Connect(connectionString); + redis.Close(); + + var general = GeneralConfig; + general.CacheSettings = new CacheSettings + { + ConnectionString = connectionString, + TypeDatabase = CacheSettings.CacheEnum.Redis + }; + GeneralConfig = general; + + return Ok(true); + } + catch (Exception ex) + { + throw new ControllerArgumentException("Error when connecting to Redis: " + ex.Message); + } + } + + [HttpPost("SetMemcached")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult SetMemcached() + { + var general = GeneralConfig; + general.CacheSettings = new CacheSettings + { + ConnectionString = null, + TypeDatabase = CacheSettings.CacheEnum.Memcached + }; + GeneralConfig = general; + + return Ok(true); + } }