-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
187 lines (160 loc) · 7.18 KB
/
Copy pathProgram.cs
File metadata and controls
187 lines (160 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using BudgetBackend.Data;
using BudgetBackend.Plugins;
using BudgetBackend.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using System.Text;
using System.Reflection;
namespace BudgetBackend
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Database configuration
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING"),
sqlServerOptions => sqlServerOptions.EnableRetryOnFailure()
)
);
// OpenAI configuration
var openAiConfig = builder.Configuration.GetSection("AiServiceSettings");
string? endpoint = openAiConfig["Endpoint"];
string? apiKey = openAiConfig["ApiKey"];
string? deploymentName = openAiConfig["DeploymentName"];
string? modelId = openAiConfig["ModelId"];
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(deploymentName) || string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException("Azure OpenAI configuration is missing or incorrect.");
}
builder.Services.AddSingleton<IChatCompletionService>(serviceProvider =>
{
return new AzureOpenAIChatCompletionService(
deploymentName: deploymentName!,
apiKey: apiKey!,
endpoint: endpoint!,
modelId: modelId!
);
});
builder.Services.AddSingleton<Kernel>(serviceProvider =>
{
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddAzureOpenAIChatCompletion(
deploymentName: deploymentName!,
apiKey: apiKey!,
endpoint: endpoint!,
modelId: modelId!,
serviceId: "openai-service"
);
kernelBuilder.Plugins.AddFromType<BudgetPlugin>();
return kernelBuilder.Build();
});
// Add AI services
builder.Services.AddAIServices(builder.Configuration);
// Register TokenService
builder.Services.AddScoped<TokenService>();
// Register AgentFactory - specify the correct namespace
builder.Services.AddScoped<BudgetBackend.Agents.AgentFactory>();
// Register history service
builder.Services.AddScoped<IHistoryService, HistoryService>();
// JWT Authentication configuration
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
builder.Configuration["JwtSettings:SecretKey"] ?? throw new InvalidOperationException("JWT Secret Key is not configured")
)),
ValidateIssuer = true,
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["JwtSettings:Audience"],
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
});
// Add authorization
builder.Services.AddAuthorization();
// Register plugins and services
builder.Services.AddScoped<BudgetPlugin>();
builder.Services.AddScoped<FinancialChatService>();
// Add CORS policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.WithOrigins(builder.Configuration["AllowedOrigins"]?.Split(',') ?? new[] { "http://localhost:3000" })
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "FinBin API", Version = "v1" });
// If you have XML comments for API documentation
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
});
// Configure email settings
builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection("EmailSettings"));
builder.Services.AddScoped<IEmailService, EmailService>();
// Add Application Insights if in production
if (!builder.Environment.IsDevelopment())
{
builder.Services.AddApplicationInsightsTelemetry();
}
var app = builder.Build();
// Apply migrations at startup
// Only do this in development - for production use proper migration strategy
if (app.Environment.IsDevelopment())
{
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction()) // Allow Swagger in production too
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FinBin API v1");
});
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseCors("AllowFrontend");
app.UseHttpsRedirection();
// Make sure UseAuthentication is called before UseAuthorization
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}