Program.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using IES.ExamServer.DI;
  2. using IES.ExamServer.DI.SignalRHost;
  3. using IES.ExamServer.Helper;
  4. using Microsoft.AspNetCore.SpaServices;
  5. using Microsoft.AspNetCore.StaticFiles;
  6. using Microsoft.Extensions.Caching.Memory;
  7. using System.Text.Json;
  8. using System.Text.Json.Nodes;
  9. using VueCliMiddleware;
  10. namespace IES.ExamServer
  11. {
  12. public class Program
  13. {
  14. public async static Task Main(string[] args)
  15. {
  16. var builder = WebApplication.CreateBuilder(args);
  17. builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
  18. builder.Services.AddSpaStaticFiles(opt => opt.RootPath = "ClientApp/dist");
  19. // Add services to the container.
  20. builder.Services.AddControllersWithViews();
  21. builder.Services.AddHttpClient();
  22. builder.Services.AddSignalR();
  23. builder.Services.AddHttpContextAccessor();
  24. string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  25. string dbpath = $"{localAppDataPath}\\ExamServer\\LiteDB";
  26. if (!System.IO.Directory.Exists(dbpath))
  27. {
  28. System.IO.Directory.CreateDirectory(dbpath);
  29. }
  30. string liteDBPath = $"Filename={dbpath}\\data.db;Connection=shared";
  31. var connections_LiteDB = new List<LiteDBFactoryOptions>
  32. {
  33. new LiteDBFactoryOptions { Name = "Master", Connectionstring = liteDBPath}
  34. };
  35. builder.Services.AddLiteDB(connections_LiteDB);
  36. builder.Services.AddMemoryCache();
  37. string path = $"{builder.Environment.ContentRootPath}/Configs";
  38. builder.Services.AddCors(options =>
  39. {
  40. options.AddDefaultPolicy(
  41. builder =>
  42. {
  43. builder.AllowAnyOrigin()
  44. .AllowAnyHeader()
  45. .AllowAnyMethod();
  46. });
  47. });
  48. var app = builder.Build();
  49. // Configure the HTTP request pipeline.
  50. if (!app.Environment.IsDevelopment())
  51. {
  52. app.UseExceptionHandler("/Home/Error");
  53. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  54. app.UseHsts();
  55. }
  56. else { app.UseDeveloperExceptionPage(); }
  57. app.UseHttpsRedirection();
  58. app.UseDefaultFiles();
  59. var contentTypeProvider = new FileExtensionContentTypeProvider();
  60. contentTypeProvider.Mappings[".glb"] = "model/gltf-binary";
  61. app.UseStaticFiles(new StaticFileOptions
  62. {
  63. ContentTypeProvider = contentTypeProvider,
  64. });
  65. app.UseRouting();
  66. app.UseAuthorization();
  67. app.UseEndpoints(endpoints =>
  68. {
  69. endpoints.MapHub<SignalRExamServerHub>("/signalr/screen").RequireCors("any");
  70. endpoints.MapControllers();
  71. // NOTE: VueCliProxy is meant for developement and hot module reload
  72. // NOTE: SSR has not been tested
  73. // Production systems should only need the UseSpaStaticFiles() (above)
  74. // You could wrap this proxy in either
  75. // if (System.Diagnostics.Debugger.IsAttached)
  76. // or a preprocessor such as #if DEBUG
  77. /*
  78. npm install -g @vue
  79. vue create app
  80. */
  81. //#if DEBUG
  82. endpoints.MapToVueCliProxy(
  83. "{*path}",
  84. new SpaOptions { SourcePath = "ClientApp" },
  85. npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null,
  86. // regex: "Compiled successfully",
  87. forceKill: true
  88. );
  89. //#else
  90. // endpoints.MapFallbackToFile("index.html");
  91. //#endif
  92. });
  93. IMemoryCache? cache = app.Services.GetRequiredService<IMemoryCache>();
  94. IHttpClientFactory? clientFactory = app.Services.GetRequiredService<IHttpClientFactory>();
  95. LiteDBFactory liteDBFactory = app.Services.GetRequiredService<LiteDBFactory>();
  96. JsonNode? data = null;
  97. try
  98. {
  99. string? CenterUrl = builder.Configuration.GetValue<string>("ExamServer:CenterUrl");
  100. var httpclient = clientFactory.CreateClient();
  101. httpclient.Timeout= TimeSpan.FromSeconds(10);
  102. HttpResponseMessage message = await httpclient.PostAsJsonAsync($"{CenterUrl}/core/system-info", new { });
  103. if (message.IsSuccessStatusCode)
  104. {
  105. string content = await message.Content.ReadAsStringAsync();
  106. data = JsonSerializer.Deserialize<JsonNode>(content);
  107. data!["centerUrl"]=CenterUrl;
  108. cache.Set(Constant._KeyServerCenter, data);
  109. SystemInfo? system = JsonSerializer.Deserialize<SystemInfo>(data);
  110. system!.id= $"{DateTimeOffset.Now.ToUnixTimeMilliseconds()}";
  111. liteDBFactory.GetLiteDatabase().GetCollection<SystemInfo>("System").Insert(system);
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. }
  117. app.Run();
  118. }
  119. }
  120. public class SystemInfo
  121. {
  122. public string? id { get; set; }
  123. public string? version { get; set; }
  124. public string? description { get; set; }
  125. public long nowtime { get; set; }
  126. public string? region { get; set; }
  127. public string? ip { get; set; }
  128. public string? date { get; set; }
  129. public string? centerUrl { get; set; }
  130. }
  131. }