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

117 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Haoliang.Core.Services;
using Haoliang.Api.Middleware;
namespace Haoliang.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "CNC Data Collection API", Version = "v1" });
});
// 配置跨域
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// 配置SignalR
services.AddSignalR();
// 配置服务依赖注入
ConfigureServices(services);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CNC Data Collection API v1"));
}
// 中间件顺序很重要
app.UseMiddleware<LoggingMiddleware>();
app.UseMiddleware<CORSMiddleware>();
app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<RealTimeHub>("/realtime");
});
}
private void ConfigureServices(IServiceCollection services)
{
// 配置数据库连接
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<Haoliang.Data.Entities.CNCDbContext>(options =>
options.UseSqlServer(connectionString));
// 注册服务
services.AddScoped<IDeviceCollectionService, DeviceCollectionService>();
services.AddScoped<IProductionService, ProductionService>();
services.AddScoped<IAlarmService, AlarmManager>();
services.AddScoped<ITemplateService, TemplateManager>();
services.AddScoped<ISystemConfigService, SystemConfigManager>();
services.AddScoped<ILoggingService, LoggingManager>();
services.AddScoped<ISchedulerService, BackgroundTaskManager>();
services.AddScoped<ICachingService, CacheManager>();
services.AddScoped<IRealTimeService, RealTimeManager>();
services.AddScoped<IWebSocketAuthMiddleware, JwtAuthMiddleware>();
// 注册仓储
services.AddScoped<IDeviceRepository, DeviceRepository>();
services.AddScoped<ITemplateRepository, TemplateRepository>();
services.AddScoped<IProductionRepository, ProductionRepository>();
services.AddScoped<IAlarmRepository, AlarmRepository>();
services.AddScoped<ISystemConfigRepository, SystemConfigRepository>();
services.AddScoped<ILogRepository, LogRepository>();
services.AddScoped<ICollectionTaskRepository, CollectionTaskRepository>();
services.AddScoped<ICollectionResultRepository, CollectionResultRepository>();
services.AddScoped<ICollectionLogRepository, CollectionLogRepository>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IProgramProductionSummaryRepository, ProgramProductionSummaryRepository>();
services.AddScoped<IProductionSummaryRepository, ProductionSummaryRepository>();
// 注册SignalR Hub
services.AddSingleton<RealTimeHub>();
}
}
}