111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
|
using Microsoft.AspNetCore.Builder;
|
|||
|
using Microsoft.AspNetCore.Hosting;
|
|||
|
using Microsoft.Extensions.Configuration;
|
|||
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using Microsoft.Extensions.Hosting;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using Repository.Models;
|
|||
|
using Repository.Helper;
|
|||
|
using Repository.BackendRepository.Interface;
|
|||
|
using Repository.BackendRepository.Implement;
|
|||
|
using Mqtt.Services;
|
|||
|
using System.Linq;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
|||
|
namespace Mqtt
|
|||
|
{
|
|||
|
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 services)
|
|||
|
{
|
|||
|
services.AddControllersWithViews();
|
|||
|
|
|||
|
services.AddSwaggerGen();
|
|||
|
services.AddRazorPages();
|
|||
|
|
|||
|
#region DBHelper <EFBFBD>`<EFBFBD>J
|
|||
|
services.Configure<DBConfig>(Configuration.GetSection("DBConfig"));
|
|||
|
services.AddTransient<IDatabaseHelper, DatabaseHelper>();
|
|||
|
#endregion DBHelper <EFBFBD>`<EFBFBD>J
|
|||
|
|
|||
|
#region BackEndConfigHelper <EFBFBD>`<EFBFBD>J
|
|||
|
services.Configure<BackEndConfig>(Configuration.GetSection("BackEndConfig"));
|
|||
|
services.AddTransient<IBackEndConfigHelper, BackEndConfigHelper>();
|
|||
|
#endregion BackEndConfigHelper <EFBFBD>`<EFBFBD>J
|
|||
|
|
|||
|
#region Repository <EFBFBD>`<EFBFBD>J
|
|||
|
services.AddTransient<IBackendRepository, BackendRepository>();
|
|||
|
services.AddTransient<IBackgroundServiceRepository, BackgroundServiceRepository>();
|
|||
|
services.AddTransient<IBackgroundServiceMsSqlRepository, BackgroundServiceMsSqlRepository>();
|
|||
|
services.AddTransient<IBackgroundServicePostgresqlRepository, BackgroundServicePostgresqlRepository>();
|
|||
|
#endregion Repository <EFBFBD>`<EFBFBD>J
|
|||
|
|
|||
|
services.AddSingleton<MqttClientService>(); // Register the MqttService
|
|||
|
services.AddControllers();
|
|||
|
services.AddSignalR();
|
|||
|
|
|||
|
services.AddLogging(
|
|||
|
builder =>
|
|||
|
{
|
|||
|
builder.AddFilter("Microsoft", LogLevel.Warning)
|
|||
|
.AddFilter("System", LogLevel.Warning)
|
|||
|
.AddFilter("NToastNotify", LogLevel.Warning)
|
|||
|
.AddConsole();
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory logFactory)
|
|||
|
{
|
|||
|
logFactory.AddFile(Configuration.GetValue<string>("LoggerPath") + "/log-{Date}.txt");
|
|||
|
|
|||
|
if (env.IsDevelopment())
|
|||
|
{
|
|||
|
app.UseDeveloperExceptionPage();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
app.UseExceptionHandler("/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();
|
|||
|
}
|
|||
|
|
|||
|
app.UseSwagger();
|
|||
|
app.UseSwaggerUI();
|
|||
|
|
|||
|
app.UseHttpsRedirection();
|
|||
|
app.UseStaticFiles(new StaticFileOptions
|
|||
|
{
|
|||
|
OnPrepareResponse = ctx =>
|
|||
|
{
|
|||
|
ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
app.UseRouting();
|
|||
|
|
|||
|
app.UseAuthorization();
|
|||
|
|
|||
|
// Add initialization logic here
|
|||
|
app.ApplicationServices.GetService<MqttClientService>()?.StartAsync();
|
|||
|
|
|||
|
app.UseEndpoints(endpoints =>
|
|||
|
{
|
|||
|
endpoints.MapRazorPages();
|
|||
|
|
|||
|
endpoints.MapControllerRoute(
|
|||
|
name: "defaultApi",
|
|||
|
pattern: "api/{controller}/{action}/{id?}");
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|