forked from fanliang11/surging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
162 lines (136 loc) · 5.8 KB
/
Startup.cs
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
using System;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using Newtonsoft.Json.Serialization;
using ApiGateWayConfig = Surging.Core.ApiGateWay.AppConfig;
using Surging.Core.CPlatform;
using Surging.Core.ProxyGenerator;
using Surging.Core.Consul;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.DotNetty;
using Surging.Core.Consul.Configurations;
using Surging.Core.ApiGateWay.OAuth.Implementation.Configurations;
using Autofac.Extensions.DependencyInjection;
using Surging.Core.ApiGateWay;
using Surging.Core.Caching.Configurations;
using Surging.Core.Codec.MessagePack;
using Microsoft.AspNetCore.Mvc;
namespace GateWay.WebApi
{
public class Startup
{
public IConfiguration Configuration { get; }
public IContainer ApplicationContainer { get; private set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddCacheFile("Configs/cacheSettings.json", optional: false)
.AddJsonFile("Configs/appsettings.json", optional: true, reloadOnChange: true)
.AddGatewayFile("Configs/gatewaySettings.json", optional: false)
.AddJsonFile($"Configs/appsettings.{env.EnvironmentName}.json", optional: true);
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
RegisterAutofac(services);
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "网关 API", Version = "v1" });
c.DescribeAllParametersInCamelCase();
});
// services.AddResponseCaching();
//分布式缓存 Microsoft.Extensions.Caching.Redis NuGet package
/*services.AddDistributedRedisCache(options =>
{
options.Configuration = "servername";
options.InstanceName = "Shopping";
});*/
//基于 的身份认证
/* services.AddCookieAuthentication(CookieAuthenticationDefaults.Authenticatio
nScheme, options =>
{
options.LoginPath = "/Account/Login/";
options.AccessDeniedPath = "/Account/Forbidden/";
options.LogoutPath = "/Account/Logout";
options.ReturnUrlParameter = "ReturnUrl";
});
Microsoft.AspNetCore.Identity.EntityFrameworkCore
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString
"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddCors();
*/
}
private IServiceProvider RegisterAutofac(IServiceCollection services)
{
var registerConfig = ApiGateWayConfig.Register;
services.AddMvc(options =>
{
options.Filters.Add(typeof(CustomExceptionFilterAttribute));
//options.CacheProfiles.Add("5minutes", new CacheProfile
//{
// Duration = 5 * 60,
// Location = ResponseCacheLocation.Any,
// VaryByHeader = "Accept-Language"
//});
//[ResponseCache(CacheProfileName = "5minutes")]
}).AddJsonOptions(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddLogging();
var builder = new ContainerBuilder();
builder.Populate(services);
builder.AddMicroService(option =>
{
option.AddClient();
// option.AddClientIntercepted(typeof(CacheProviderInterceptor));
option.UseConsulManager(new ConfigInfo(registerConfig.Address));
option.UseDotNettyTransport();
option.AddApiGateWay();
option.UseJsonCodec();
// option.UseMessagePackCodec();
builder.Register(m => new CPlatformContainer(ServiceLocator.Current));
});
ServiceLocator.Current = builder.Build();
return new AutofacServiceProvider(ServiceLocator.Current);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
// app.UseAuthentication();
//app.UseCors(builder => builder.WithOrigins("http://mysite.com"));
//app.UseResponseCaching();
app.UseMvc();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "网关 API V1");
});
}
}
}