Files
xSaherElmAiApi/Startup.cs
T
2026-04-10 17:41:01 +03:30

283 lines
8.6 KiB
C#

using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using IdentityModel.AspNetCore.OAuth2Introspection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using xCommons.Configurations;
using xCommons.Extensions;
using xHttpService.DI;
using xIdentityService.DI;
using xPushService.DI;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using xDataService.Configuration;
using xDataService.Constants;
using xDataService.DI;
using xDataService.Interfaces;
using xPushService.Helpers;
using xAiApi.Extensions;
using xAiApi.Data.Helpers;
using xAiApi.Data;
using xAiApi.Data.Seeder;
using xAiApi.AI.DI;
using xAiApi.AI.Extensions;
// using xApi.Extensions;
// using xDataHelper;
// using xDataHelper.DbSeeder;
// using xDataHelper.Helpers;
// using xFileService.Hubs;
// using xPushHelper.DI;
// using xServices.DI;
// using xServices.TermsConditions.Push;
// using xStringService.Hubs;
// using xTagService.Hubs;
namespace xAiApi
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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);
//
// Register Api Versioning ...
services.AddApiVersioning(opt =>
{
//
// Set Default Api Version ...
opt.DefaultApiVersion = new ApiVersion(1, 0);
//
// Set Routing to Default API Version, if Version unspecified ...
opt.AssumeDefaultVersionWhenUnspecified = true;
//
// Report All Available Api Versions on Response ...
opt.ReportApiVersions = true;
});
//
var lifeTime = ServiceLifetime.Scoped;
//
// 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();
});
//
// Register XPushService ...
services.AddXPushService(Configuration);
// services.AddXPushHubProvider(lifeTime);
//
// Register XIdentityService ...
services.AddXIdentityService(Configuration, lifeTime);
//
// Register DataService here ...
#region Register xDataService ...
//
// Register IXDataServiceHelper ...
services.AddSingleton<IXDataServiceHelper, XAiApiDataProviderHelper>();
//
// Prepare Db Options Builder based on Provider ...
var providerType = Configuration.GetXDbProviderType();
switch (providerType)
{
//
case XDbProviders.MySQL:
case XDbProviders.SQLite:
case XDbProviders.SQLServer:
Action<dynamic> optionsBuilder = optionsBuilder = b =>
{
b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
};
//
// Register xDataService on DI ...
services.AddXDataService<XAiApiDbContext, XAiApiDbSeeder>(
Configuration,
lifeTime,
XDbProviderConfigurations.DEFAULT_CONNECTION_NAME,
optionsBuilder
);
break;
//
case XDbProviders.MongoDB:
//
// Register xDataService on DI ...
services.AddXDataService<XAiApiDbSeeder>(
Configuration,
lifeTime,
XDbProviderConfigurations.DEFAULT_CONNECTION_NAME
);
break;
}
#endregion
//
// Register GraphQL here ...
#region Register xGraphQL ...
//
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
//
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
//
// Register XGraphQL Helper ...
services.AddSingleton<IXGraphQLHelper, XAiApiDataHelperGraphQLHelper>();
//
services.AddXGraphQL(options =>
{
//
options.EnableMetrics = true;
options.UnhandledExceptionDelegate = context =>
{
Console.WriteLine("XGraphQL Error: " + context.OriginalException.Message);
};
});
#endregion
//
// Registering Configurations ...
var xAiApiConfiguration = Configuration
.GetXAiApiConfiguration();
services.AddXAiApiConfiguration(xAiApiConfiguration);
//
// Register AI Service ...
services.AddXAIServices();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//
var withPlayground = false;
if (env.IsDevelopment())
{
//
withPlayground = true;
app.UseDeveloperExceptionPage();
}
//
// Use Swagger Middleware ...
app.UseXSwagger();
//
app.UseHttpsRedirection();
//
// Using Cors ...
app.UseXCors();
//
app.UseRouting();
//
// Use xIdentityService ...
app.UseXIdentityService();
//
app.UseAuthorization();
//
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//
#region XPushService ...
//
// var helper = new XPushServiceHelper();
//
// helper.AddHub<XTermsHub>("termsHub");
// helper.AddHub<XTagEntityHub>("tagEntityHub");
// helper.AddHub<XFileEntityHub>("fileEntityHub");
// helper.AddHub<XStringEntityHub>("stringEntityHub");
//
// app.UseXPushService(helper);
#endregion
//
// Use xDataService Middleware ...
app.UseXDataService();
//
// Use xGraphQL Middleware ...
app.UseXGraphQL(withPlayground: withPlayground);
//
// Use AI Services ...
app.UseXAIServices();
}
}
}