refactoring for integration
This commit is contained in:
parent
80144bf9fc
commit
070f41ac7e
48 changed files with 100 additions and 46 deletions
141
src/Yavsc.Api/Program.cs
Normal file
141
src/Yavsc.Api/Program.cs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
Copyright (c) 2024 HigginsSoft, Alexander Higgins - https://github.com/alexhiggins732/
|
||||
|
||||
Copyright (c) 2018, Brock Allen & Dominick Baier. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
Source code and license this software can be found
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
*/
|
||||
|
||||
using Anthropic.SDK;
|
||||
using IdentityModel;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc;
|
||||
using Yavsc.Abstract.Interfaces;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Interface;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Services;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
Console.Title = "API";
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var services = builder.Services;
|
||||
|
||||
// Anthropic client
|
||||
builder.Services.AddSingleton<AnthropicClient>(_ =>
|
||||
new AnthropicClient(
|
||||
new APIAuthentication(
|
||||
builder.Configuration["ANTHROPIC_API_KEY"]
|
||||
?? throw new InvalidOperationException("ANTHROPIC_API_KEY manquante")
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Service de modération
|
||||
if (builder.Environment.IsDevelopment())
|
||||
builder.Services.AddScoped<IModerationService, MockModerationService>();
|
||||
else
|
||||
builder.Services.AddScoped<IModerationService, ClaudeModerationService>();
|
||||
|
||||
|
||||
// accepts any access token issued by identity server
|
||||
// adds an authorization policy for scope 'scope1'
|
||||
|
||||
services
|
||||
.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("ApiScope", policy =>
|
||||
{
|
||||
policy
|
||||
.RequireAuthenticatedUser()
|
||||
.RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
|
||||
});
|
||||
})
|
||||
.AddCors(options =>
|
||||
{
|
||||
// this defines a CORS policy called "default"
|
||||
options.AddPolicy("default", policy =>
|
||||
{
|
||||
policy.WithOrigins("https://localhost:5003")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
})
|
||||
.AddControllers();
|
||||
|
||||
// accepts any access token issued by identity server
|
||||
var authenticationBuilder = services.AddAuthentication("Bearer")
|
||||
.AddJwtBearer("Bearer", options =>
|
||||
{
|
||||
options.IncludeErrorDetails = true;
|
||||
options.Authority = "https://localhost:5001";
|
||||
options.TokenValidationParameters =
|
||||
new() { ValidateAudience = false, RoleClaimType = YavscConstants.RoleClaimType };
|
||||
options.MapInboundClaims = true;
|
||||
});
|
||||
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
services.AddLocalization(options =>
|
||||
{
|
||||
options.ResourcesPath = "Resources";
|
||||
});
|
||||
//
|
||||
services.AddTransient<Microsoft.AspNetCore.Identity.IEmailSender<ApplicationUser>, MailSender>();
|
||||
services.AddTransient<ITrueEmailSender, MailSender>()
|
||||
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender,
|
||||
MailSender>()
|
||||
.AddTransient<IBillingService, BillingService>()
|
||||
.AddTransient<ICalendarManager, CalendarManager>();
|
||||
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
|
||||
builder.Services.AddSession(options =>
|
||||
{
|
||||
options.IdleTimeout = TimeSpan.FromMinutes(30);
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.IsEssential = false;
|
||||
});
|
||||
|
||||
builder.Services.AddDistributedMemoryCache();
|
||||
|
||||
builder.Services.Configure<RequestLocalizationOptions>(options =>
|
||||
{
|
||||
var supportedCultures = new[] { "fr", "en", "pt" };
|
||||
options.SetDefaultCulture(supportedCultures[0])
|
||||
.AddSupportedCultures(supportedCultures)
|
||||
.AddSupportedUICultures(supportedCultures);
|
||||
});
|
||||
WorkflowHelpers.ConfigureBillingService();
|
||||
using (var app = builder.Build())
|
||||
{
|
||||
if (app.Environment.IsDevelopment())
|
||||
app.UseDeveloperExceptionPage();
|
||||
|
||||
app
|
||||
.UseRouting()
|
||||
.UseAuthentication()
|
||||
.UseAuthorization()
|
||||
.UseCors("default")
|
||||
;
|
||||
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
|
||||
app.MapDefaultControllerRoute();
|
||||
app.MapGet("/identity", (HttpContext context) =>
|
||||
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
|
||||
);
|
||||
|
||||
app.UseSession();
|
||||
await app.RunAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue