Skip to content

Commit b10e889

Browse files
author
agile.zhou
committed
Correct naming
1 parent 5e5f3bb commit b10e889

27 files changed

+93
-149
lines changed

src/AgileConfig.Server.Apisite/Appsettings.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using AgileConfig.Server.Common;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63

74
namespace AgileConfig.Server.Apisite
85
{

src/AgileConfig.Server.Apisite/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public IActionResult Echo()
110110
[AllowAnonymous]
111111
public IActionResult GetIP()
112112
{
113-
return Content(String.Join(',', IPExt.GetEndpointIp()));
113+
return Content(String.Join(',', IpExt.GetEndpointIp()));
114114
}
115115
}
116116
}

src/AgileConfig.Server.Apisite/Filters/AdmBasicAuthenticationAttribute.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
72
using AgileConfig.Server.IService;
83
using Microsoft.AspNetCore.Http;
94
using Microsoft.AspNetCore.Mvc;
@@ -16,7 +11,7 @@ namespace AgileConfig.Server.Apisite.Filters
1611
/// </summary>
1712
public class AdmBasicAuthenticationAttribute : ActionFilterAttribute
1813
{
19-
private IAdmBasicAuthService _admBasicAuthService;
14+
private readonly IAdmBasicAuthService _admBasicAuthService;
2015
public AdmBasicAuthenticationAttribute(IAdmBasicAuthService admBasicAuthService)
2116
{
2217
_admBasicAuthService = admBasicAuthService;
@@ -32,7 +27,7 @@ public override async Task OnActionExecutionAsync(ActionExecutingContext context
3227
await base.OnActionExecutionAsync(context, next);
3328
}
3429

35-
public async Task<bool> Valid(HttpRequest httpRequest)
30+
private async Task<bool> Valid(HttpRequest httpRequest)
3631
{
3732
return await _admBasicAuthService.ValidAsync(httpRequest);
3833
}

src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace AgileConfig.Server.Apisite.Filters
1111
/// </summary>
1212
public class AppBasicAuthenticationAttribute : ActionFilterAttribute
1313
{
14-
private IAppBasicAuthService _appBasicAuthService;
14+
private readonly IAppBasicAuthService _appBasicAuthService;
1515
public AppBasicAuthenticationAttribute(IAppBasicAuthService appBasicAuthService)
1616
{
1717
_appBasicAuthService = appBasicAuthService;

src/AgileConfig.Server.Apisite/Filters/ModelValidAttribute.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Text;
72
using Microsoft.AspNetCore.Mvc;
83
using Microsoft.AspNetCore.Mvc.Filters;
94

src/AgileConfig.Server.Apisite/Filters/PermissionCheckAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using AgileConfig.Server.Apisite.Models;
2-
using AgileConfig.Server.Apisite.Utilites;
32
using AgileConfig.Server.Common;
43
using AgileConfig.Server.IService;
54
using Microsoft.AspNetCore.Mvc;

src/AgileConfig.Server.Apisite/Filters/PermissionCheckByBasicAttribute.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using AgileConfig.Server.Apisite.Models;
2-
using AgileConfig.Server.Apisite.Utilites;
3-
using AgileConfig.Server.IService;
4-
using Microsoft.AspNetCore.Mvc;
1+
using AgileConfig.Server.IService;
52
using Microsoft.AspNetCore.Mvc.Filters;
6-
using System;
7-
using System.Collections.Generic;
83
using System.Linq;
94
using System.Threading.Tasks;
105

src/AgileConfig.Server.Apisite/InitService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private string GetIp()
8585
{
8686
try
8787
{
88-
var myips = IPExt.GetEndpointIp();
88+
var myips = IpExt.GetEndpointIp();
8989
_logger.LogInformation("AgileConfig node's IP " + String.Join(',', myips));
9090

9191
return myips.FirstOrDefault();

src/AgileConfig.Server.Apisite/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IService
123123
AddSwaggerMiddleWare(app);
124124
}
125125

126-
app.UseMiddleware<ReactUIMiddleware>();
126+
app.UseMiddleware<ReactUiMiddleware>();
127127

128128
app.UseCors(op =>
129129
{

src/AgileConfig.Server.Apisite/StartupExtension.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using OpenTelemetry.Metrics;
44
using System.Net.Http;
55
using OpenTelemetry.Trace;
6-
using OpenTelemetry.Logs;
76
using OpenTelemetry.Exporter;
87
using OpenTelemetry;
98
using Npgsql;
@@ -13,12 +12,12 @@ namespace AgileConfig.Server.Apisite
1312
{
1413
public static class StartupExtension
1514
{
16-
public static void AddDefaultHttpClient(this IServiceCollection services, bool isTrustSSL)
15+
public static void AddDefaultHttpClient(this IServiceCollection services, bool isTrustSsl)
1716
{
1817
services.AddHttpClient(Global.DefaultHttpClientName)
1918
.ConfigurePrimaryHttpMessageHandler(() =>
2019
{
21-
return NewMessageHandler(isTrustSSL);
20+
return NewMessageHandler(isTrustSsl);
2221
})
2322
;
2423
}
@@ -90,10 +89,7 @@ static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl)
9089
var handler = new HttpClientHandler();
9190
if (alwaysTrustSsl)
9291
{
93-
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
94-
{
95-
return true;
96-
};
92+
handler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
9793
}
9894

9995
return handler;

src/AgileConfig.Server.Apisite/UIExtension/ReactUIMiddleware.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,50 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading.Tasks;
8-
using System.Text;
98
using Microsoft.Extensions.Primitives;
109
using System.Collections.Concurrent;
1110
using AgileConfig.Server.Common;
1211
using Microsoft.AspNetCore.Hosting;
1312

1413
namespace AgileConfig.Server.Apisite.UIExtension
1514
{
16-
public class ReactUIMiddleware
15+
public class ReactUiMiddleware
1716
{
18-
private static string UiDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot/ui");
19-
private static readonly ConcurrentDictionary<string, UIFileBag> StaticFilesCache = new();
17+
private static string _uiDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot/ui");
18+
private static readonly ConcurrentDictionary<string, UiFileBag> StaticFilesCache = new();
2019
private readonly RequestDelegate _next;
2120
private readonly ILogger _logger;
22-
private readonly IWebHostEnvironment _environment;
23-
public ReactUIMiddleware(
21+
22+
public ReactUiMiddleware(
2423
RequestDelegate next,
2524
ILoggerFactory loggerFactory,
2625
IWebHostEnvironment environment
2726
)
2827
{
2928
_next = next;
3029
_logger = loggerFactory.
31-
CreateLogger<ReactUIMiddleware>();
32-
_environment = environment;
30+
CreateLogger<ReactUiMiddleware>();
3331
#if DEBUG
3432
//if debug mode, try to switch to project wwwwroot dir
35-
var projectUIPath = Path.Combine(_environment.ContentRootPath, "wwwroot/ui");
36-
if (Directory.Exists(projectUIPath))
33+
var projectUiPath = Path.Combine(environment.ContentRootPath, "wwwroot/ui");
34+
if (Directory.Exists(projectUiPath))
3735
{
38-
UiDirectory = projectUIPath;
36+
_uiDirectory = projectUiPath;
3937
}
4038
#endif
4139
}
4240

4341
private bool IsAdminConsoleMode => "true".Equals(Global.Config["adminConsole"], StringComparison.OrdinalIgnoreCase);
4442

45-
private bool ShouldHandleUIRequest(HttpContext context)
43+
private static bool ShouldHandleUiRequest(HttpContext context)
4644
{
47-
return context.Request.Path.HasValue && context.Request.Path.Value.Equals("/ui", StringComparison.OrdinalIgnoreCase);
45+
return context.Request.Path is { HasValue: true, Value: not null } && context.Request.Path.Value.Equals("/ui", StringComparison.OrdinalIgnoreCase);
4846
}
4947

50-
private bool ShouldHandleUIStaticFilesRequest(HttpContext context)
48+
private bool ShouldHandleUiStaticFilesRequest(HttpContext context)
5149
{
5250
//请求的的Referer为 0.0.0.0/ui ,以此为依据判断是否是reactui需要的静态文件
53-
if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("."))
51+
if (context.Request.Path is { HasValue: true, Value: not null } && context.Request.Path.Value.Contains('.'))
5452
{
5553
context.Request.Headers.TryGetValue("Referer", out StringValues refererValues);
5654
if (refererValues.Any())
@@ -105,14 +103,14 @@ public async Task Invoke(HttpContext context)
105103
{
106104
//handle /ui request
107105
var filePath = "";
108-
if (ShouldHandleUIRequest(context))
106+
if (ShouldHandleUiRequest(context))
109107
{
110-
filePath = UiDirectory + "/index.html";
108+
filePath = _uiDirectory + "/index.html";
111109
}
112110
//handle static files that Referer = xxx/ui
113-
if (ShouldHandleUIStaticFilesRequest(context))
111+
if (ShouldHandleUiStaticFilesRequest(context))
114112
{
115-
filePath = UiDirectory + context.Request.Path;
113+
filePath = _uiDirectory + context.Request.Path;
116114
}
117115

118116
if (string.IsNullOrEmpty(filePath))
@@ -146,7 +144,7 @@ public async Task Invoke(HttpContext context)
146144
var fileData = await File.ReadAllBytesAsync(filePath); //read file bytes
147145
var lastModified = File.GetLastWriteTime(filePath);
148146
var extType = Path.GetExtension(filePath);
149-
uiFile = new UIFileBag()
147+
uiFile = new UiFileBag()
150148
{
151149
FilePath = filePath,
152150
Data = fileData,

src/AgileConfig.Server.Apisite/UIExtension/UIFileBag.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace AgileConfig.Server.Apisite.UIExtension;
66

7-
internal class UIFileBag
7+
internal class UiFileBag
88
{
9-
private static Dictionary<string, string> _contentTypesMap = new()
9+
private static readonly Dictionary<string, string> ContentTypesMap = new()
1010
{
1111
{".html", "text/html; charset=utf-8"},
1212
{".css", "text/css; charset=utf-8"},
@@ -25,7 +25,7 @@ internal class UIFileBag
2525

2626
public string ExtType { get; init; }
2727

28-
public string ContentType => _contentTypesMap[ExtType];
28+
public string ContentType => ContentTypesMap[ExtType];
2929

3030
public DateTime LastModified { get; init; }
3131
}

src/AgileConfig.Server.Apisite/Utilites/ControllerExt.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using AgileConfig.Server.Common;
22
using AgileConfig.Server.IService;
3-
using Microsoft.AspNetCore.Http;
43
using Microsoft.AspNetCore.Mvc;
54
using System.Linq;
65
using System.Threading.Tasks;
@@ -31,15 +30,15 @@ public static string GetCurrentUserName(this Controller ctrl)
3130
/// </summary>
3231
/// <param name="ctrl"></param>
3332
/// <returns></returns>
34-
public static async Task<string> GetCurrentUserId(this Controller ctrl, IUserService userservice = null)
33+
public static async Task<string> GetCurrentUserId(this Controller ctrl, IUserService userService)
3534
{
3635
var userId = ctrl.HttpContext.GetUserIdFromClaim();
3736
if (string.IsNullOrEmpty(userId))
3837
{
3938
var result = ctrl.Request.GetUserNamePasswordFromBasicAuthorization();
4039
if (!string.IsNullOrEmpty(result.Item1))
4140
{
42-
var user =(await userservice.GetUsersByNameAsync(result.Item1)).FirstOrDefault(x=>x.Status == Data.Entity.UserStatus.Normal);
41+
var user =(await userService.GetUsersByNameAsync(result.Item1)).FirstOrDefault(x=>x.Status == Data.Entity.UserStatus.Normal);
4342
userId = user?.Id;
4443
}
4544
}

src/AgileConfig.Server.Apisite/Utilites/IPExt.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace AgileConfig.Server.Apisite.Utilites
77
{
8-
public class IPExt
8+
public class IpExt
99
{
1010
public static string[] GetEndpointIp()
1111
{
@@ -21,11 +21,15 @@ public static string[] GetEndpointIp()
2121
{
2222
var props = item.GetIPProperties();
2323
//this is ip for ipv4
24-
var firstIpV4Address = props.UnicastAddresses
24+
var firstIpV4Address = props.UnicastAddresses?
2525
.Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
2626
.Select(c => c.Address)
27-
.FirstOrDefault().ToString();
28-
ips.Add(firstIpV4Address);
27+
.FirstOrDefault()?.ToString();
28+
29+
if (!string.IsNullOrEmpty(firstIpV4Address))
30+
{
31+
ips.Add(firstIpV4Address);
32+
}
2933
}
3034
}
3135
return ips.ToArray();

src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/IMessageHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Net.WebSockets;
2-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
32
using Microsoft.AspNetCore.Http;
43

54
namespace AgileConfig.Server.Apisite.Websocket.MessageHandlers;

src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/MessageHandler.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net.Http.Json;
32
using System.Net.WebSockets;
43
using System.Text;
54
using System.Threading;
@@ -54,13 +53,10 @@ await webSocket.SendAsync(new ArraySegment<byte>(data, 0, data.Length), WebSocke
5453
}
5554
public async Task Handle(string message, HttpRequest request, WebsocketClient client)
5655
{
57-
if (message == null)
58-
{
59-
message = "";
60-
}
56+
message ??= "";
6157

6258
// "ping" is old version
63-
if (message == "ping" || message == "c:ping")
59+
if (message is "ping" or "c:ping")
6460
{
6561
//如果是ping,回复本地数据的md5版本
6662
var appId = request.Headers["appid"];

src/AgileConfig.Server.Apisite/Websocket/MessageHandlers/WebsocketMessageHandlers.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ public class WebsocketMessageHandlers
99
public WebsocketMessageHandlers(IConfigService configService, IRegisterCenterService registerCenterService,
1010
IServiceInfoService serviceInfoService)
1111
{
12-
MessageHandlers = new List<IMessageHandler>();
13-
14-
MessageHandlers.Add(new OldMessageHandler(configService));
15-
MessageHandlers.Add(new MessageHandler(configService, registerCenterService, serviceInfoService));
12+
MessageHandlers =
13+
[
14+
new OldMessageHandler(configService),
15+
new MessageHandler(configService, registerCenterService, serviceInfoService)
16+
];
1617
}
1718
}

0 commit comments

Comments
 (0)