-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (76 loc) · 2.76 KB
/
Program.cs
File metadata and controls
93 lines (76 loc) · 2.76 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
using CodeVault.Data;
using CodeVault.Services;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.EntityFrameworkCore;
using CodeVault.Extensions;
DotNetEnv.Env.Load();
var builder = WebApplication.CreateBuilder(args);
if (builder.Environment.IsDevelopment())
{
builder.Configuration.AddUserSecrets<Program>();
}
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddControllersWithViews();
// Add JSON options to handle circular references
builder.Services.AddControllersWithViews()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
options.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});
builder.Services.AddHttpClient();
builder.Services.AddScoped<IOpenAiService, OpenAiService>();
builder.Services.AddDbContext<CodeDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<CodeService>();
builder.Services.AddScoped<ConversationService>();
builder.Services.AddHttpClient<VectorEmbeddingService>();
builder.Services.AddScoped<VectorEmbeddingService>();
builder.Services.AddScoped<CodeAnalysisService>();
builder.Services.AddScoped<CodeComparisonService>();
builder.Services.AddScoped<SecurityAnalysisService>();
builder.Services.AddApiServices();
builder.Services.AddControllers();
if (builder.Environment.IsDevelopment())
{
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "CodeVault API", Version = "v1" });
});
}
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var dbContext = services.GetRequiredService<CodeDbContext>();
dbContext.Database.Migrate();
Console.WriteLine("Successfully applied migrations");
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating the database.");
Console.WriteLine($"Error applying migrations: {ex.Message}");
}
}
app.UseStaticFiles();
app.UseRouting();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CodeVault API v1"));
}
app.UseCors("ApiCorsPolicy");
app.MapControllers();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Chat}/{action=Index}/{id?}");
app.Run();