Files
xSaherElmAiApi/Startup.cs
T
2026-07-29 00:15:56 +03:30

245 lines
7.1 KiB
C#

using xTagService.DI;
using xHttpService.DI;
using xAiApi.Database;
using xFileService.DI;
using xDataService.DI;
using xPushService.DI;
using Newtonsoft.Json;
using System.Reflection;
using xAiApi.Extensions;
using xStringService.DI;
using xStorageService.DI;
using xCommons.Extensions;
using xDataService.Models;
using xIdentityService.DI;
using xPushService.Helpers;
using xCommons.Configurations;
using Newtonsoft.Json.Serialization;
using IdentityModel.AspNetCore.OAuth2Introspection;
namespace xAiApi
{
public class Startup
{
//
public IConfiguration Configuration { get; }
private readonly List<XDatabaseDescriptor> databases;
private readonly XAiApiDatabaseDescriptor apiDatabaseDescriptor;
public Startup(IConfiguration configuration)
{
//
Configuration = configuration;
//
// Create an Api Descriptor Class Instance ...
apiDatabaseDescriptor = new XAiApiDatabaseDescriptor();
databases = new List<XDatabaseDescriptor>
{
apiDatabaseDescriptor
};
}
public void ConfigureServices(IServiceCollection services)
{
//
// Register Validation Provider and all XCommons Module Services ...
services.AddXCommons();
//
// Register App Configuration ...
services.AddXAppConfiguration(Configuration);
var appConfiguration = services.GetRegisteredService<XAppConfiguration>();
//
// Register Allowed Origins ...
services.AddXCors(appConfiguration.AllowedOrigins);
//
// Register Swagger ...
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
services.AddXSwagger(Configuration, xmlFilePath: xmlPath);
//
// Register HttpService ...
services.AddXHttpService(Configuration);
//
// Add Api V1 Versioning ...
services.AddXApiV1Versioning();
//
// OAuthIntrospectin Http Client Handler ...
// this is for handling SSLErrors ...
services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
};
});
//
// Register Authorizations ...
services.AddXAuthorization();
//
services.AddControllers()
.AddNewtonsoftJson(x =>
{
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
x.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
//
var lifeTime = ServiceLifetime.Scoped;
var repositoryType = xDataService.Constants.XRepositoryType.EF;
//
// Register XIdentity Service ...
services.AddXIdentityService(
lifeTime: lifeTime,
configuration: Configuration
);
//
// Storage Service ...
services.AddXStorageService(Configuration);
//
// Push Service ...
services.AddXPushService(
lifeTime: lifeTime,
config: Configuration
);
//
// Preparing DataBases ...
if (databases.HasChild())
{
//
databases
.ToList()
.ForEach(database =>
{
//
// Register DataBase ...
services.AddXDatabase(
lifeTime: lifeTime,
descriptor: database,
configuration: Configuration
);
});
}
//
// Register String Service ...
services.AddXStringService<XAiApiDbContext>(
lifeTime: lifeTime,
repositoryType: repositoryType
);
//
// Register Tag Service ...
services.AddXTagService<XAiApiDbContext>(
lifeTime: lifeTime,
repositoryType: repositoryType
);
//
// Register File Service ...
services.AddXFileService<XAiApiDbContext>(
lifeTime: lifeTime,
repositoryType: repositoryType
);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//
// Configure the HTTP request pipeline.
var isDevelopmentEnvironment = false;
if (env.IsDevelopment())
{
isDevelopmentEnvironment = true;
app.UseDeveloperExceptionPage();
}
//
// Use Swagger Middleware ...
app.UseXSwagger();
//
// Force Https Redirection ...
app.UseHttpsRedirection();
//
// Using Cors ...
app.UseXCors();
//
// Using Routing ...
app.UseRouting();
//
// Use xIdentityService ...
app.UseXIdentityService();
//
// Use Authorization ...
app.UseAuthorization();
//
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//
// Use Storage Service ...
app.UseXStorageService();
//
// Use PushService ...
var pushHelper = new XPushServiceHelper();
app.UseXPushService(pushHelper);
//
// Using DataBase ...
if (databases.HasChild())
{
//
databases
.ToList()
.ForEach(database =>
{
//
app.UseXDatabase(
descriptor: database,
isDevelopmentEnvironment: isDevelopmentEnvironment
);
});
}
//
// Using String Service ...
app.UseXStringService(
isDevelopmentEnvironment: isDevelopmentEnvironment
);
//
// Using XTag Service ...
app.UseXTagService(
isDevelopmentEnvironment: isDevelopmentEnvironment
);
//
// Using File Service ...
app.UseXFileService(
isDevelopmentEnvironment: isDevelopmentEnvironment
);
}
}
}