mirror of
https://github.com/tribufu/ServerManagers
synced 2026-06-01 09:42:39 +00:00
WebApi Changes
This commit is contained in:
parent
d217c08ab0
commit
33af7cf391
17 changed files with 319 additions and 309 deletions
|
|
@ -1,27 +0,0 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ServerManager.WebApplication.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[ApiVersion("1.0")]
|
||||
[Produces("application/json")]
|
||||
public class HealthController : ControllerBase
|
||||
{
|
||||
[HttpGet()]
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult Get()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,10 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ServerManager.WebApplication.Models;
|
||||
using ServerManager.WebApplication.Models.ApiVersion1;
|
||||
using ServerManager.WebApplication.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ServerManager.WebApplication.Controllers;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ServerManager.WebApplication.Models.Data;
|
||||
using ServerManager.WebApplication.Services;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions;
|
||||
|
||||
public static class ServerQueryExtensions
|
||||
{
|
||||
public static IServiceCollection AddServerQueryServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var settings = configuration.GetSectionAs<ServerQuerySettings>();
|
||||
services.AddSingleton(settings);
|
||||
|
||||
services.AddScoped<IServerQueryService, QueryMasterService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static T GetSectionAs<T>(this IConfiguration configuration, string sectionName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sectionName))
|
||||
sectionName = typeof(T).Name;
|
||||
|
||||
return configuration.GetSection(sectionName).Get<T>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions
|
||||
{
|
||||
public class SwaggerDefaultValues : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var apiDescription = context.ApiDescription;
|
||||
|
||||
operation.Deprecated |= apiDescription.IsDeprecated();
|
||||
|
||||
foreach (var responseType in context.ApiDescription.SupportedResponseTypes)
|
||||
{
|
||||
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
|
||||
var response = operation.Responses[responseKey];
|
||||
|
||||
foreach (var contentType in response.Content.Keys)
|
||||
{
|
||||
if (!responseType.ApiResponseFormats.Any(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);
|
||||
|
||||
if (parameter.Description is null)
|
||||
{
|
||||
parameter.Description = description.ModelMetadata?.Description;
|
||||
}
|
||||
|
||||
if (parameter.Schema.Default is null && description.DefaultValue is not null)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(description.DefaultValue, description.ModelMetadata.ModelType);
|
||||
parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json);
|
||||
}
|
||||
|
||||
parameter.Required |= description.IsRequired;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace ServerManager.WebApplication.Middleware;
|
||||
|
||||
public class SwaggerDefaultValues : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var apiDescription = context.ApiDescription;
|
||||
|
||||
operation.Deprecated |= apiDescription.IsDeprecated();
|
||||
|
||||
foreach (var responseType in context.ApiDescription.SupportedResponseTypes)
|
||||
{
|
||||
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
|
||||
var response = operation.Responses[responseKey];
|
||||
|
||||
foreach (var contentType in response.Content.Keys)
|
||||
{
|
||||
if (!responseType.ApiResponseFormats.Any(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);
|
||||
|
||||
if (parameter.Description is null)
|
||||
{
|
||||
parameter.Description = description.ModelMetadata?.Description;
|
||||
}
|
||||
|
||||
if (parameter.Schema.Default is null && description.DefaultValue is not null)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(description.DefaultValue, description.ModelMetadata.ModelType);
|
||||
parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json);
|
||||
}
|
||||
|
||||
parameter.Required |= description.IsRequired;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,15 +2,14 @@
|
|||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ServerManager.WebApplication.Models.ApiVersion1
|
||||
namespace ServerManager.WebApplication.Models.ApiVersion1;
|
||||
|
||||
public class ErrorResponse
|
||||
{
|
||||
public class ErrorResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// List of errors.
|
||||
/// </summary>
|
||||
[Required]
|
||||
[Description("List of errors.")]
|
||||
public ICollection<string> Errors { get; set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ServerManager.WebApplication.Models.ApiVersion1
|
||||
namespace ServerManager.WebApplication.Models.ApiVersion1;
|
||||
|
||||
public class ServerStatusResponse
|
||||
{
|
||||
public class ServerStatusResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// True if the server is available; otherwise false.
|
||||
/// </summary>
|
||||
[Required]
|
||||
[Description("True if the server is available; otherwise false.")]
|
||||
public string Available { get; set; } = false.ToString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ServerManager.WebApplication.Models.Data
|
||||
namespace ServerManager.WebApplication.Models.Data;
|
||||
|
||||
[DataContract]
|
||||
public class ManagerCode
|
||||
{
|
||||
[DataContract]
|
||||
public class ManagerCode
|
||||
{
|
||||
[DataMember]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
[DataMember]
|
||||
public string Code { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace ServerManager.WebApplication.Models.Data
|
||||
{
|
||||
public class ServerQuerySettings
|
||||
{
|
||||
public List<ManagerCode> ManagerCodes { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,10 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ServerManager.WebApplication.Models
|
||||
namespace ServerManager.WebApplication.Models;
|
||||
|
||||
public class ServerManagerApiException : Exception
|
||||
{
|
||||
public class ServerManagerApiException : Exception
|
||||
{
|
||||
public ServerManagerApiException() : base()
|
||||
{ }
|
||||
|
||||
|
|
@ -27,5 +27,4 @@ namespace ServerManager.WebApplication.Models
|
|||
public int StatusCode { get; private set; } = 0;
|
||||
|
||||
public ICollection<string> Messages { get; private set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace ServerManager.WebApplication
|
||||
namespace ServerManager.WebApplication;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
|
|
@ -16,5 +16,4 @@ namespace ServerManager.WebApplication
|
|||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
namespace ServerManager.WebApplication.Services
|
||||
namespace ServerManager.WebApplication.Services;
|
||||
|
||||
public interface IServerQueryService
|
||||
{
|
||||
public interface IServerQueryService
|
||||
{
|
||||
bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,21 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using QueryMaster;
|
||||
using ServerManager.WebApplication.Models;
|
||||
using ServerManager.WebApplication.Models.Data;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using QueryMaster;
|
||||
using ServerManager.WebApplication.Models;
|
||||
using ServerManager.WebApplication.Models.Data;
|
||||
|
||||
namespace ServerManager.WebApplication.Services
|
||||
namespace ServerManager.WebApplication.Services;
|
||||
|
||||
public class QueryMasterService : IServerQueryService
|
||||
{
|
||||
public class QueryMasterService : IServerQueryService
|
||||
{
|
||||
internal const string CONFIG_MANAGERCODES = "ManagerCodes";
|
||||
private readonly ServerQuerySettings _settings;
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public QueryMasterService(IConfiguration configuration)
|
||||
public QueryMasterService(ServerQuerySettings settings)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port)
|
||||
|
|
@ -28,9 +25,7 @@ namespace ServerManager.WebApplication.Services
|
|||
try
|
||||
{
|
||||
using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port);
|
||||
|
||||
var serverInfo = server.GetInfo();
|
||||
return serverInfo != null;
|
||||
return server.GetInfo() != null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
@ -48,7 +43,7 @@ namespace ServerManager.WebApplication.Services
|
|||
}
|
||||
else
|
||||
{
|
||||
var managerCodes = _configuration.GetSection(CONFIG_MANAGERCODES).Get<List<ManagerCode>>() ?? new List<ManagerCode>();
|
||||
var managerCodes = _settings.ManagerCodes ?? new List<ManagerCode>();
|
||||
if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
errors.Add("Manager code is invalid.");
|
||||
|
|
@ -74,5 +69,4 @@ namespace ServerManager.WebApplication.Services
|
|||
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
|
|
@ -7,12 +8,12 @@ using Microsoft.Extensions.Configuration;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ServerManager.WebApplication.Extensions;
|
||||
using ServerManager.WebApplication.Services;
|
||||
using ServerManager.WebApplication.Middleware;
|
||||
|
||||
namespace ServerManager.WebApplication
|
||||
namespace ServerManager.WebApplication;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
|
|
@ -48,12 +49,14 @@ namespace ServerManager.WebApplication
|
|||
o.GroupNameFormat = "'v'VVV";
|
||||
});
|
||||
|
||||
services.AddScoped<IServerQueryService, QueryMasterService>();
|
||||
services.AddServerQueryServices(Configuration);
|
||||
|
||||
services.AddSwaggerGen(o =>
|
||||
{
|
||||
o.OperationFilter<SwaggerDefaultValues>();
|
||||
});
|
||||
|
||||
services.AddHealthChecks();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
@ -64,6 +67,7 @@ namespace ServerManager.WebApplication
|
|||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
|
||||
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
|
||||
if (enableSwagger)
|
||||
{
|
||||
|
|
@ -95,7 +99,10 @@ namespace ServerManager.WebApplication
|
|||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHealthChecks("/api/health", new HealthCheckOptions()
|
||||
{
|
||||
AllowCachingResponses = false
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,13 +7,15 @@
|
|||
}
|
||||
},
|
||||
|
||||
"EnableSwagger": false,
|
||||
"EnableSwagger": true,
|
||||
"SwaggerRoutePrefix": "",
|
||||
|
||||
"ServerQuerySettings": {
|
||||
"ManagerCodes": [
|
||||
{
|
||||
"Name": "Unknown",
|
||||
"Code": "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"EnableSwagger": false,
|
||||
"SwaggerRoutePrefix": "",
|
||||
|
||||
"ServerQuerySettings": {
|
||||
"ManagerCodes": [
|
||||
{
|
||||
"Name": "Unknown",
|
||||
|
|
@ -16,4 +17,5 @@
|
|||
"Code": "03F9106D-2B7B-411A-B533-FB641C44218D"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue