commit 852dc73ac2e67afb4fa6b9da02472b9de42f58af Author: Hadi Khazaee Asl Date: Wed Nov 18 09:11:59 2026 +0330 Initial Commit ... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a03941f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# +bin +obj diff --git a/Attributes/SwaggerDefaultValues.cs b/Attributes/SwaggerDefaultValues.cs new file mode 100644 index 0000000..f50013f --- /dev/null +++ b/Attributes/SwaggerDefaultValues.cs @@ -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 + { + /// + /// Apply Default Values ... + /// + /// + /// + 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"); + } + } + } +} \ No newline at end of file diff --git a/Constants/ConfigurationNodeNames.cs b/Constants/ConfigurationNodeNames.cs new file mode 100644 index 0000000..e1bf455 --- /dev/null +++ b/Constants/ConfigurationNodeNames.cs @@ -0,0 +1,7 @@ +namespace xApi.Constants +{ + public partial struct ConfigurationNodeNames + { + public const string ApiConfigurationNodeName = "ApiConfiguration"; + } +} \ No newline at end of file diff --git a/Constants/Params.cs b/Constants/Params.cs new file mode 100644 index 0000000..05b37e8 --- /dev/null +++ b/Constants/Params.cs @@ -0,0 +1,9 @@ +namespace xApi.Constants +{ + public struct Params + { + // + // Common Params ... + public const string ApiVersionParam = "version"; + } +} \ No newline at end of file diff --git a/Controllers/StartupController.cs b/Controllers/StartupController.cs new file mode 100644 index 0000000..ff5f15b --- /dev/null +++ b/Controllers/StartupController.cs @@ -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 ..."); + } + } +} \ No newline at end of file diff --git a/Extensions/DIHelperExtensions.cs b/Extensions/DIHelperExtensions.cs new file mode 100644 index 0000000..340aba2 --- /dev/null +++ b/Extensions/DIHelperExtensions.cs @@ -0,0 +1,57 @@ +using xApi.Models; +using xApi.Constants; + +namespace xApi.Extensions +{ + public static class DIHelperExtensions + { + /// + /// Extract Application Configuration based on appSetting file ... + /// + /// + /// + public static ApiConfiguration? GetApiConfiguration( + this IConfiguration source + ) + { + // + var section = source + .GetSection(ConfigurationNodeNames.ApiConfigurationNodeName); + var result = section.Get(); + 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() + }; + } + + // + return result; + } + + /// + /// Inject Specific Registered Service from IServiceCollection + /// + /// + /// + /// + public static T? GetRegisteredService(this IServiceCollection source) + { + // + T? result = default; + var serviceProvider = source.BuildServiceProvider(); + if (serviceProvider is not null) + { + result = serviceProvider.GetService(); + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/Extensions/ModelExtensions.cs b/Extensions/ModelExtensions.cs new file mode 100644 index 0000000..30206af --- /dev/null +++ b/Extensions/ModelExtensions.cs @@ -0,0 +1,24 @@ +using xApi.Models; + +namespace xApi.Extensions +{ + public static class ModelExtensions + { + /// + /// Validate Configuration Model ... + /// + /// + /// + 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; + } + } +} \ No newline at end of file diff --git a/Models/ApiConfiguration.cs b/Models/ApiConfiguration.cs new file mode 100644 index 0000000..bb0efd0 --- /dev/null +++ b/Models/ApiConfiguration.cs @@ -0,0 +1,49 @@ +namespace xApi.Models +{ + /// + /// Descibe Application Configuration ... + /// + public class ApiConfiguration + { + /// + /// Application Name ... + /// + public string? Name { get; set; } + + /// + /// Application Description ... + /// + public string? Description { get; set; } + + /// + /// Application Version ... + /// + public string? Version { get; set; } = "1.0"; + + /// + /// Maintainer ... + /// + public ApiConfigurationContact? Contact { get; set; } + } + + /// + /// Maintainer Contact Model ... + /// + public class ApiConfigurationContact + { + /// + /// Maintainer Name ... + /// + public string? Name { get; set; } + + /// + /// Maintainer Email ... + /// + public string? Email { get; set; } + + /// + /// Organization Web Site ... + /// + public string? Url { get; set; } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..52d5b7f --- /dev/null +++ b/Program.cs @@ -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(); + + // + // 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(); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..4f440d7 --- /dev/null +++ b/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..8a85a1a --- /dev/null +++ b/appsettings.Development.json @@ -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" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/xApi.csproj b/xApi.csproj new file mode 100644 index 0000000..363a324 --- /dev/null +++ b/xApi.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + + + + + true + true + $(NoWarn);1591 + + + + + + + + + diff --git a/xApi.http b/xApi.http new file mode 100644 index 0000000..96dd31e --- /dev/null +++ b/xApi.http @@ -0,0 +1,6 @@ +@xApi_HostAddress = http://localhost:5239 + +GET {{xApi_HostAddress}}/weatherforecast/ +Accept: application/json + +###