ibms-MCUT/Mqtt/Startup.cs
2025-01-24 12:05:03 +08:00

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 ª`¤J
services.Configure<DBConfig>(Configuration.GetSection("DBConfig"));
services.AddTransient<IDatabaseHelper, DatabaseHelper>();
#endregion DBHelper ª`¤J
#region BackEndConfigHelper ª`¤J
services.Configure<BackEndConfig>(Configuration.GetSection("BackEndConfig"));
services.AddTransient<IBackEndConfigHelper, BackEndConfigHelper>();
#endregion BackEndConfigHelper ª`¤J
#region Repository ª`¤J
services.AddTransient<IBackendRepository, BackendRepository>();
services.AddTransient<IBackgroundServiceRepository, BackgroundServiceRepository>();
services.AddTransient<IBackgroundServiceMsSqlRepository, BackgroundServiceMsSqlRepository>();
services.AddTransient<IBackgroundServicePostgresqlRepository, BackgroundServicePostgresqlRepository>();
#endregion Repository ª`¤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?}");
});
}
}
}