Skip to content

Commit

Permalink
Refactor Config API
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Sep 28, 2024
1 parent 24dd584 commit 1236ca6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 114 deletions.
150 changes: 36 additions & 114 deletions src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AgileConfig.Server.Apisite.Filters;
using AgileConfig.Server.Apisite.Metrics;
using AgileConfig.Server.Apisite.Models;
using AgileConfig.Server.Apisite.Models.Mapping;
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
Expand All @@ -21,26 +22,23 @@ public class ConfigController : Controller
{
private readonly IConfigService _configService;
private readonly IAppService _appService;
private readonly IUserService _userService;
private readonly IMemoryCache _cacheMemory;
private readonly ITinyEventBus _tinyEventBus;
private readonly IMeterService _meterService;
private readonly Controllers.ConfigController _configController;

public ConfigController(
IConfigService configService,
IAppService appService,
IUserService userService,
IMemoryCache cacheMemory,
ITinyEventBus tinyEventBus,
IMeterService meterService
IMeterService meterService,
Controllers.ConfigController configController
)
{
_configService = configService;
_appService = appService;
_userService = userService;
_cacheMemory = cacheMemory;
_tinyEventBus = tinyEventBus;
_meterService = meterService;
_configController = configController;
}

/// <summary>
Expand All @@ -52,41 +50,26 @@ IMeterService meterService
/// <returns></returns>
[TypeFilter(typeof(AppBasicAuthenticationAttribute))]
[HttpGet("app/{appId}")]
public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [FromQuery] string env)
public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [FromQuery] EnvString env)
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

ISettingService.IfEnvEmptySetDefault(ref env);
ArgumentException.ThrowIfNullOrEmpty(appId);

var app = await _appService.GetAsync(appId);
if (!app.Enabled)
{
return NotFound();
}

var cacheKey = $"ConfigController_APPCONFIG_{appId}_{env}";
var cacheKey = $"ConfigController_AppConfig_{appId}_{env.Value}";
List<ApiConfigVM> configs = null;
_cacheMemory?.TryGetValue(cacheKey, out configs);
if (configs != null)
{
return configs;
}

var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env);
var vms = appConfigs.Select(c =>
{
return new ApiConfigVM()
{
Id = c.Id,
AppId = c.AppId,
Group = c.Group,
Key = c.Key,
Value = c.Value,
Status = c.Status,
OnlineStatus = c.OnlineStatus,
EditStatus = c.EditStatus
};
}).ToList();
var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env.Value);
var vms = appConfigs.Select(x => x.ToApiConfigVM()).ToList();

//增加5s的缓存,防止同一个app同时启动造成db的压力过大
var cacheOp = new MemoryCacheEntryOptions()
Expand All @@ -106,26 +89,13 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [F
/// <returns></returns>
[TypeFilter(typeof(AdmBasicAuthenticationAttribute))]
[HttpGet()]
public async Task<ActionResult<List<ApiConfigVM>>> GetConfigs(string appId, string env)
public async Task<ActionResult<List<ApiConfigVM>>> GetConfigs(string appId, [FromQuery] EnvString env)
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

ISettingService.IfEnvEmptySetDefault(ref env);
ArgumentException.ThrowIfNullOrEmpty(appId);

var configs = await _configService.GetByAppIdAsync(appId, env);
var configs = await _configService.GetByAppIdAsync(appId, env.Value);

return configs.Select(config => new ApiConfigVM()
{
Id = config.Id,
AppId = config.AppId,
Group = config.Group,
Key = config.Key,
Value = config.Value,
Status = config.Status,
Description = config.Description,
OnlineStatus = config.OnlineStatus,
EditStatus = config.EditStatus
}).ToList();
return configs.Select(x => x.ToApiConfigVM()).ToList();
}

/// <summary>
Expand All @@ -136,30 +106,17 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetConfigs(string appId, stri
/// <returns></returns>
[TypeFilter(typeof(AdmBasicAuthenticationAttribute))]
[HttpGet("{id}")]
public async Task<ActionResult<ApiConfigVM>> GetConfig(string id, string env)
public async Task<ActionResult<ApiConfigVM>> GetConfig(string id, [FromQuery] EnvString env)
{
ArgumentNullException.ThrowIfNullOrEmpty(id);

ISettingService.IfEnvEmptySetDefault(ref env);
ArgumentException.ThrowIfNullOrEmpty(id);

var config = await _configService.GetAsync(id, env);
var config = await _configService.GetAsync(id, env.Value);
if (config == null || config.Status == ConfigStatus.Deleted)
{
return NotFound();
}

return new ApiConfigVM()
{
Id = config.Id,
AppId = config.AppId,
Group = config.Group,
Key = config.Key,
Value = config.Value,
Status = config.Status,
Description = config.Description,
OnlineStatus = config.OnlineStatus,
EditStatus = config.EditStatus
};
return config.ToApiConfigVM();
}

/// <summary>
Expand All @@ -185,35 +142,21 @@ public async Task<IActionResult> Add([FromBody] ApiConfigVM model, EnvString env
});
}

var ctrl = new Controllers.ConfigController(
_configService,
_appService,
_userService,
_tinyEventBus
);
ctrl.ControllerContext.HttpContext = HttpContext;
_configController.ControllerContext.HttpContext = HttpContext;

var result = (await ctrl.Add(new ConfigVM()
{
Id = model.Id,
AppId = model.AppId,
Group = model.Group,
Key = model.Key,
Value = model.Value,
Description = model.Description
}, env)) as JsonResult;
var result = (await _configController.Add(model.ToConfigVM(), env)) as JsonResult;

dynamic obj = result.Value;
dynamic obj = result?.Value;

if (obj.success == true)
if (obj?.success == true)
{
return Created("/api/config/" + obj.data.Id, "");
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand All @@ -240,35 +183,20 @@ public async Task<IActionResult> Edit(string id, [FromBody] ApiConfigVM model, E
});
}

var ctrl = new Controllers.ConfigController(
_configService,
_appService,
_userService,
_tinyEventBus
);
ctrl.ControllerContext.HttpContext = HttpContext;

_configController.ControllerContext.HttpContext = HttpContext;
model.Id = id;
var result = (await ctrl.Edit(new ConfigVM()
{
Id = model.Id,
AppId = model.AppId,
Group = model.Group,
Key = model.Key,
Value = model.Value,
Description = model.Description
}, env)) as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
var result = (await _configController.Edit(model.ToConfigVM(), env)) as JsonResult;

dynamic obj = result?.Value;
if (obj?.success == true)
{
return Ok();
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand All @@ -284,26 +212,20 @@ public async Task<IActionResult> Edit(string id, [FromBody] ApiConfigVM model, E
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(string id, EnvString env)
{
var ctrl = new Controllers.ConfigController(
_configService,
_appService,
_userService,
_tinyEventBus
);
ctrl.ControllerContext.HttpContext = HttpContext;

var result = (await ctrl.Delete(id, env)) as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
_configController.ControllerContext.HttpContext = HttpContext;

var result = (await _configController.Delete(id, env)) as JsonResult;

dynamic obj = result?.Value;
if (obj?.success == true)
{
return NoContent();
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,25 @@ public class ApiConfigVM : IAppIdModel
/// </summary>
public string Description { get; set; }
}

public static class ApiConfigVMExtension
{
public static ConfigVM ToConfigVM(this ApiConfigVM model)
{
if (model is null)
{
return null;
}

return new ConfigVM()
{
Id = model.Id,
AppId = model.AppId,
Group = model.Group,
Key = model.Key,
Value = model.Value,
Description = model.Description
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,29 @@ public static ApiServiceInfoVM ToApiServiceInfoVM(this ServiceInfo serviceInfo)
}
}

public static class ConfigExtension
{
public static ApiConfigVM ToApiConfigVM(this Config config)
{
if (config == null)
{
return null;
}

var vm = new ApiConfigVM()
{
Id = config.Id,
AppId = config.AppId,
Group = config.Group,
Key = config.Key,
Value = config.Value,
Status = config.Status,
OnlineStatus = config.OnlineStatus,
EditStatus = config.EditStatus
};

return vm;
}
}

}

0 comments on commit 1236ca6

Please sign in to comment.