mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Dotnet 8.x Update
- updated all nugets to latest 8.x versions - linting fixes - switched the application insights to use the connection string.
This commit is contained in:
parent
5d8fe6ca90
commit
f5a96f965a
19 changed files with 64 additions and 98 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
|
@ -36,4 +37,4 @@ public class PluginController : ControllerBase
|
|||
return StatusCode(StatusCodes.Status500InternalServerError, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Asp.Versioning;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
|
@ -59,12 +59,9 @@ public class ServerController : ControllerBase
|
|||
// check for valid service
|
||||
if (_serverQueryService == null)
|
||||
{
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = new List<string>
|
||||
{
|
||||
"Server query service not available."
|
||||
}
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = ["Server query service not available."]
|
||||
};
|
||||
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
|
||||
}
|
||||
|
|
@ -74,9 +71,9 @@ public class ServerController : ControllerBase
|
|||
var stopWatch = Stopwatch.StartNew();
|
||||
|
||||
var result = _serverQueryService.CheckServerStatus(managerCode, managerVersion, ipString, port);
|
||||
var response = new ServerStatusResponse
|
||||
{
|
||||
Available = result.ToString()
|
||||
var response = new ServerStatusResponse
|
||||
{
|
||||
Available = result.ToString()
|
||||
};
|
||||
|
||||
stopWatch.Stop();
|
||||
|
|
@ -85,22 +82,19 @@ public class ServerController : ControllerBase
|
|||
}
|
||||
catch (ServerManagerApiException ex)
|
||||
{
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = ex.Messages
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = ex.Messages
|
||||
};
|
||||
return StatusCode(ex.StatusCode, response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = new List<string>
|
||||
{
|
||||
ex.Message
|
||||
}
|
||||
var response = new ErrorResponse
|
||||
{
|
||||
Errors = [ex.Message]
|
||||
};
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,8 +11,8 @@ public static class ServerQueryExtensions
|
|||
{
|
||||
var settings = configuration.GetSectionAs<ServerQuerySettings>();
|
||||
services.AddSingleton(settings);
|
||||
|
||||
|
||||
services.AddScoped<IServerQueryService, QueryMasterService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,4 +11,4 @@ public static class ServiceCollectionExtensions
|
|||
|
||||
return configuration.GetSection(sectionName).Get<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,27 +21,19 @@ public class SwaggerDefaultValues : IOperationFilter
|
|||
|
||||
foreach (var contentType in response.Content.Keys)
|
||||
{
|
||||
if (!responseType.ApiResponseFormats.Any(x => x.MediaType == contentType))
|
||||
{
|
||||
if (responseType.ApiResponseFormats.All(x => x.MediaType != contentType))
|
||||
response.Content.Remove(contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation.Parameters is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var parameter in operation.Parameters)
|
||||
{
|
||||
var description = apiDescription.ParameterDescriptions
|
||||
.First(p => p.Name == parameter.Name);
|
||||
var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
|
||||
|
||||
if (parameter.Description is null)
|
||||
{
|
||||
parameter.Description = description.ModelMetadata?.Description;
|
||||
}
|
||||
parameter.Description ??= description.ModelMetadata?.Description;
|
||||
|
||||
if (parameter.Schema.Default is null && description.DefaultValue is not null)
|
||||
{
|
||||
|
|
@ -52,4 +44,4 @@ public class SwaggerDefaultValues : IOperationFilter
|
|||
parameter.Required |= description.IsRequired;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,5 +11,5 @@ public class ErrorResponse
|
|||
/// </summary>
|
||||
[Required]
|
||||
[Description("List of errors.")]
|
||||
public ICollection<string> Errors { get; set; } = new List<string>();
|
||||
}
|
||||
public ICollection<string> Errors { get; set; } = [];
|
||||
}
|
||||
|
|
@ -11,4 +11,4 @@ public class ServerStatusResponse
|
|||
[Required]
|
||||
[Description("True if the server is available; otherwise false.")]
|
||||
public string Available { get; set; } = false.ToString();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,4 +9,4 @@ public class ManagerCode
|
|||
public string Name { get; set; } = string.Empty;
|
||||
[DataMember]
|
||||
public string Code { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,6 @@ namespace ServerManager.WebApplication.Models.Data
|
|||
{
|
||||
public class ServerQuerySettings
|
||||
{
|
||||
public List<ManagerCode> ManagerCodes { get; set; }
|
||||
public List<ManagerCode> ManagerCodes { get; set; } = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ServerManager.WebApplication.Models;
|
||||
|
||||
public class ServerManagerApiException : Exception
|
||||
{
|
||||
public ServerManagerApiException() : base()
|
||||
public ServerManagerApiException()
|
||||
{ }
|
||||
|
||||
public ServerManagerApiException(int statusCode, ICollection<string> messages) : base()
|
||||
public ServerManagerApiException(int statusCode, ICollection<string> messages)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Messages = messages;
|
||||
}
|
||||
|
||||
public ServerManagerApiException(int statusCode, ICollection<string> messages, Exception innerException) : base(null, innerException)
|
||||
public ServerManagerApiException(int statusCode, ICollection<string> messages, Exception innerException)
|
||||
: base(null, innerException)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Messages = messages;
|
||||
}
|
||||
|
||||
protected ServerManagerApiException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{ }
|
||||
|
||||
public int StatusCode { get; private set; } = 0;
|
||||
public int StatusCode { get; private set; }
|
||||
|
||||
public ICollection<string> Messages { get; private set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
|
|
@ -16,4 +16,4 @@ public class Program
|
|||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
},
|
||||
"ServerManager.WebApplication": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": "true",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
|
|
@ -28,4 +28,4 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
<ApplicationIcon>Art\favicon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
|
@ -16,14 +16,16 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\QueryMaster\QueryMaster.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
@ -3,4 +3,4 @@
|
|||
public interface IServerQueryService
|
||||
{
|
||||
bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port);
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ public class QueryMasterService : IServerQueryService
|
|||
try
|
||||
{
|
||||
using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port);
|
||||
return server.GetInfo() != null;
|
||||
return server.GetInfo() is not null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
@ -38,35 +38,23 @@ public class QueryMasterService : IServerQueryService
|
|||
var errors = new List<string>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(managerCode))
|
||||
{
|
||||
errors.Add("Manager code is required.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var managerCodes = _settings.ManagerCodes ?? new List<ManagerCode>();
|
||||
var managerCodes = _settings.ManagerCodes ?? [];
|
||||
if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
errors.Add("Manager code is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(ipString))
|
||||
{
|
||||
errors.Add("IP Address is required.");
|
||||
}
|
||||
else if (!IPAddress.TryParse(ipString, out IPAddress _))
|
||||
{
|
||||
else if (!IPAddress.TryParse(ipString, out _))
|
||||
errors.Add("IP Address is invalid.");
|
||||
}
|
||||
|
||||
if (port <= ushort.MinValue || port >= ushort.MaxValue)
|
||||
{
|
||||
if (port is <= ushort.MinValue or >= ushort.MaxValue)
|
||||
errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue}).");
|
||||
}
|
||||
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
using Asp.Versioning;
|
||||
using Asp.Versioning.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Mvc.Versioning;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
|
@ -40,13 +39,10 @@ public class Startup
|
|||
new MediaTypeApiVersionReader("Version"),
|
||||
new HeaderApiVersionReader("X-Version")
|
||||
);
|
||||
});
|
||||
|
||||
services.AddVersionedApiExplorer(o =>
|
||||
}).AddApiExplorer(options =>
|
||||
{
|
||||
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
|
||||
// note: the specified format code will format the version as "'v'major[.minor][-status]"
|
||||
o.GroupNameFormat = "'v'VVV";
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
services.AddServerQueryServices(Configuration);
|
||||
|
|
@ -64,18 +60,14 @@ public class Startup
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
|
||||
if (enableSwagger)
|
||||
{
|
||||
var swaggerRoutePrefix = Configuration.GetValue<string>("SwaggerRoutePrefix");
|
||||
if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/"))
|
||||
{
|
||||
swaggerRoutePrefix += "/";
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(o =>
|
||||
|
|
@ -105,4 +97,4 @@ public class Startup
|
|||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
}
|
||||
},
|
||||
"ApplicationInsights": {
|
||||
"InstrumentationKey": ""
|
||||
"ConnectionString": ""
|
||||
},
|
||||
|
||||
"EnableSwagger": true,
|
||||
|
|
@ -26,4 +26,4 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,4 +18,4 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,4 +12,4 @@
|
|||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue