318 lines
9.5 KiB
C#
318 lines
9.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using xAiApi.DI;
|
|
using System.Linq;
|
|
using xAiModels.DI;
|
|
using xTagService.DI;
|
|
using System.Net.Http;
|
|
using xAiApi.Database;
|
|
using xHttpService.DI;
|
|
using xPushService.DI;
|
|
using xFileService.DI;
|
|
using xDataService.DI;
|
|
using Newtonsoft.Json;
|
|
using System.Reflection;
|
|
using xAiApi.Extensions;
|
|
using xStringService.DI;
|
|
using xStorageService.DI;
|
|
using xIdentityService.DI;
|
|
using xDataService.Models;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Constants;
|
|
using xPushService.Helpers;
|
|
using xCommons.Configurations;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
|
using xAiService.DI;
|
|
|
|
namespace xAiApi
|
|
{
|
|
public class Startup
|
|
{
|
|
//
|
|
#region Props ...
|
|
public IConfiguration Configuration { get; }
|
|
private readonly List<XDatabaseDescriptor> databases;
|
|
private readonly XAiApiDatabaseDescriptor apiDatabaseDescriptor;
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor(s) ...
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
//
|
|
Configuration = configuration;
|
|
|
|
//
|
|
// Create an Api Descriptor Class Instance ...
|
|
apiDatabaseDescriptor = new XAiApiDatabaseDescriptor();
|
|
databases = new List<XDatabaseDescriptor>
|
|
{
|
|
apiDatabaseDescriptor
|
|
};
|
|
}
|
|
#endregion
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
//
|
|
// Register Validation Provider and all XCommons Module Services ...
|
|
services.AddXCommons(Configuration);
|
|
|
|
//
|
|
// 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
|
|
);
|
|
|
|
//
|
|
// Register AiApi Data Service ...
|
|
services.AddXAiDataService<XAiApiDbContext>(
|
|
lifeTime: lifeTime,
|
|
repositoryType: repositoryType
|
|
);
|
|
|
|
//
|
|
// Register XAiApi Configuration ...
|
|
services.AddXAiApiConfiguration(Configuration);
|
|
|
|
//
|
|
// Register MCP Server Provider ...
|
|
services.AddXAiMcpServerProvider();
|
|
|
|
//
|
|
// Register File Content Extractor Services ...
|
|
services.AddXAiFileContentExtractors();
|
|
|
|
//
|
|
// Register Text Chunker Service ...
|
|
services.AddXAiTextChunker();
|
|
|
|
//
|
|
// Register Chat With Documents Services (Text Chunker + In-Memory Vector Store) ...
|
|
services.AddXAiChatWithDocuments();
|
|
|
|
//
|
|
// Register XAiMcpClient ...
|
|
services.AddXAiMcpClient();
|
|
|
|
//
|
|
// Persistent RAG (Knowledge over XAiVector Table) ...
|
|
services.AddXRagService();
|
|
|
|
//
|
|
// Register Default AI Services ...
|
|
services.AddXAiServices();
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
//
|
|
// Configure the HTTP request pipeline.
|
|
// Order: Exception Handling -> Https -> Static Files -> Swagger (Dev) -> Cors
|
|
// -> Model Proxy (Terminal for /{RoutePrefix}) -> Routing -> AuthN -> AuthZ -> Endpoints ...
|
|
var isDevelopmentEnvironment = env.IsDevelopment();
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler(XAiModelsConstants.XAiErrorRoute);
|
|
app.UseHsts();
|
|
}
|
|
|
|
//
|
|
// Force Https Redirection ...
|
|
app.UseHttpsRedirection();
|
|
|
|
//
|
|
// Use Storage Service (Prepare Folders + Static Files) ...
|
|
// Must be before Routing so Static Files Short Circuit Pipeline ...
|
|
app.UseXStorageService();
|
|
|
|
//
|
|
// Use Swagger Middleware (Only in Development) ...
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseXSwagger();
|
|
}
|
|
|
|
//
|
|
// Using Cors (before Proxy so Browser Requests get CORS Headers) ...
|
|
app.UseXCors();
|
|
|
|
//
|
|
// Enable Model Proxy (Handles its own Authentication) ...
|
|
app.UseXAiModelProxy();
|
|
|
|
//
|
|
// Using Routing ...
|
|
app.UseRouting();
|
|
|
|
//
|
|
// Use xIdentityService (Authentication) ...
|
|
app.UseXIdentityService();
|
|
|
|
//
|
|
// Use Authorization ...
|
|
app.UseAuthorization();
|
|
|
|
//
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
|
|
//
|
|
// Use PushService (Legacy UseSignalR -> Registers its own Router) ...
|
|
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
|
|
);
|
|
}
|
|
}
|
|
}
|