159 lines
4.6 KiB
C#
159 lines
4.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using xAiMcpServer.DI;
|
|
using Newtonsoft.Json;
|
|
using System.Reflection;
|
|
using xLoggingService.DI;
|
|
using xIdentityService.DI;
|
|
using xCommons.Extensions;
|
|
using xAiService.Extensions;
|
|
using xCommons.Configurations;
|
|
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;
|
|
|
|
namespace xAiMcpServer
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
//
|
|
// Register MCP Handler ...
|
|
services.AddXAiMcpHandler();
|
|
|
|
//
|
|
// Register Validation Provider and all XCommons Module Services ...
|
|
services.AddXCommons(Configuration);
|
|
|
|
//
|
|
// Register App Configuration ...
|
|
services.AddXAppConfiguration(Configuration);
|
|
var appConfiguration = services.GetRegisteredService<XAppConfiguration>();
|
|
|
|
//
|
|
// Register Logging Service ...
|
|
services.AddXLoggingService(Configuration);
|
|
|
|
//
|
|
// 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);
|
|
|
|
//
|
|
// 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
|
|
};
|
|
});
|
|
|
|
//
|
|
services.AddControllers()
|
|
.AddNewtonsoftJson(x =>
|
|
{
|
|
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
x.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
});
|
|
|
|
//
|
|
var lifeTime = ServiceLifetime.Scoped;
|
|
|
|
//
|
|
// Register XIdentity Service ...
|
|
services.AddXIdentityService(
|
|
lifeTime: lifeTime,
|
|
configuration: Configuration
|
|
);
|
|
|
|
//
|
|
// Register Authorizations ...
|
|
services.AddXAuthorization();
|
|
|
|
//
|
|
// Register MCP Tools in DI ...
|
|
services.AddXAiMcpTools(Configuration);
|
|
}
|
|
|
|
public void Configure(
|
|
IApplicationBuilder app,
|
|
IWebHostEnvironment env
|
|
)
|
|
{
|
|
//
|
|
// Configure the HTTP request pipeline ...
|
|
var isDevelopmentEnvironment = env.IsDevelopment();
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseHsts();
|
|
}
|
|
|
|
//
|
|
// Setting Environment in App Confiuration ...
|
|
app.AddEnvironmentToXAppConfiguration(env.EnvironmentName);
|
|
|
|
//
|
|
// Force Https Redirection ...
|
|
app.UseHttpsRedirection();
|
|
|
|
//
|
|
// Use Swagger Middleware (Only in Development) ...
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseXSwagger();
|
|
}
|
|
|
|
//
|
|
// Using Cors (before Proxy so Browser Requests get CORS Headers) ...
|
|
app.UseXCors();
|
|
|
|
//
|
|
// Using Routing ...
|
|
app.UseRouting();
|
|
|
|
//
|
|
// Use xIdentityService (Authentication) ...
|
|
app.UseXIdentityService();
|
|
|
|
//
|
|
// Use Authorization ...
|
|
app.UseAuthorization();
|
|
|
|
//
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|