Skip to content

Commit

Permalink
remove frontend proxy and use cors allowed list in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
thegovind committed Nov 7, 2023
1 parent 9f4ab50 commit b8c4ae4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
17 changes: 12 additions & 5 deletions services/recommendation-service/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
// Add Semantic Kernel services
builder.Services.AddSkServices();

// Explicitly configure Kestrel to only use HTTP
builder.WebHost.ConfigureKestrel(serverOptions =>
// Configure CORS to allow specific origins from KernelSettings
builder.Services.AddCors(options =>
{
// Remove the HTTPS endpoint if it's there
serverOptions.ListenAnyIP(80); // Listen on port 80 for HTTP
options.AddPolicy(name: "MiyagiAllowSpecificOrigins",
policy =>
{
policy.WithOrigins(kernelSettings.CorsAllowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod();
});
});

var app = builder.Build();
Expand All @@ -34,9 +39,11 @@
app.UseSwagger();
app.UseSwaggerUI();

app.UseCors();
app.UseCors("MiyagiAllowSpecificOrigins");
// app.UseHttpsRedirection(); // Issue with Next.js to use https redirection

app.UseRouting();

app.Map("/", () => Results.Redirect("/swagger"));

// app.UseAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ internal class KernelSettings
[JsonPropertyName("cosmosDbConnectionString")]
public string CosmosDbConnectionString { get; set; } = string.Empty;

[JsonPropertyName("corsAllowedOrigins")]
public string[] CorsAllowedOrigins { get; set; } = Array.Empty<string>();


/// <summary>
/// Load the kernel settings from settings.json if the file exists and if not attempt to use user secrets.
Expand Down
2 changes: 1 addition & 1 deletion ui/typescript/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-DC9QMEZBZL
RECCOMMENDATION_SERVICE_URL=http://localhost:5224
NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL=http://localhost:5224
NEXT_PUBLIC_COPILOT_CHAT_BASE_URL=https://copilotdemo.app
2 changes: 1 addition & 1 deletion ui/typescript/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEXT_PUBLIC_GA_MEASUREMENT_ID=
RECCOMMENDATION_SERVICE_URL=
NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL=
NEXT_PUBLIC_CHAT_ID=
COPILOT_CHAT_BASE_URL=
NEXT_PUBLIC_COPILOT_CHAT_BASE_URL=
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/chat-session/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const userId = searchParams.get('userId');
const RECCOMMENDATION_SERVICE_URL = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
const chatsUrl = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
console.log("Request body: ");
console.dir(userId)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(chatsUrl, {
method: 'POST',
headers: {
'Content-type': `application/json`
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/chatSession/getAllChats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const userId = searchParams.get('userId');
const RECCOMMENDATION_SERVICE_URL = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
const chatsUrl = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
console.log("Request body: ");
console.dir(userId)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(chatsUrl, {
method: 'POST',
headers: {
'Content-type': `application/json`
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/personalize/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { NextResponse } from 'next/server';

export async function POST(request: Request) {
console.dir(request);
const RECCOMMENDATION_SERVICE_URL = `${process.env.RECCOMMENDATION_SERVICE_URL}/personalize`
const personalizeUrl = `${process.env.NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL}/personalize`
const { body } = request
console.log("Request body: ");
console.dir(body)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(personalizeUrl, {
method: 'POST',
headers: { 'Content-type': `application/json` },
duplex: 'half',
Expand Down
24 changes: 20 additions & 4 deletions ui/typescript/src/components/personalize/personalize-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,33 @@ export default function PersonalizeDrawer() {
stocks,
};

const response = await personalizeMutation.mutateAsync(requestData);
console.log('Successfully got personalization');
const response = await fetch(`${process.env.NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL}/personalize`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData),
});

console.dir(response);

if (!response.ok) {
console.error('HTTP error: ', response);
toast.error('Failed to fetch personalization. Try again later.');
}

const responseData = await response.json();
console.log('Successfully got personalization');
console.dir(responseData);
toast.success('Personalization successful');

// Extract the relevant data from the response
const updatedAssetData = response.assets.portfolio.map((item, index) => ({
const updatedAssetData = responseData.assets.portfolio.map((item, index) => ({
...assetsInfo[index],
gptRecommendation: item.gptRecommendation,
}));

const updatedInvestmentData = response.investments.portfolio.map((item, index) => ({
const updatedInvestmentData = responseData.investments.portfolio.map((item, index) => ({
...investmentsInfo[index],
gptRecommendation: item.gptRecommendation,
}));
Expand All @@ -107,6 +122,7 @@ export default function PersonalizeDrawer() {
}
};


return (
<Transition appear show={isPersonalizeOpen} as={Fragment}>
<Dialog
Expand Down

0 comments on commit b8c4ae4

Please sign in to comment.