using IES.ExamServer.DI; using IES.ExamServer.DI.SignalRHost; using IES.ExamServer.Helper; using Microsoft.AspNetCore.SpaServices; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Caching.Memory; using System.Text.Json; using System.Text.Json.Nodes; using VueCliMiddleware; namespace IES.ExamServer { public class Program { public async static Task Main(string[] args) { var builder = WebApplication.CreateBuilder(args); builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); builder.Services.AddSpaStaticFiles(opt => opt.RootPath = "ClientApp/dist"); // Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddHttpClient(); builder.Services.AddSignalR(); builder.Services.AddHttpContextAccessor(); string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string dbpath = $"{localAppDataPath}\\ExamServer\\LiteDB"; if (!System.IO.Directory.Exists(dbpath)) { System.IO.Directory.CreateDirectory(dbpath); } string liteDBPath = $"Filename={dbpath}\\data.db;Connection=shared"; var connections_LiteDB = new List { new LiteDBFactoryOptions { Name = "Master", Connectionstring = liteDBPath} }; builder.Services.AddLiteDB(connections_LiteDB); builder.Services.AddMemoryCache(); string path = $"{builder.Environment.ContentRootPath}/Configs"; builder.Services.AddCors(options => { options.AddDefaultPolicy( builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); 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(); } else { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseDefaultFiles(); var contentTypeProvider = new FileExtensionContentTypeProvider(); contentTypeProvider.Mappings[".glb"] = "model/gltf-binary"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = contentTypeProvider, }); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapHub("/signalr/screen").RequireCors("any"); endpoints.MapControllers(); // NOTE: VueCliProxy is meant for developement and hot module reload // NOTE: SSR has not been tested // Production systems should only need the UseSpaStaticFiles() (above) // You could wrap this proxy in either // if (System.Diagnostics.Debugger.IsAttached) // or a preprocessor such as #if DEBUG /* npm install -g @vue vue create app */ //#if DEBUG endpoints.MapToVueCliProxy( "{*path}", new SpaOptions { SourcePath = "ClientApp" }, npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null, // regex: "Compiled successfully", forceKill: true ); //#else // endpoints.MapFallbackToFile("index.html"); //#endif }); IMemoryCache? cache = app.Services.GetRequiredService(); IHttpClientFactory? clientFactory = app.Services.GetRequiredService(); LiteDBFactory liteDBFactory = app.Services.GetRequiredService(); JsonNode? data = null; try { string? CenterUrl = builder.Configuration.GetValue("ExamServer:CenterUrl"); var httpclient = clientFactory.CreateClient(); httpclient.Timeout= TimeSpan.FromSeconds(10); HttpResponseMessage message = await httpclient.PostAsJsonAsync($"{CenterUrl}/core/system-info", new { }); if (message.IsSuccessStatusCode) { string content = await message.Content.ReadAsStringAsync(); data = JsonSerializer.Deserialize(content); data!["centerUrl"]=CenterUrl; cache.Set(Constant._KeyServerCenter, data); SystemInfo? system = JsonSerializer.Deserialize(data); system!.id= $"{DateTimeOffset.Now.ToUnixTimeMilliseconds()}"; liteDBFactory.GetLiteDatabase().GetCollection("System").Insert(system); } } catch (Exception ex) { } app.Run(); } } public class SystemInfo { public string? id { get; set; } public string? version { get; set; } public string? description { get; set; } public long nowtime { get; set; } public string? region { get; set; } public string? ip { get; set; } public string? date { get; set; } public string? centerUrl { get; set; } } }