123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using Edge.Core.Processor;
- using Edge.Core.Processor.Dispatcher;
- using Edge.Core.UniversalApi;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.ApiExplorer;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Core;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using Microsoft.OpenApi.Models;
- using MQTTnet.AspNetCore;
- using MQTTnet.Diagnostics;
- using MQTTnet.Protocol;
- using MQTTnet.Server;
- using Edge.WebHost.Hubs;
- using Swashbuckle.AspNetCore.SwaggerGen;
- namespace Edge.WebHost
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection aspNetCoreServices)
- {
- aspNetCoreServices.AddCors(options => options.AddPolicy("cors", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin()));
- aspNetCoreServices.AddLogging(loggingBuilder =>
- {
- loggingBuilder.ClearProviders();
- loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
- });//Startup.Services.GetRequiredService<ILoggerFactory>());
-
- aspNetCoreServices.AddControllersWithViews();
- // .AddRazorOptions(opt =>
- //{
- // opt.ViewLocationFormats.Add("/AspNetCoreWebApi/Views/{1}/{0}.cshtml");
- // opt.ViewLocationFormats.Add("/AspNetCoreWebApi/Views/Shared/{0}.cshtml");
- //});
- // Register the Swagger generator, defining 1 or more Swagger documents
- aspNetCoreServices.AddSwaggerGen(c =>
- {
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "Universal Api - WebApi", Version = "v1" });
- c.DocumentFilter<InjectProcessorApiDocumentFilter>();
- // Set the comments path for the Swagger JSON and UI.
- var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
- var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
- c.IncludeXmlComments(xmlPath);
- });
- aspNetCoreServices.AddMqttWebSocketServerAdapter();
- aspNetCoreServices.AddSignalR();
- //var mqttServerOptionsBuilder = new MqttServerOptionsBuilder()
- // .WithoutDefaultEndpoint()
- // //.WithDefaultEndpointPort(mqttServerPort)
- // .WithConnectionValidator(
- // c =>
- // {
- // c.ReasonCode = MqttConnectReasonCode.Success;
- // }).WithSubscriptionInterceptor(
- // c =>
- // {
- // c.AcceptSubscription = true;
- // }).WithApplicationMessageInterceptor(
- // c =>
- // {
- // c.AcceptPublish = true;
- // });
- aspNetCoreServices
- .AddHostedMqttServer(mqttServer => mqttServer.WithoutDefaultEndpoint())
- .AddMqttConnectionHandler()
- .AddConnections();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- //app.UseCors();
- app.UseCors("cors");
- // Enable middleware to serve generated Swagger as a JSON endpoint.
- app.UseSwagger();
- // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
- // specifying the Swagger JSON endpoint.
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "Universal Api - WebApi V1");
- });
- //if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- // else
- //{
- // app.UseExceptionHandler("/Home/Error");
- //}
- app.UseStaticFiles();
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- endpoints.MapHub<AlarmHub>("/alarmHub");
- });
- //app.UseEndpoints(endpoints =>
- //{
- // endpoints.MapControllerRoute(
- // name: "WebConsole",
- // pattern: "WebConsole/{controller=Home}/{action=Index}/{id?}");
- //});
- //app.UseEndpoints(endpoints =>
- //{
- // endpoints.MapMqtt("/mqtt");
- //});
- //MqttNetGlobalLogger.LogMessagePublished += (s, e) =>
- //{
- // if (e.LogMessage?.Exception != null)
- // {
- // var trace = $">> [{e.LogMessage.Timestamp:O}] [{e.LogMessage.ThreadId}] [{e.LogMessage.Source}] [{e.LogMessage.Level}]: {e.LogMessage.Message}";
- // trace += Environment.NewLine + e.LogMessage.Exception.ToString();
- // //AppInstance?.logger?.LogInformation("Mqtt Server or client component exceptioned: " + Environment.NewLine + trace);
- // }
- //};
- //app.UseMqttEndpoint("/mqtt");
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapMqtt("/mqtt");
- });
- app.UseMqttServer(mqttServer =>
- {
- //mqttServer.UseClientConnectedHandler(e =>
- //{
- // //var debug = e.ClientId;
- //});
- //mqttServer.UseClientDisconnectedHandler(e =>
- //{
- // //var debug = e.ClientId;
- //});
- //mqttServer.UseApplicationMessageReceivedHandler(async e =>
- //{
- // //var debug = e.ApplicationMessage.Topic + " " +
- // // Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
- // //if (this.logger.IsEnabled(LogLevel.Debug))
- // //{
- // // this.logger.LogDebug($"+ received msg on Topic = {e.ApplicationMessage.Topic}");
- // // this.logger.LogDebug($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
- // // this.logger.LogDebug($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
- // // this.logger.LogDebug($"+ Retain = {e.ApplicationMessage.Retain}");
- // //}
- //});
- });
- }
- }
- }
|