lixiaojun
2023-10-19 321b2a3adebe2aab1340fc5ffd7134c9131348b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Serilog;
using SqlSugar;
using System.Text.Json.Serialization;
using Yw.Application;
using Mapster;
using IGeekFan.AspNetCore.Knife4jUI;
using Furion.SpecificationDocument;
using Microsoft.AspNetCore.Mvc.Controllers;
using Furion.Authorization;
 
namespace IStation.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    [AppStartup(10)]
    public class Startup : AppStartup
    {
        /// <summary>
        /// 
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConfigurableOptions<Yw.JWT.JWTSettingsOptions>();
 
            services.Configure<KestrelServerOptions>(options =>
            {
                options.Limits.MaxRequestBodySize = int.MaxValue;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = int.MaxValue;
            });
 
            services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
 
            //需在 services.AddControllers() 之前注册
            services.AddCorsAccessor();
 
            services.AddControllers().AddInjectWithUnifyResult<XnRestfulResultProvider>();
            services.AddSwaggerGen(c =>
            {
                //避免dto 同名
                c.CustomSchemaIds(x => x.FullName);
                //支持knife4ui(必备)
                c.CustomOperationIds(s =>
                {
                    var a = s.ActionDescriptor as ControllerActionDescriptor;
                    return a.ControllerTypeInfo.Name + "-" + s.HttpMethod + a.ActionName;
                });
                c.UseInlineDefinitionsForEnums();
            }).AddMiniProfiler();
 
 
            services.AddJsonOptions(options =>
            {
                //返回属性大小写问题
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                //时间格式处理
                options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter());
                options.JsonSerializerOptions.Converters.Add(new DateTimeNullableJsonConverter());
 
                //长整型格式处理
                options.JsonSerializerOptions.Converters.Add(new LongJsonConverter());
                options.JsonSerializerOptions.Converters.Add(new LongNullableJsonConverter());
                //忽略循环引用
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 仅.NET 6支持
            });
            // 添加即时通讯
            services.AddSignalR();
            SnowFlakeSingle.WorkId = Yw.Settings.SqlSugarParasHelper.SqlSugar.SnowFlakeWorkId;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
 
            // 添加规范化结果状态码,需要在这里注册
            app.UseUnifyResultStatusCodes();
 
            app.UseHttpsRedirection();
 
            #region 开放 data 文件夹(可以通过url访问文件)
 
            //app.UseStaticFiles();
            string path = AppContext.BaseDirectory;
            path = Path.Combine(path, Yw.Settings.ServiceParasHelper.Service.DataFolder);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //通过url访问文件
            app.UseStaticFiles(new StaticFileOptions()//自定义自己的文件路径
            {
                RequestPath = new PathString(""),//对外的访问路径 之前为 "/Data" ,改动是为了隐藏Data对外显示
                FileProvider = new PhysicalFileProvider(path),//指定实际物理路径
                OnPrepareResponse = (stf) =>//静态资源跨域问题
                {
                    stf.Context.Response.Headers["Access-Control-Allow-Origin"] = "*";
                    stf.Context.Response.Headers["Access-Control-Allow-Headers"] = "*";
                }
            });
 
            #endregion
 
            // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
            //app.UseSerilogRequestLogging();
 
            app.UseRouting();
 
            //需在 app.UseRouting(); 和 app.UseAuthentication(); 之间注册
            app.UseCorsAccessor();
 
            app.UseAuthentication();
            app.UseAuthorization();
 
            // 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存)
            app.UseKnife4UI(options =>
            {
                options.RoutePrefix = "Api";  // 配置 Knife4UI 路由地址
                foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
                {
                    options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
                }
            });
 
            app.UseInject(string.Empty);
 
            app.UseEndpoints(endpoints =>
            {
                // 注册集线器
                endpoints.MapHub<ChatHub>("/hubs/chathub");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
 
 
        }
 
 
 
 
    }
}