-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (114 loc) · 4.25 KB
/
Program.cs
File metadata and controls
142 lines (114 loc) · 4.25 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
using Microsoft.EntityFrameworkCore;
using PMS.Web.Data;
using PMS.Web.Services;
using System.Text.Json;
using System.Text.RegularExpressions;
var builder = WebApplication.CreateBuilder(args);
// Helper method to load API key from test.txt
static void LoadApiKeyFromTestFile(WebApplicationBuilder builder)
{
try
{
var env = builder.Environment;
var testFilePath = Path.Combine(env.ContentRootPath, "test.txt");
var appSettingsPath = Path.Combine(env.ContentRootPath, "appsettings.json");
// Check if test.txt exists
if (!File.Exists(testFilePath))
{
Console.WriteLine("test.txt not found. Using default API key configuration.");
return;
}
// Read and extract API key
var testFileContent = File.ReadAllText(testFilePath).Trim();
var apiKey = ExtractApiKey(testFileContent);
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.WriteLine("Could not extract API key from test.txt.");
return;
}
// Check if appsettings.json has placeholder
if (File.Exists(appSettingsPath))
{
var appSettingsContent = File.ReadAllText(appSettingsPath);
if (appSettingsContent.Contains("\"ApiKey\": \"YOUR_GROQ_API_KEY\""))
{
// Update appsettings.json
var updatedContent = appSettingsContent.Replace(
"\"ApiKey\": \"YOUR_GROQ_API_KEY\"",
$"\"ApiKey\": \"{apiKey}\""
);
File.WriteAllText(appSettingsPath, updatedContent);
Console.WriteLine("API key loaded from test.txt and updated in appsettings.json.");
}
}
// Also update configuration in memory
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
{ "Groq:ApiKey", apiKey }
});
}
catch (Exception ex)
{
Console.WriteLine($"Warning: Failed to load API key from test.txt: {ex.Message}");
}
}
// Helper method to extract API key from test.txt
static string ExtractApiKey(string content)
{
// Method 1: Remove "test" prefix and suffix (case-insensitive)
var trimmed = content.Trim();
if (trimmed.StartsWith("test", StringComparison.OrdinalIgnoreCase))
{
trimmed = trimmed.Substring(4);
}
if (trimmed.EndsWith("test", StringComparison.OrdinalIgnoreCase))
{
trimmed = trimmed.Substring(0, trimmed.Length - 4);
}
// Method 2: Use regex to find Groq API key pattern (starts with gsk_)
var regex = new Regex(@"gsk_[A-Za-z0-9]{32,}", RegexOptions.IgnoreCase);
var match = regex.Match(trimmed);
if (match.Success)
{
return match.Value;
}
// Method 3: Return trimmed content as fallback
return trimmed.Trim();
}
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();
// Add DbContext
builder.Services.AddDbContext<PMSDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Register AuditLogService
builder.Services.AddScoped<AuditLogService>();
// Register CustomerAnalyticsService
builder.Services.AddScoped<CustomerAnalyticsService>();
// Load API key from test.txt before building app (bypass GitHub secret scanning)
LoadApiKeyFromTestFile(builder);
// Register AI RAG Services
builder.Services.AddHttpClient<GroqAIService>();
builder.Services.AddScoped<SQLQueryService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
// Serve static files from wwwroot (required for runtime-created uploads, e.g. /uploads/customers/...)
app.UseStaticFiles();
app.MapStaticAssets();
// Map attribute-routed controllers first
app.MapControllers();
// Map conventional routes
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();