Skip to content

Commit

Permalink
Allow CORS for API calls (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Feb 13, 2025
1 parent e7ad542 commit 6fbb1c6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Core/CrestApps.OrchardCore.AI.Core/AIConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static class AIConstants

public const string CollectionName = "AI";

public const string AllowAllOriginsHeadersMethodsPolicyName = "AllowAllOriginsHeadersMethods";

public static class SystemMessages
{
public const string UseMarkdownSyntax = "- Provide a response using Markdown syntax.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CrestApps.OrchardCore.AI.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
Expand All @@ -10,6 +11,7 @@ public static IEndpointRouteBuilder AddApiAIChatSessionEndpoint(this IEndpointRo
{
_ = builder.MapGet("api/ai/chat/session", AIChatSessionEndpoint.HandleAsync)
.DisableAntiforgery()
.RequireCors(AIConstants.AllowAllOriginsHeadersMethodsPolicyName)
.RequireAuthorization(new AuthorizeAttribute { AuthenticationSchemes = "Api" });

return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CrestApps.OrchardCore.AI.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
Expand All @@ -10,6 +11,7 @@ public static IEndpointRouteBuilder AddApiAICompletionEndpoint<T>(this IEndpoint
{
_ = builder.MapPost("api/ai/completion/chat", AICompletionEndpoint.HandleAsync<T>)
.DisableAntiforgery()
.RequireCors(AIConstants.AllowAllOriginsHeadersMethodsPolicyName)
.RequireAuthorization(new AuthorizeAttribute { AuthenticationSchemes = "Api" });

return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CrestApps.OrchardCore.AI.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
Expand All @@ -10,6 +11,7 @@ public static IEndpointRouteBuilder AddApiAIUtilityCompletionEndpoint<T>(this IE
{
_ = builder.MapPost("api/ai/completion/utility", AIUtilityCompletionEndpoint.HandleAsync<T>)
.DisableAntiforgery()
.RequireCors(AIConstants.AllowAllOriginsHeadersMethodsPolicyName)
.RequireAuthorization(new AuthorizeAttribute { AuthenticationSchemes = "Api" });

return builder;
Expand Down
21 changes: 21 additions & 0 deletions src/Modules/CrestApps.OrchardCore.AI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.Data;
Expand Down Expand Up @@ -136,8 +137,28 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro
[Feature(AIConstants.Feature.ChatApi)]
public sealed class ApiChatStartup : StartupBase
{
public override int Order
=> OrchardCoreConstants.ConfigureOrder.Cors;

public override void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(AIConstants.AllowAllOriginsHeadersMethodsPolicyName,
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
}

public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
app.UseCors();

routes
.AddApiAIChatSessionEndpoint()
.AddApiAIUtilityCompletionEndpoint<ApiChatStartup>()
Expand Down

0 comments on commit 6fbb1c6

Please sign in to comment.