167 lines
4.5 KiB
C#
167 lines
4.5 KiB
C#
using Asp.Versioning;
|
|
using Asp.Versioning.ApiExplorer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.OpenApi.Models;
|
|
using PortBlog.API.DbContexts;
|
|
using PortBlog.API.Extensions;
|
|
using PortBlog.API.Middleware;
|
|
using Serilog;
|
|
using System.Reflection;
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Debug()
|
|
.WriteTo.Console()
|
|
.WriteTo.File("logs/portblog.txt", rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var urls = builder.Configuration.GetSection("Urls");
|
|
|
|
if (!string.IsNullOrEmpty(urls.Value))
|
|
{
|
|
var allowedUrlsToUse = urls.Value.Split(',');
|
|
|
|
builder.WebHost.UseUrls(allowedUrlsToUse);
|
|
}
|
|
|
|
builder.Host.UseSerilog();
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers(options =>
|
|
{
|
|
options.ReturnHttpNotAcceptable = true;
|
|
}).AddNewtonsoftJson();
|
|
|
|
builder.Services.AddProblemDetails();
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
var connectionString = builder.Configuration.GetConnectionString("PortBlogDBConnectionString");
|
|
|
|
if (builder.Configuration.GetValue<bool>("ConnectionStrings:Encryption"))
|
|
{
|
|
connectionString = builder.Configuration.DecryptConnectionString(connectionString);
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
{
|
|
throw new Exception("Connection string cannot be empty");
|
|
}
|
|
|
|
builder.Services
|
|
.AddDbContext<CvBlogContext>(dbContextOptions
|
|
=> dbContextOptions.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
|
|
|
builder.Services.AddRepositories();
|
|
builder.Services.AddServices();
|
|
|
|
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
|
|
|
|
|
// Registering API Versioning Specification services
|
|
builder.Services.AddApiVersioning(setupAction =>
|
|
{
|
|
setupAction.ReportApiVersions = true;
|
|
setupAction.AssumeDefaultVersionWhenUnspecified = true;
|
|
setupAction.DefaultApiVersion = new ApiVersion(1, 0);
|
|
}).AddMvc()
|
|
.AddApiExplorer(setupAction =>
|
|
{
|
|
setupAction.SubstituteApiVersionInUrl = true;
|
|
});
|
|
|
|
var apiVersionDescriptionProvider = builder.Services.BuildServiceProvider()
|
|
.GetRequiredService<IApiVersionDescriptionProvider>();
|
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
foreach(var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
|
|
{
|
|
c.SwaggerDoc(
|
|
$"{description.GroupName}",
|
|
new()
|
|
{
|
|
Title = "Portfolio Blog API",
|
|
Version = description.ApiVersion.ToString(),
|
|
Description = "Through this API you can access candidate cv details and along with other details."
|
|
});
|
|
}
|
|
|
|
// XML Comments file for API Documentation
|
|
var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlCommentsFullPath = $"{Path.Combine(AppContext.BaseDirectory, xmlCommentsFile)}";
|
|
|
|
c.IncludeXmlComments(xmlCommentsFullPath);
|
|
|
|
// Add Security Definition to the Swagger
|
|
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
|
|
{
|
|
Description = "ApiKey must appear in header",
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Name = "XApiKey",
|
|
In = ParameterLocation.Header,
|
|
Scheme = "ApiKeyScheme"
|
|
});
|
|
|
|
var key = new OpenApiSecurityScheme()
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "ApiKey"
|
|
},
|
|
In = ParameterLocation.Header
|
|
};
|
|
|
|
var requirement = new OpenApiSecurityRequirement()
|
|
{
|
|
{key, new List<string> {} }
|
|
};
|
|
|
|
c.AddSecurityRequirement(requirement);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler();
|
|
}
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(setupAction =>
|
|
{
|
|
var descriptions = app.DescribeApiVersions();
|
|
foreach(var description in descriptions)
|
|
{
|
|
setupAction.SwaggerEndpoint(
|
|
$"/swagger/{description.GroupName}/swagger.json",
|
|
description.GroupName.ToUpperInvariant());
|
|
}
|
|
});
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseStaticFiles(new StaticFileOptions()
|
|
{
|
|
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Images")),
|
|
RequestPath = new PathString("/images")
|
|
});
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseMiddleware<ApiKeyMiddleware>();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|