93 lines
2.3 KiB
C#
93 lines
2.3 KiB
C#
using xApi.Extensions;
|
|
using xApi.Attributes;
|
|
using System.Reflection;
|
|
using Microsoft.OpenApi.Models;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
//
|
|
// Access Application Builder ...
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//
|
|
// Get XMl Documentation for Swagger UI Support ...
|
|
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlFilePath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
|
|
|
|
//
|
|
// Register Builder Services ...
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
//
|
|
// Register Swagger Generator ...
|
|
var appConfiguration = builder.Configuration
|
|
.GetApiConfiguration() ??
|
|
throw new InvalidDataException("Api Configuration is not Found ...");
|
|
if (!appConfiguration.IsValid())
|
|
{
|
|
throw new InvalidDataException("Api Configuration is not Valid ...");
|
|
}
|
|
|
|
//
|
|
builder.Services.AddSingleton(appConfiguration);
|
|
builder.Services.AddSwaggerGen(delegate (SwaggerGenOptions options)
|
|
{
|
|
//
|
|
// Include XM File if Provided ...
|
|
if (xmlFilePath != null && xmlFilePath.Length > 0)
|
|
{
|
|
options.IncludeXmlComments(xmlFilePath);
|
|
}
|
|
|
|
//
|
|
// Preparing Info ...
|
|
var apiDoc = new OpenApiInfo
|
|
{
|
|
Title = appConfiguration.Name,
|
|
Version = appConfiguration.Version,
|
|
Description = appConfiguration.Description
|
|
};
|
|
if (appConfiguration.Contact is not null)
|
|
{
|
|
//
|
|
apiDoc.Contact = new OpenApiContact
|
|
{
|
|
Name = appConfiguration.Contact.Name,
|
|
Email = appConfiguration.Contact.Email,
|
|
Url = !string.IsNullOrWhiteSpace(appConfiguration.Contact.Url)
|
|
? new Uri(appConfiguration.Contact.Url)
|
|
: null
|
|
};
|
|
}
|
|
options.SwaggerDoc("v1", apiDoc);
|
|
|
|
//
|
|
// Adding Default Values Provider for Swagger ...
|
|
options.OperationFilter<SwaggerDefaultValues>();
|
|
|
|
//
|
|
// TODO: Add if Required ...
|
|
// options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {});
|
|
// options.AddSecurityRequirement(new OpenApiSecurityRequirement {});
|
|
});
|
|
|
|
//
|
|
// Register Controllers ...
|
|
builder.Services.AddControllers();
|
|
|
|
//
|
|
var app = builder.Build();
|
|
|
|
//
|
|
// Configure the HTTP request pipeline ...
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
//
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
//
|
|
app.UseHttpsRedirection();
|
|
app.MapControllers();
|
|
app.Run();
|