feat: add api versioning

This commit is contained in:
Oliver Booth 2024-02-25 15:40:58 +00:00
parent a1a7d6dd96
commit b6d3eb72fe
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
4 changed files with 15 additions and 3 deletions

View File

@ -1,17 +1,19 @@
using Asp.Versioning;
using Humanizer;
using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
using OliverBooth.Services;
namespace OliverBooth.Controllers.Api;
namespace OliverBooth.Controllers.Api.v1;
/// <summary>
/// Represents a controller for the blog API.
/// </summary>
[ApiController]
[Route("api/blog")]
[Route("api/v{version:apiVersion}/blog")]
[Produces("application/json")]
[ApiVersion(1)]
public sealed class BlogApiController : ControllerBase
{
private readonly IBlogPostService _blogPostService;

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.0.0" />
<PackageReference Include="AspNetCore.ReCaptcha" Version="1.7.0"/>
<PackageReference Include="BCrypt.Net-Core" Version="1.6.0"/>
<PackageReference Include="HtmlAgilityPack" Version="1.11.59"/>

View File

@ -1,3 +1,4 @@
using Asp.Versioning;
using AspNetCore.ReCaptcha;
using Markdig;
using OliverBooth.Data.Blog;
@ -31,6 +32,14 @@ builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
.UseSmartyPants()
.Build());
builder.Services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1);
options.ReportApiVersions = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
});
builder.Services.AddDbContextFactory<BlogContext>();
builder.Services.AddDbContextFactory<WebContext>();
builder.Services.AddHttpClient();

View File

@ -2,7 +2,7 @@ import BlogPost from "./BlogPost";
import Author from "./Author";
class API {
private static readonly BASE_URL: string = "/api";
private static readonly BASE_URL: string = "/api/v1";
private static readonly BLOG_URL: string = "/blog";
static async getBlogPostCount(): Promise<number> {