123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using IES.ExamServer.DI;
- using IES.ExamServer.DI.SignalRHost;
- using IES.ExamServer.Filters;
- using Microsoft.AspNetCore.StaticFiles;
- using NLog.Extensions.Logging;
- using SkiaSharp;
- using System.Drawing;
- using System.Text;
- using System.Text.Encodings.Web;
- using System.Text.Unicode;
- using static System.Net.Mime.MediaTypeNames;
- using System.Diagnostics;
- using System.Security.Authentication;
- using Microsoft.Extensions.FileProviders;
- namespace IES.ExamServer.Server
- {
- public class Program
- {
- public async static Task Main(string[] args)
- {
- Console.OutputEncoding = Encoding.UTF8;
- var builder = WebApplication.CreateBuilder(args);
- // Add services to the container.
- builder.WebHost.ConfigureKestrel(serverOptions =>
- {
- // 配置支持的 SSL/TLS 协议版本
- serverOptions.ConfigureHttpsDefaults(httpsOptions =>
- {
- httpsOptions.SslProtocols = SslProtocols.Tls |
- SslProtocols.Tls11 |
- SslProtocols.Tls12 |
- SslProtocols.Tls13;
- });
- });
- string path = $"{builder.Environment.ContentRootPath}/Configs";
- // Add services to the container.
- builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
- // Add services to the container.
- builder.Services.AddControllersWithViews().AddJsonOptions(options =>
- {
- // 设置 JSON 序列化选项
- options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); // 允许所有 Unicode 字符
- options.JsonSerializerOptions.WriteIndented = true; // 格式化输出(可选)
- });
- builder.Services.AddHttpClient();
- builder.Services.AddSignalR();
- builder.Services.AddHttpContextAccessor();
- //此处能在Linux及MacOS运行,
- //Windows的路径是LocalApplicationData Path: C:\Users\john\AppData\Local
- //Linux的路径是LocalApplicationData Path: /home/john/.local/share,
- //MacOS 的路径LocalApplicationData Path: /Users/john/Library/Application Support
- string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
- //string dbpath = $"{localAppDataPath}\\ExamServer\\LiteDB";
- string dbpath = Path.Combine(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();
- // 注册 ConnectionService 为单例
- builder.Services.AddSingleton<CenterServiceConnectionService>();
- builder.Services.AddSingleton<ServiceInitializer>();
- // 注册 DataQueue 服务
- builder.Services.AddSingleton<DataQueue>();
- // 注册后台服务
- builder.Services.AddHostedService<SubjectPushService>();
- builder.Services.AddCors(options =>
- {
- //options.AddDefaultPolicy(
- //builder =>
- //{
- // builder.AllowAnyOrigin()
- // .AllowAnyHeader()
- // .AllowAnyMethod();
- //});
- options.AddPolicy("any", builder =>
- {
- //builder.SetIsOriginAllowed(x => true)
- //.AllowAnyMethod()
- //.AllowAnyHeader()
- //.AllowCredentials();
- builder.AllowAnyOrigin()
- .AllowAnyHeader()
- .AllowAnyMethod();
- });
- });
- builder.Services.AddMvcFilter<AuthTokenActionFilter>();
- // 添加自定义日志提供程序
- //builder.Logging.ClearProviders();
- //bool enableConsoleOutput = true;
- //builder.Logging.AddProvider(new CustomFileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "Logs"), enableConsoleOutput));
- // 添加日志服务
- builder.Logging.ClearProviders();
- builder.Logging.AddNLog();
- builder.Services.AddHostedService<SignalRCloudClientHub>();
- var app = builder.Build();
- app.UseDefaultFiles();
- //var contentTypeProvider = new FileExtensionContentTypeProvider();
- //contentTypeProvider.Mappings[".txt"] = "text/plain";
- //contentTypeProvider.Mappings[".jpg"] = "image/jpeg";
- //contentTypeProvider.Mappings[".jpeg"] = "image/jpeg";
- //contentTypeProvider.Mappings[".png"] = "image/png";
- //contentTypeProvider.Mappings[".html"] = "text/html";
- //contentTypeProvider.Mappings[".js"] = "application/javascript";
- //contentTypeProvider.Mappings[".css"] = "text/css";
- //contentTypeProvider.Mappings[".mp4"] = "video/mp4";
- //contentTypeProvider.Mappings[".mp3"] = "audio/mpeg";
- //contentTypeProvider.Mappings[".json"] = "application/json";
- //contentTypeProvider.Mappings[".pdf"] = "application/pdf";
- string packagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package");
- if (!Directory.Exists(packagePath))
- {
- Directory.CreateDirectory(packagePath);
- }
- //会自动处理文件夹下的子文件夹及文件映射关系。
- /*
- 请求 URL 映射到的文件路径
- https://localhost/data/data.json C:\Users\YourUser\AppData\Local\ExamData\data.json
- https://localhost/data/abc/a.json C:\Users\YourUser\AppData\Local\ExamData\abc\a.json
- https://localhost/data/123/1.json C:\Users\YourUser\AppData\Local\ExamData\123\1.json
- */
- string uploadPath = Path.Combine(localAppDataPath, "ExamServer", "Upload");
- if (!Directory.Exists(uploadPath))
- {
- Directory.CreateDirectory(uploadPath);
- }
- app.UseStaticFiles(new StaticFileOptions
- {
- FileProvider = new PhysicalFileProvider(uploadPath),
- RequestPath = "/upload" // 映射请求路径,学生上传文件
- });
- string answerPath = Path.Combine(localAppDataPath, "ExamServer", "Answer");
- if (!Directory.Exists(answerPath))
- {
- Directory.CreateDirectory(answerPath);
- }
- app.UseStaticFiles(new StaticFileOptions
- {
- FileProvider = new PhysicalFileProvider(answerPath),
- RequestPath = "/answer" // 映射请求路径,学生作答文件
- });
- app.UseStaticFiles();
- // Configure the HTTP request pipeline.
- app.UseHttpsRedirection();
- app.UseRouting();
- app.UseCors("any");
- app.UseAuthorization();
- app.MapControllers();
- app.MapFallbackToFile("/index.html");
- app.MapHub<SignalRExamServerHub>("/signalr/exam").RequireCors("any");
- var connectionManager = app.Services.GetRequiredService<ServiceInitializer>();
- await connectionManager.InitializeAsync();
- await app.RunAsync();
- }
- }
- }
|