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.
 
 
 

80 line
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using JT808.Gateway.SimpleQueueNotification.Configs;
  6. using JT808.Gateway.SimpleQueueNotification.Hubs;
  7. using JT808.Gateway.SimpleQueueNotification.Middlewares;
  8. using Microsoft.AspNetCore.Builder;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Hosting;
  14. using Microsoft.Extensions.Logging;
  15. using JT808.Protocol;
  16. using JT808.Gateway.Kafka;
  17. using JT808.Gateway.MsgIdHandler;
  18. using JT808.Gateway.SimpleQueueNotification.Impl;
  19. namespace JT808.Gateway.SimpleQueueNotification
  20. {
  21. public class Startup
  22. {
  23. public Startup(IHostEnvironment env)
  24. {
  25. var builder = new ConfigurationBuilder()
  26. .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
  27. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  28. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
  29. .AddEnvironmentVariables();
  30. Configuration = builder.Build();
  31. }
  32. public IConfiguration Configuration { get; }
  33. // This method gets called by the runtime. Use this method to add services to the container.
  34. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  35. public void ConfigureServices(IServiceCollection services)
  36. {
  37. services.AddSingleton<ILoggerFactory, LoggerFactory>();
  38. services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
  39. services.AddLogging((configure) => {
  40. configure.AddConsole();
  41. configure.SetMinimumLevel(LogLevel.Trace);
  42. });
  43. services.AddSignalR();
  44. services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
  45. {
  46. //builder.AllowAnyOrigin()
  47. // .AllowAnyMethod()
  48. // .AllowAnyHeader()
  49. // .AllowAnyOrigin();
  50. builder.AllowAnyMethod()
  51. .AllowAnyHeader()
  52. .WithOrigins(Configuration.GetSection("CorsHosts").Get<List<string>>().ToArray())
  53. .AllowCredentials();
  54. }));
  55. services.Configure<AuthOptions>(Configuration.GetSection("AuthOptions"));
  56. services.AddJT808Configure()
  57. .AddClientKafka()
  58. .AddMsgConsumer(Configuration)
  59. .AddMsgIdHandler<JT808MsgIdHandlerImpl>();
  60. }
  61. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  62. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  63. {
  64. app.UseDefaultFiles();
  65. app.UseStaticFiles();
  66. app.UseRouting();
  67. app.UseJT808JwtVerify();
  68. app.UseCors("CorsPolicy");
  69. app.UseEndpoints(endpoints =>
  70. {
  71. endpoints.MapHub<JT808MsgHub>("/JT808MsgHub");
  72. });
  73. }
  74. }
  75. }