123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 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<LiteDBFactoryOptions>
- {
- 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<SignalRExamServerHub>("/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<IMemoryCache>();
- IHttpClientFactory? clientFactory = app.Services.GetRequiredService<IHttpClientFactory>();
- LiteDBFactory liteDBFactory = app.Services.GetRequiredService<LiteDBFactory>();
- JsonNode? data = null;
- try
- {
- string? CenterUrl = builder.Configuration.GetValue<string>("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<JsonNode>(content);
- data!["centerUrl"]=CenterUrl;
- cache.Set(Constant._KeyServerCenter, data);
- SystemInfo? system = JsonSerializer.Deserialize<SystemInfo>(data);
- system!.id= $"{DateTimeOffset.Now.ToUnixTimeMilliseconds()}";
- liteDBFactory.GetLiteDatabase().GetCollection<SystemInfo>("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; }
- }
- }
|