119 lines
4.7 KiB
C#
119 lines
4.7 KiB
C#
using Domain.AggregateModel.AppAggregate;
|
|
using Domain.AggregateModel.LinkAggregate;
|
|
using FluentValidation;
|
|
using FluentValidation.AspNetCore;
|
|
using Infrastructure;
|
|
using Infrastructure.Repositories;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Redis;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.OpenApi.Models;
|
|
using Pomelo.EntityFrameworkCore.MySql;
|
|
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
|
|
using QRCodeService.Application.Behaviors;
|
|
using QRCodeService.Application.Commands;
|
|
using QRCodeService.Application.Queries;
|
|
using QRCodeService.Application.Validations;
|
|
using QRCodeService.Infrastructure.Middlewares;
|
|
using QRCodeService.Options;
|
|
using QRCodeService.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace QRCodeService
|
|
{
|
|
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().AddRazorRuntimeCompilation();
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" });
|
|
});
|
|
//配置
|
|
services.Configure<ServiceOption>(Configuration.GetSection("Hosting"));
|
|
|
|
//validator
|
|
services.AddTransient<IValidator<CreateLinkCommand>, CreateLinkCommandValidator>();
|
|
|
|
//MediatR+
|
|
services.AddMediatR(Assembly.GetExecutingAssembly())
|
|
.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>))
|
|
.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>))
|
|
.AddTransient(typeof(IPipelineBehavior<,>), typeof(TransactionBehaviour<,>));
|
|
//EFCore
|
|
services.AddDbContext<AppDbContext>(
|
|
dbContextOptions => dbContextOptions
|
|
.UseMySql(
|
|
Configuration.GetConnectionString("Default"),
|
|
// For common usages, see pull request #1233.
|
|
new MariaDbServerVersion(new Version(10, 5, 9)), // use MariaDbServerVersion for MariaDB
|
|
mySqlOptions => mySqlOptions
|
|
.CharSetBehavior(CharSetBehavior.NeverAppend))
|
|
// Everything from this point on is optional but helps with debugging.
|
|
.EnableSensitiveDataLogging()
|
|
.EnableDetailedErrors()
|
|
);
|
|
|
|
//Repository
|
|
services.AddScoped<IAppRepository, AppRepository>();
|
|
services.AddScoped<ILinkRepository, LinkRepository>();
|
|
//Queries
|
|
services.AddScoped<ILinkQueries, LinkQueries>(s => new LinkQueries(Configuration.GetConnectionString("Default")));
|
|
services.AddScoped<IAppQueries, AppQueries>(s => new AppQueries(Configuration.GetConnectionString("Default")));
|
|
//redis cache
|
|
services.AddDistributedRedisCache(options => options.Configuration = Configuration.GetSection("redis:default").Value);
|
|
|
|
//反向代理中间件
|
|
services.Configure<ForwardedHeadersOptions>(options =>
|
|
{
|
|
options.ForwardedHeaders =
|
|
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
|
});
|
|
|
|
services.AddTransient<IShortCodeService, ShortCodeService>();
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
//if (env.IsDevelopment())
|
|
//{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QRCodeService v1"));
|
|
//}
|
|
app.UseForwardedHeaders();
|
|
app.UseRouting();
|
|
|
|
app.UseAuthorization();
|
|
app.UseMiddleware<CheckSignMiddleware>();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|