Skip to content

Commit

Permalink
Can not get swagger to work ;(
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob-SA committed Nov 23, 2024
1 parent 44dff82 commit 96b11f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
8 changes: 5 additions & 3 deletions Backend/Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
Expand Down
11 changes: 11 additions & 0 deletions Backend/Controllers/AuctionWareController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace Backend.Controllers;

using System.ComponentModel;
using Backend.Models; // Assuming AuctionWare is in the Models namespace

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

[ApiController]
[Route("api/[controller]")]
/// <summary>
/// Controller for managing auction wares.
/// </summary>
public class AuctionWaresController : ControllerBase
{
private readonly DatabaseContext _context;
Expand All @@ -16,7 +20,14 @@ public AuctionWaresController(DatabaseContext context)
_context = context;
}

/// <summary>
/// Creates a new auction ware.
/// </summary>
/// <param name="auctionware">The auction ware to create.</param>
/// <returns>The created auction ware.</returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAuctionWare([FromBody] AuctionWare auctionware)
{
_context.AuctionWare.Add(auctionware);
Expand Down
61 changes: 29 additions & 32 deletions Backend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
builder.Services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
Expand All @@ -14,23 +20,38 @@
builder.Services.AddDbContext<DatabaseContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));




// Configure CORS to allow requests from your frontend
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("http://51.120.6.166")
.AllowAnyMethod()
.AllowAnyHeader();
builder.WithOrigins("http://51.120.6.166", "http://localhost")
.AllowAnyMethod()
.AllowAnyHeader();
});
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // Serve the Swagger UI at the app's root
});


// app.UseHttpsRedirection();

// Use CORS policy
app.UseCors("AllowSpecificOrigin");

app.UseAuthorization();

app.MapControllers();

using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
Expand All @@ -46,29 +67,5 @@
}
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // Serve the Swagger UI at the app's root
});
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

// app.UseHttpsRedirection();

// Use CORS policy
app.UseCors("AllowSpecificOrigin");

app.UseAuthorization();

app.MapControllers();

app.Run();
app.Run();

0 comments on commit 96b11f4

Please sign in to comment.