Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup swagger #33

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Campaign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static bool TryParse(int id, out Campaign campaign)
}
public static bool TryParse(string name, out Campaign campaign)
{
campaign = Campaigns.Where(region => region.Name == name).FirstOrDefault()!;
campaign = Campaigns.Where(region => region.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is true).FirstOrDefault()!;
if (campaign is null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Continent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static bool TryParse(int id, out Continent continent)
}
public static bool TryParse(string name, out Continent continent)
{
continent = Continents.Where(continent => continent.Name == name).FirstOrDefault()!;
continent = Continents.Where(continent => continent.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is true).FirstOrDefault()!;
if (continent is null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ public static bool TryParse(int id, out Map map)
}
public static bool TryParse(string name, out Map map)
{
map = Maps.Where(map => map.Name == name).FirstOrDefault()!;
map = Maps.Where(map => map.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is true).FirstOrDefault()!;
if (map is null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ public static bool TryParse(int id, out Region region)
}
public static bool TryParse(string name, out Region region)
{
region = Regions.Where(region => region.Name == name).FirstOrDefault()!;
region = Regions.Where(region => region.Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is true).FirstOrDefault()!;
if (region is null)
{
return false;
Expand Down
31 changes: 0 additions & 31 deletions GuildWarsPartySearch/Endpoints/CharactersController.cs

This file was deleted.

93 changes: 93 additions & 0 deletions GuildWarsPartySearch/Endpoints/PartySearchController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using GuildWarsPartySearch.Server.Models.Endpoints;
using GuildWarsPartySearch.Server.Services.PartySearch;
using Microsoft.AspNetCore.Mvc;
using System.Core.Extensions;

namespace GuildWarsPartySearch.Server.Endpoints;

[Route("party-search")]
public sealed class PartySearchController : Controller
{
private readonly IPartySearchService partySearchService;

public PartySearchController(
IPartySearchService partySearchService)
{
this.partySearchService = partySearchService.ThrowIfNull();
}

[HttpGet("characters/{charName}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetByCharName(string charName)
{
var result = await this.partySearchService.GetPartySearchesByCharName(charName, this.HttpContext.RequestAborted);
return result.Switch<IActionResult>(
onSuccess: list => this.Ok(list),
onFailure: failure => failure switch
{
GetPartySearchFailure.InvalidCharName => this.BadRequest("Invalid char name"),
_ => this.Problem()
});
}

[HttpGet("campaigns/{campaign}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetByCampaign(string campaign)
{
var result = await this.partySearchService.GetPartySearchesByCampaign(campaign, this.HttpContext.RequestAborted);
return result.Switch<IActionResult>(
onSuccess: list => this.Ok(list),
onFailure: failure => failure switch
{
GetPartySearchFailure.InvalidCampaign => this.BadRequest("Invalid campaign"),
_ => this.Problem()
});
}

[HttpGet("continents/{continent}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetByContinent(string continent)
{
var result = await this.partySearchService.GetPartySearchesByContinent(continent, this.HttpContext.RequestAborted);
return result.Switch<IActionResult>(
onSuccess: list => this.Ok(list),
onFailure: failure => failure switch
{
GetPartySearchFailure.InvalidContinent => this.BadRequest("Invalid continent"),
_ => this.Problem()
});
}

[HttpGet("regions/{region}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetByRegion(string region)
{
var result = await this.partySearchService.GetPartySearchesByRegion(region, this.HttpContext.RequestAborted);
return result.Switch<IActionResult>(
onSuccess: list => this.Ok(list),
onFailure: failure => failure switch
{
GetPartySearchFailure.InvalidRegion => this.BadRequest("Invalid region"),
_ => this.Problem()
});
}

[HttpGet("maps/{map}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetByMap(string map)
{
var result = await this.partySearchService.GetPartySearchesByMap(map, this.HttpContext.RequestAborted);
return result.Switch<IActionResult>(
onSuccess: list => this.Ok(list),
onFailure: failure => failure switch
{
GetPartySearchFailure.InvalidMap => this.BadRequest("Invalid map"),
_ => this.Problem()
});
}
}
2 changes: 1 addition & 1 deletion GuildWarsPartySearch/Filters/ApiKeyProtected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GuildWarsPartySearch.Server.Filters;

public sealed class ApiKeyProtected : IActionFilter
{
private const string ApiKeyHeader = "X-ApiKey";
public const string ApiKeyHeader = "X-ApiKey";

public void OnActionExecuting(ActionExecutingContext context)
{
Expand Down
2 changes: 2 additions & 0 deletions GuildWarsPartySearch/GuildWarsPartySearch.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.8.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="SystemExtensions.NetCore" Version="1.0.1" />
Expand Down
11 changes: 9 additions & 2 deletions GuildWarsPartySearch/Launch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using AspNetCoreRateLimit;
using GuildWarsPartySearch.Server.Options;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
Expand All @@ -26,6 +26,11 @@ private static async Task Main()
var builder = WebApplication.CreateBuilder()
.SetupOptions()
.SetupHostedServices();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Guild Wars Party Search API", Version = "v1" });
c.DocumentFilter<WebSocketEndpointsDocumentFilter>();
});
builder.Services.AddSingleton(jsonOptions);
builder.Logging.SetupLogging();
builder.Services.SetupServices();
Expand Down Expand Up @@ -54,13 +59,15 @@ private static async Task Main()
}

var app = builder.Build();
app.UseIpRateLimiting()
app.UseSwagger()
.UseIpRateLimiting()
.UseWebSockets()
.UseRouting()
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
})
.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "Guild Wars Party Search API"))
.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(contentDirectory.FullName)
Expand Down
124 changes: 124 additions & 0 deletions GuildWarsPartySearch/Launch/WebSocketEndpointsDocumentFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using GuildWarsPartySearch.Server.Filters;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace GuildWarsPartySearch.Server.Launch;

public sealed class WebSocketEndpointsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Paths.Add("/party-search/live-feed",
new OpenApiPathItem
{
Summary = "Connect to WebSocket for live feed",
Description = "WebSocket endpoint for live feed.",
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{
OperationType.Get,
new OpenApiOperation()
{
Tags = new List<OpenApiTag>
{
new()
{
Name = "PartySearch"
}
},
Description = "WebSocket endpoint for live feed.",
Responses = new OpenApiResponses
{
{ "200", new OpenApiResponse(){ Description = "Success" } },
}
}
}
},
Parameters = new List<OpenApiParameter>
{
new()
{
Name = "Connection",
In = ParameterLocation.Header,
Description = "WebSocket upgrade header",
Required = true,

},
new()
{
Name = "Sec-WebSocket-Version",
In = ParameterLocation.Header,
Description = "WebSocket protocol version",
Required = true
},
new()
{
Name = "Sec-WebSocket-Key",
In = ParameterLocation.Header,
Description = "WebSocket key",
Required = true
},
}
});
swaggerDoc.Paths.Add("/party-search/update",
new OpenApiPathItem
{
Summary = "Connect to WebSocket for updates",
Description = $"WebSocket endpoint for posting party search updates. Protected by {ApiKeyProtected.ApiKeyHeader} header.",
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{
OperationType.Get,
new OpenApiOperation()
{
Tags = new List<OpenApiTag>
{
new()
{
Name = "PartySearch"
}
},
Description = $"WebSocket endpoint for posting party search updates. Protected by {ApiKeyProtected.ApiKeyHeader} header.",
Responses = new OpenApiResponses
{
{ "200", new OpenApiResponse(){ Description = "Success" } },
{ "403", new OpenApiResponse(){ Description = "Forbidden" } }
}
}
}
},
Parameters = new List<OpenApiParameter>
{
new()
{
Name = ApiKeyProtected.ApiKeyHeader,
In = ParameterLocation.Header,
Description = "Header is required in order to connect to this endpoint",
Required = true
},
new()
{
Name = "Connection",
In = ParameterLocation.Header,
Description = "WebSocket upgrade header",
Required = true,

},
new()
{
Name = "Sec-WebSocket-Version",
In = ParameterLocation.Header,
Description = "WebSocket protocol version",
Required = true
},
new()
{
Name = "Sec-WebSocket-Key",
In = ParameterLocation.Header,
Description = "WebSocket key",
Required = true
},
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ namespace GuildWarsPartySearch.Server.Services.Database;

public interface IPartySearchDatabase
{
Task<List<Server.Models.PartySearch>> GetPartySearchesByCampaign(Campaign campaign, CancellationToken cancellationToken);
Task<List<Server.Models.PartySearch>> GetPartySearchesByContinent(Continent continent, CancellationToken cancellationToken);
Task<List<Server.Models.PartySearch>> GetPartySearchesByRegion(Region region, CancellationToken cancellationToken);
Task<List<Server.Models.PartySearch>> GetPartySearchesByMap(Map map, CancellationToken cancellationToken);
Task<List<Server.Models.PartySearch>> GetPartySearchesByCharName(string charName, CancellationToken cancellationToken);
Task<List<Server.Models.PartySearch>> GetAllPartySearches(CancellationToken cancellationToken);
Task<bool> SetPartySearches(Campaign campaign, Continent continent, Region region, Map map, string district, List<PartySearchEntry> partySearch, CancellationToken cancellationToken);
Expand Down
Loading
Loading