From 0d9a42dd24e7d1906ff664ee894dc9a92d4cee1a Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Fri, 22 Mar 2024 02:17:42 +0100 Subject: [PATCH] swagger added --- WatchIt/Program.cs | 50 ++++++++++++++++++++++++---- WatchIt/WatchIt.csproj | 8 ++--- WatchIt/WebAPI/AccountsController.cs | 16 +++++++++ 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 WatchIt/WebAPI/AccountsController.cs diff --git a/WatchIt/Program.cs b/WatchIt/Program.cs index 474574c..3086beb 100644 --- a/WatchIt/Program.cs +++ b/WatchIt/Program.cs @@ -20,12 +20,9 @@ namespace WatchIt { _builder = WebApplication.CreateBuilder(args); - // Logging - _builder.Logging.ClearProviders(); - _builder.Logging.AddConsole(); - - // Database - _builder.Services.AddDbContext(x => x.UseNpgsql(_builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Singleton); + ConfigureLogging(); + ConfigureDatabase(); + ConfigureWebAPI(); // Add services to the container. _builder.Services.AddRazorComponents() @@ -40,12 +37,29 @@ namespace WatchIt // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } + else + { + app.UseSwagger(x => + { + x.RouteTemplate = "api/swagger/{documentname}/swagger.json"; + }); + app.UseSwaggerUI(x => + { + x.SwaggerEndpoint("/api/swagger/v1/swagger.json", "WatchIt API"); + x.RoutePrefix = "api/swagger"; + }); + } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseAntiforgery(); + app.UseAuthentication(); + app.UseAuthorization(); + + app.MapControllers(); + app.MapRazorComponents() .AddInteractiveServerRenderMode(); @@ -53,5 +67,29 @@ namespace WatchIt } #endregion + + + + #region PRIVATE METHODS + + protected static void ConfigureLogging() + { + _builder.Logging.ClearProviders(); + _builder.Logging.AddConsole(); + } + + protected static void ConfigureDatabase() + { + _builder.Services.AddDbContext(x => x.UseNpgsql(_builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Singleton); + } + + protected static void ConfigureWebAPI() + { + _builder.Services.AddControllers(); + _builder.Services.AddEndpointsApiExplorer(); + _builder.Services.AddSwaggerGen(); + } + + #endregion } } diff --git a/WatchIt/WatchIt.csproj b/WatchIt/WatchIt.csproj index df189f9..8207f5d 100644 --- a/WatchIt/WatchIt.csproj +++ b/WatchIt/WatchIt.csproj @@ -6,10 +6,6 @@ enable - - - - @@ -23,6 +19,10 @@ + + + + diff --git a/WatchIt/WebAPI/AccountsController.cs b/WatchIt/WebAPI/AccountsController.cs new file mode 100644 index 0000000..c897fdf --- /dev/null +++ b/WatchIt/WebAPI/AccountsController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace WatchIt.WebAPI +{ + [ApiController] + [Route("api/accounts")] + public class AccountsController : ControllerBase + { + [HttpPost] + [Route("create-account")] + public async Task CreateAccount() + { + } + } +}