-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (211 loc) · 7.76 KB
/
Copy pathProgram.cs
File metadata and controls
238 lines (211 loc) · 7.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.FileProviders;
using Syncfusion.EJ2;
using Syncfusion.Licensing;
#region Schedule Usings
using EJ2ScheduleSample.Pages;
#endregion
#region AI Usings
using OpenAI;
using Syncfusion.EJ2.AI;
using Microsoft.Extensions.AI;
using EJ2CoreSampleBrowser.Services;
#if STAGING
using Azure.AI.OpenAI;
using System.ClientModel;
#endif
#endregion
#region Diagram Usings
using EJ2CoreSampleBrowser.Services;
using StackExchange.Redis;
#endregion
using System.Text.RegularExpressions;
if (File.Exists(Directory.GetCurrentDirectory() + "/SyncfusionLicense.txt"))
{
string licenseKey = File.ReadAllText(Directory.GetCurrentDirectory() + "/SyncfusionLicense.txt").Trim();
SyncfusionLicenseProvider.RegisterLicense(licenseKey);
if (File.Exists(Directory.GetCurrentDirectory() + "/wwwroot/scripts/index.js"))
{
string regexPattern = "ej.base.registerLicense(.*);";
string jsContent = File.ReadAllText(Directory.GetCurrentDirectory() + "/wwwroot/scripts/index.js");
MatchCollection matchCases = Regex.Matches(jsContent, regexPattern);
foreach (Match matchCase in matchCases)
{
var replaceableString = matchCase.ToString();
jsContent = jsContent.Replace(replaceableString, "ej.base.registerLicense('" + licenseKey + "');");
}
File.WriteAllText(Directory.GetCurrentDirectory() + "/wwwroot/scripts/index.js", jsContent);
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRouting(options => options.LowercaseUrls = true);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHsts(options =>
{
options.MaxAge = TimeSpan.FromDays(730); // Set max-age to 730 days
options.IncludeSubDomains = true; // Include subdomains
options.Preload = true; // Enable preload
});
builder.Services.AddRazorPages();
#region AI Configuration
#if STAGING
/* Azure OpenAI Service */
string AzureApiKey = Environment.GetEnvironmentVariable("API_KEY") ?? "Your-Api-Key";
string AzureDeploymentName = Environment.GetEnvironmentVariable("DEPLOYMENT_NAME") ?? "Your-Model-Name";
string AzureEndpoint = Environment.GetEnvironmentVariable("END_POINT") ?? "https://your-azure-openai.openai.azure.com/";
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(
new Uri(AzureEndpoint),
new ApiKeyCredential(AzureApiKey)
);
IChatClient AIChatClient = azureOpenAIClient.GetChatClient(AzureDeploymentName).AsIChatClient();
#else
/* OpenAI Service */
string apiKey = Environment.GetEnvironmentVariable("API_KEY") ?? "Your-Api-Key";
string deploymentName = Environment.GetEnvironmentVariable("DEPLOYMENT_NAME") ?? "Your-Model-Name";
OpenAIClient openAIClient = new OpenAIClient(apiKey);
IChatClient AIChatClient = openAIClient.GetChatClient(deploymentName).AsIChatClient();
#endif
builder.Services.AddChatClient(AIChatClient);
builder.Services.AddScoped<UserTokenService>();
builder.Services.AddScoped<IChatInferenceService, AIService>(sp =>
{
var userTokenService = sp.GetRequiredService<UserTokenService>();
return new AIService(userTokenService, AIChatClient);
});
builder.Services.AddScoped<AIService>();
builder.Services.AddSyncfusionSmartComponents();
#endregion
builder.Services.AddMvc()
.AddNewtonsoftJson(x =>
{
x.SerializerSettings.ContractResolver = null;
});
#region Schedule Configuration
builder.Services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
options.MaximumReceiveMessageSize = 10 * 1024 * 1024;
options.MaximumParallelInvocationsPerClient = 2;
options.HandshakeTimeout = TimeSpan.FromMinutes(1);
});
#endregion
#region Diagram Configuration
builder.Services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
options.MaximumReceiveMessageSize = 10 * 1024 * 1024;
options.MaximumParallelInvocationsPerClient = 2;
options.HandshakeTimeout = TimeSpan.FromMinutes(1);
})
.AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
options.PayloadSerializerOptions.WriteIndented = false;
options.PayloadSerializerOptions.MaxDepth = 64;
options.PayloadSerializerOptions.DefaultIgnoreCondition =
System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});
// Redis Connection
builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
{
var connectionString = builder.Configuration.GetConnectionString("Redis")
?? "localhost:6379,abortConnect=false";
return ConnectionMultiplexer.Connect(connectionString);
});
#endregion
builder.Services.AddDirectoryBrowser();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
#if REDIS
builder.Services.AddMemoryCache();
builder.Services.AddDistributedRedisCache(option => { option.Configuration = builder.Configuration["ConnectionStrings:Redis"]; });
#endif
// CORS policy for Blazor client and Azure-hosted URLs (used by SignalR)
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowBlazorClient", policy =>
{
policy.WithOrigins(
"https://blazor.syncfusion.com/",
"https://sfblazor.azurewebsites.net/"
)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/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.UseCookiePolicy(new CookiePolicyOptions
{
HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always, // Always make cookies HttpOnly
Secure = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always,
MinimumSameSitePolicy = SameSiteMode.Strict // SameSiteStrict to prevent cross-site requests
});
app.UseRouting();
// Important for SignalR WebSocket upgrade when hosting client separately
app.UseCors("AllowBlazorClient");
app.UseAuthorization();
app.MapControllerRoute(name: "default",
pattern: "{controller}/{action}");
app.MapRazorPages();
app.UseFileServer();
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "plain/text",
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Pages")),
RequestPath = "/Pages"
});
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "plain/text",
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Models")),
RequestPath = "/Models"
});
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "plain/text",
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Controllers")),
RequestPath = "/Controllers"
});
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "plain/text",
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Views")),
RequestPath = "/Views"
});
#region Schedule Hub Configuration
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ScheduleHub>("/scheduleHub");
});
#endregion
app.Run();