Initial Commit ...
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
#
|
||||||
|
bin
|
||||||
|
obj
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using xApi.Constants;
|
||||||
|
using Microsoft.OpenApi.Any;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
|
||||||
|
namespace xApi.Attributes
|
||||||
|
{
|
||||||
|
public class SwaggerDefaultValues : IOperationFilter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Apply Default Values ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="operation"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
public void Apply(
|
||||||
|
OpenApiOperation operation,
|
||||||
|
OperationFilterContext context
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Detect Version Parameter ...
|
||||||
|
var versionParam = operation.Parameters
|
||||||
|
.FirstOrDefault(p => p.Name == Params.ApiVersionParam);
|
||||||
|
if (versionParam is not null)
|
||||||
|
{
|
||||||
|
versionParam.Schema.Default = new OpenApiString("1.0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace xApi.Constants
|
||||||
|
{
|
||||||
|
public partial struct ConfigurationNodeNames
|
||||||
|
{
|
||||||
|
public const string ApiConfigurationNodeName = "ApiConfiguration";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace xApi.Constants
|
||||||
|
{
|
||||||
|
public struct Params
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Common Params ...
|
||||||
|
public const string ApiVersionParam = "version";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace xApi.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class StartupController : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet("")]
|
||||||
|
public IActionResult Get()
|
||||||
|
{
|
||||||
|
return Ok("API is running ...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using xApi.Models;
|
||||||
|
using xApi.Constants;
|
||||||
|
|
||||||
|
namespace xApi.Extensions
|
||||||
|
{
|
||||||
|
public static class DIHelperExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extract Application Configuration based on appSetting file ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ApiConfiguration? GetApiConfiguration(
|
||||||
|
this IConfiguration source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var section = source
|
||||||
|
.GetSection(ConfigurationNodeNames.ApiConfigurationNodeName);
|
||||||
|
var result = section.Get<ApiConfiguration>();
|
||||||
|
if (result is null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = new ApiConfiguration
|
||||||
|
{
|
||||||
|
Name = source[nameof(ApiConfiguration.Name)],
|
||||||
|
Version = source[nameof(ApiConfiguration.Version)],
|
||||||
|
Description = source[nameof(ApiConfiguration.Description)],
|
||||||
|
Contact = source.GetSection(nameof(ApiConfiguration.Contact)).Get<ApiConfigurationContact>()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Inject Specific Registered Service from IServiceCollection
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T? GetRegisteredService<T>(this IServiceCollection source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
T? result = default;
|
||||||
|
var serviceProvider = source.BuildServiceProvider();
|
||||||
|
if (serviceProvider is not null)
|
||||||
|
{
|
||||||
|
result = serviceProvider.GetService<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using xApi.Models;
|
||||||
|
|
||||||
|
namespace xApi.Extensions
|
||||||
|
{
|
||||||
|
public static class ModelExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Configuration Model ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(this ApiConfiguration? configuration)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = configuration != null
|
||||||
|
&& !string.IsNullOrWhiteSpace(configuration.Name)
|
||||||
|
&& !string.IsNullOrWhiteSpace(configuration.Version)
|
||||||
|
&& !string.IsNullOrWhiteSpace(configuration.Description);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
namespace xApi.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Descibe Application Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
public class ApiConfiguration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Application Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Application Description ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Application Version ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Version { get; set; } = "1.0";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maintainer ...
|
||||||
|
/// </summary>
|
||||||
|
public ApiConfigurationContact? Contact { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maintainer Contact Model ...
|
||||||
|
/// </summary>
|
||||||
|
public class ApiConfigurationContact
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maintainer Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maintainer Email ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Email { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Organization Web Site ...
|
||||||
|
/// </summary>
|
||||||
|
public string? Url { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
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();
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:61289",
|
||||||
|
"sslPort": 44380
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5239",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:5001;http://localhost:5000;https://0.0.0.0:5001",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"Name": "xRPKRMA Api",
|
||||||
|
"Description": "an API for Providing RPK Realease Manager Assistant Tools ...",
|
||||||
|
"Version": "1.0.0",
|
||||||
|
"Contact": {
|
||||||
|
"Name": "Hadi Khazaee Asl",
|
||||||
|
"Email": "hkhazaeeasl@rpk.ir",
|
||||||
|
"Url": "https://rpkavosh.ir"
|
||||||
|
},
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- For XML Documentation Support -->
|
||||||
|
<PropertyGroup>
|
||||||
|
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Dependencies -->
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user