using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Falcon.Extend;
using FAuth.DataBase;
using FAuth.Extensions;
using FAuth.Extensions.Account;
using FAuth.Extensions.Decryptor;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
namespace FAuth
{
///
/// 应用设置
///
public class Startup
{
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
///
/// 注册到服务容器
///
/// 服务容器
public void ConfigureServices(IServiceCollection services) {
//注册Json序列化
services.AddMsJsonProvider();
//注册数据库
services.AddDbContext(option => {
//option.UseSqlServer(this.Configuration.GetValue("Database:FAuthDbSqlServer"));
var dbType = this.Configuration.GetValue("Database:UseDb").ToLower();
switch(dbType) {
case "mysql":
option.UseMySQL(this.Configuration.GetValue("Database:FAuthDbMySql"));
break;
case "sqlserver":
option.UseSqlServer(this.Configuration.GetValue("Database:FAuthDbSqlServer"));
break;
default:
throw new Exception("Database:UseDb配置错误!只能为mysql或sqlserver");
}
});
services.AddAccountHelper();
//注册Redis
var rop = this.Configuration.GetSection("Redis").Get();
services.AddRedis(rop);
//注册MVC
services.AddControllersWithViews()
.AddJsonOptions(option => {
option.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
option.JsonSerializerOptions.PropertyNamingPolicy = null;
});
//注册Swagger
services.AddSwaggerGen(c => {
c.SwaggerDoc("V1",new OpenApiInfo {
Title = "接口文档",
Version = "1.0",
Description = "api",
Contact = new OpenApiContact {
Name = "Falcon",
Url = new Uri("http://39.105.71.191/Falcon/FAuth"),
},
});
var xmlPath = Path.Combine(AppContext.BaseDirectory,typeof(Program).Assembly.GetName().Name + ".xml");
c.IncludeXmlComments(xmlPath,true);
c.AddXmlEnumEnable(xmlPath);
});
//注册用户票据生成器
services.AddAESCrypto();
var UTDO = Configuration.GetSection("UserTicketDecryptorOption");
services.AddUserTicketDryptor(UTDO);
//注册api错误处理器
services.AddScoped();
}
// 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();
} else {
app.UseExceptionHandler("/Home/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();
}
using(var socpe = app.ApplicationServices.CreateScope()) {
var db = socpe.ServiceProvider.GetService();
db.InitSync().Wait();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档");
c.RoutePrefix = "";
});
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}