You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.Extensions.FileProviders;
  3. using Microsoft.Extensions.Options;
  4. using Microsoft.IdentityModel.Tokens;
  5. using Microsoft.OpenApi.Models;
  6. using NLog.Web;
  7. using Shentun.PeisReport.Api.Jwt;
  8. using System.IdentityModel.Tokens.Jwt;
  9. using System.Text;
  10. var builder = WebApplication.CreateBuilder(args);
  11. var configuration = builder.Configuration;
  12. // Add services to the container.
  13. //nlog
  14. builder.Logging.AddNLog("Configs/NLog.config");
  15. builder.Services.AddControllers();
  16. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  17. builder.Services.AddEndpointsApiExplorer();
  18. builder.Services.AddSwaggerGen(opt =>
  19. {
  20. var baseDirectory = AppContext.BaseDirectory;
  21. var commentsFile = Path.Combine(baseDirectory, "Shentun.PeisReport.Api.xml");
  22. opt.IncludeXmlComments(commentsFile, true);
  23. });
  24. var apiGroupNames = configuration["ApiGroupName"];
  25. builder.Services.AddSwaggerGen(c =>
  26. {
  27. if (apiGroupNames.Contains("JianLiApi"))
  28. {
  29. c.SwaggerDoc("JianLiApi", new OpenApiInfo { Title = "����ҽԺ", Version = "V1", Description = "����ҽԺ" });
  30. }
  31. if (apiGroupNames.Contains("QingHaiWuYuanApi"))
  32. {
  33. c.SwaggerDoc("QingHaiWuYuanApi", new OpenApiInfo { Title = "�ຣ��Ժ", Version = "V1", Description = "�ຣ��Ժ" });
  34. }
  35. var baseDirectory = AppContext.BaseDirectory;
  36. //����ע��
  37. var commentsFile = Path.Combine(baseDirectory, "Shentun.PeisReport.Api.xml");
  38. c.IncludeXmlComments(commentsFile, true);
  39. });
  40. builder.Services.AddCors(options =>
  41. {
  42. options.AddPolicy("AllowAll",
  43. builder => builder
  44. .AllowAnyOrigin()
  45. .AllowAnyMethod()
  46. .AllowAnyHeader());
  47. });
  48. var jwtHelper = new JwtHelper();
  49. //����JWT ��֤����
  50. builder.Services.AddAuthentication(opt =>
  51. {
  52. opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  53. opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  54. }).AddJwtBearer(opt =>
  55. {
  56. opt.TokenValidationParameters = jwtHelper.GetTokenValidationParameters();
  57. });
  58. var app = builder.Build();
  59. var isSwagger = Convert.ToBoolean(configuration["Swagger:IsEnabled"]);
  60. if (isSwagger)
  61. {
  62. app.UseSwagger();
  63. //app.UseSwaggerUI();
  64. app.UseSwaggerUI(c =>
  65. {
  66. if (apiGroupNames.Contains("JianLiApi"))
  67. c.SwaggerEndpoint($"/swagger/JianLiApi/swagger.json", "����ҽԺApi"); //������ʾ
  68. if (apiGroupNames.Contains("QingHaiWuYuanApi"))
  69. c.SwaggerEndpoint($"/swagger/QingHaiWuYuanApi/swagger.json", "�ຣ��ԺApi"); //������ʾ
  70. //c.DefaultModelExpandDepth(-1);
  71. });
  72. }
  73. //����Ŀ¼
  74. app.UseStaticFiles(new StaticFileOptions
  75. {
  76. FileProvider = new PhysicalFileProvider(configuration["ReportVirtualPath:RealPath"]),
  77. RequestPath = configuration["ReportVirtualPath:RequestPath"]
  78. });
  79. app.UseStaticFiles(new StaticFileOptions
  80. {
  81. FileProvider = new PhysicalFileProvider(configuration["LisReportVirtualPath:RealPath"]),
  82. RequestPath = configuration["LisReportVirtualPath:RequestPath"]
  83. });
  84. app.UseCors("AllowAll");
  85. app.UseAuthentication();
  86. app.UseAuthorization();
  87. app.MapControllers();
  88. app.Run();