WebApi Changes

This commit is contained in:
Brett Hewitson 2023-03-07 20:40:37 +10:00
parent d217c08ab0
commit 33af7cf391
17 changed files with 319 additions and 309 deletions

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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>();
}
}

View file

@ -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;
}
}
}
}

View file

@ -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;
}
}
}

View file

@ -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>();
}
/// <summary>
/// List of errors.
/// </summary>
[Required]
[Description("List of errors.")]
public ICollection<string> Errors { get; set; } = new List<string>();
}

View file

@ -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();
}
/// <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();
}

View file

@ -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;
}
[DataMember]
public string Name { get; set; } = string.Empty;
[DataMember]
public string Code { get; set; } = string.Empty;
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace ServerManager.WebApplication.Models.Data
{
public class ServerQuerySettings
{
public List<ManagerCode> ManagerCodes { get; set; }
}
}

View file

@ -2,30 +2,29 @@
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()
{ }
public ServerManagerApiException(int statusCode, ICollection<string> messages) : base()
{
public ServerManagerApiException() : base()
{ }
public ServerManagerApiException(int statusCode, ICollection<string> messages) : base()
{
StatusCode = statusCode;
Messages = messages;
}
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 ICollection<string> Messages { get; private set; } = new List<string>();
StatusCode = statusCode;
Messages = messages;
}
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 ICollection<string> Messages { get; private set; } = new List<string>();
}

View file

@ -1,20 +1,19 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace ServerManager.WebApplication
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
namespace ServerManager.WebApplication;
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

View file

@ -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);
}
bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port);
}

View file

@ -1,78 +1,72 @@
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
private readonly ServerQuerySettings _settings;
public QueryMasterService(ServerQuerySettings settings)
{
internal const string CONFIG_MANAGERCODES = "ManagerCodes";
_settings = settings;
}
private readonly IConfiguration _configuration;
public bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port)
{
ValidateServerStatusRequest(managerCode, ipString, port);
public QueryMasterService(IConfiguration configuration)
try
{
_configuration = configuration;
using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port);
return server.GetInfo() != null;
}
public bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port)
catch
{
ValidateServerStatusRequest(managerCode, ipString, port);
return false;
}
}
try
{
using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port);
private void ValidateServerStatusRequest(string managerCode, string ipString, int port)
{
var errors = new List<string>();
var serverInfo = server.GetInfo();
return serverInfo != null;
}
catch
if (string.IsNullOrWhiteSpace(managerCode))
{
errors.Add("Manager code is required.");
}
else
{
var managerCodes = _settings.ManagerCodes ?? new List<ManagerCode>();
if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase)))
{
return false;
errors.Add("Manager code is invalid.");
}
}
private void ValidateServerStatusRequest(string managerCode, string ipString, int port)
if (string.IsNullOrWhiteSpace(ipString))
{
var errors = new List<string>();
errors.Add("IP Address is required.");
}
else if (!IPAddress.TryParse(ipString, out IPAddress _))
{
errors.Add("IP Address is invalid.");
}
if (string.IsNullOrWhiteSpace(managerCode))
{
errors.Add("Manager code is required.");
}
else
{
var managerCodes = _configuration.GetSection(CONFIG_MANAGERCODES).Get<List<ManagerCode>>() ?? new List<ManagerCode>();
if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase)))
{
errors.Add("Manager code is invalid.");
}
}
if (port <= ushort.MinValue || port >= ushort.MaxValue)
{
errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue}).");
}
if (string.IsNullOrWhiteSpace(ipString))
{
errors.Add("IP Address is required.");
}
else if (!IPAddress.TryParse(ipString, out IPAddress _))
{
errors.Add("IP Address is invalid.");
}
if (port <= ushort.MinValue || port >= ushort.MaxValue)
{
errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue}).");
}
if (errors.Count > 0)
{
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
}
if (errors.Count > 0)
{
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
}
}
}

View file

@ -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,95 +8,101 @@ 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)
{
public Startup(IConfiguration configuration)
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddResponseCaching();
/*
* https://github.com/Microsoft/aspnet-api-versioning/wiki
*/
services.AddApiVersioning(o =>
{
Configuration = configuration;
o.DefaultApiVersion = ApiVersion.Default;
o.AssumeDefaultVersionWhenUnspecified = true;
o.ReportApiVersions = true;
o.ApiVersionReader = ApiVersionReader.Combine(
new MediaTypeApiVersionReader("Version"),
new HeaderApiVersionReader("X-Version")
);
});
services.AddVersionedApiExplorer(o =>
{
// 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";
});
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.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
if (enableSwagger)
{
services.AddControllers();
services.AddResponseCaching();
/*
* https://github.com/Microsoft/aspnet-api-versioning/wiki
*/
services.AddApiVersioning(o =>
var swaggerRoutePrefix = Configuration.GetValue<string>("SwaggerRoutePrefix");
if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/"))
{
o.DefaultApiVersion = ApiVersion.Default;
o.AssumeDefaultVersionWhenUnspecified = true;
o.ReportApiVersions = true;
o.ApiVersionReader = ApiVersionReader.Combine(
new MediaTypeApiVersionReader("Version"),
new HeaderApiVersionReader("X-Version")
);
});
services.AddVersionedApiExplorer(o =>
{
// 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";
});
services.AddScoped<IServerQueryService, QueryMasterService>();
services.AddSwaggerGen(o =>
{
o.OperationFilter<SwaggerDefaultValues>();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
swaggerRoutePrefix += "/";
}
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
if (enableSwagger)
app.UseSwagger();
app.UseSwaggerUI(o =>
{
var swaggerRoutePrefix = Configuration.GetValue<string>("SwaggerRoutePrefix");
if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/"))
// build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions)
{
swaggerRoutePrefix += "/";
o.SwaggerEndpoint($"/{swaggerRoutePrefix}swagger/{description.GroupName}/swagger.json", $"Server Managers API {description.GroupName.ToUpperInvariant()}");
}
app.UseSwagger();
app.UseSwaggerUI(o =>
{
// build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions)
{
o.SwaggerEndpoint($"/{swaggerRoutePrefix}swagger/{description.GroupName}/swagger.json", $"Server Managers API {description.GroupName.ToUpperInvariant()}");
}
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseResponseCaching();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseResponseCaching();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/api/health", new HealthCheckOptions()
{
AllowCachingResponses = false
});
});
}
}

View file

@ -7,13 +7,15 @@
}
},
"EnableSwagger": false,
"EnableSwagger": true,
"SwaggerRoutePrefix": "",
"ManagerCodes": [
{
"Name": "Unknown",
"Code": "00000000-0000-0000-0000-000000000000"
}
]
"ServerQuerySettings": {
"ManagerCodes": [
{
"Name": "Unknown",
"Code": "00000000-0000-0000-0000-000000000000"
}
]
}
}

View file

@ -2,18 +2,20 @@
"EnableSwagger": false,
"SwaggerRoutePrefix": "",
"ManagerCodes": [
{
"Name": "Unknown",
"Code": "00000000-0000-0000-0000-000000000000"
},
{
"Name": "Ark",
"Code": "6DCE02B1-8F41-4AF8-A6EA-E2E026CAB023"
},
{
"Name": "Conan",
"Code": "03F9106D-2B7B-411A-B533-FB641C44218D"
}
]
"ServerQuerySettings": {
"ManagerCodes": [
{
"Name": "Unknown",
"Code": "00000000-0000-0000-0000-000000000000"
},
{
"Name": "Ark",
"Code": "6DCE02B1-8F41-4AF8-A6EA-E2E026CAB023"
},
{
"Name": "Conan",
"Code": "03F9106D-2B7B-411A-B533-FB641C44218D"
}
]
}
}