Skip to content

Commit 0952a2a

Browse files
committed
Gateways Config
配置网关
1 parent 591e9c3 commit 0952a2a

19 files changed

Lines changed: 339 additions & 116 deletions

File tree

Blog.Core.AdminMvc/Blog.Core.AdminMvc.csproj

Lines changed: 0 additions & 11 deletions
This file was deleted.

Blog.Core.AdminMvc/Startup.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

Blog.Core.AdminMvc/appsettings.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

Blog.Core.Api/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
"Enabled": true
170170
},
171171
"Consul": {
172-
"Enabled": false
172+
"Enabled": true
173173
},
174174
"IpRateLimit": {
175175
"Enabled": true

Blog.Core.Extensions/Authorizations/Policys/ApiResponseHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Blog.Core.AuthHelper.Policys;
1+
using Blog.Core.Model;
22
using Microsoft.AspNetCore.Authentication;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.Extensions.Logging;

Blog.Core.Extensions/Middlewares/ExceptionHandlerMidd.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Blog.Core.AuthHelper.Policys;
1+
using Blog.Core.Model;
22
using Microsoft.AspNetCore.Http;
33
using Newtonsoft.Json;
44
using System;

Blog.Core.Extensions/ServiceExtensions/AppConfigSetup.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ public static void AddAppConfigSetup(this IServiceCollection services, IWebHostE
139139
ConsoleHelper.WriteSuccessLine($"RabbitMQ: True");
140140
}
141141

142+
// Consul 注册服务
143+
if (!Appsettings.app("Middleware", "Consul", "Enabled").ObjToBool())
144+
{
145+
Console.WriteLine($"Consul service: False");
146+
}
147+
else
148+
{
149+
ConsoleHelper.WriteSuccessLine($"Consul service: True");
150+
}
151+
142152
// EventBus 事件总线
143153
if (!Appsettings.app("EventBus", "Enabled").ObjToBool())
144154
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Remove="Extensions\ApiResponseHandler.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
13+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
14+
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.1" />
15+
<PackageReference Include="Ocelot" Version="17.0.0" />
16+
<PackageReference Include="Ocelot.Provider.Consul" Version="17.0.0" />
17+
<PackageReference Include="Ocelot.Provider.Polly" Version="17.0.0" />
18+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\Blog.Core.Extensions\Blog.Core.Extensions.csproj" />
23+
<ProjectReference Include="..\Blog.Core.Model\Blog.Core.Model.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Blog.Core.Common.HttpContextUser;
2+
using Blog.Core.Model;
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Security.Claims;
8+
9+
namespace Blog.Core.Gateway.Controllers
10+
{
11+
[Authorize]
12+
[Route("/gateway/[controller]/[action]")]
13+
public class UserController : ControllerBase
14+
{
15+
private readonly IUser _user;
16+
17+
public UserController(IUser user)
18+
{
19+
_user = user;
20+
}
21+
22+
[HttpGet]
23+
public MessageModel<List<Claim>> MyClaims()
24+
{
25+
return new MessageModel<List<Claim>>()
26+
{
27+
success = true,
28+
response = _user.GetClaimsIdentity().ToList()
29+
};
30+
}
31+
}
32+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Blog.Core.Model;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Serialization;
4+
using System;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Blog.Core.Gateway.Extensions
12+
{
13+
/// <summary>
14+
/// 这里不需要,目前集成的是 Blog.Core.Extensions 下的接口处理器
15+
/// 但是你可以单独在网关中使用这个。
16+
/// </summary>
17+
public class ApiResponseHandler : DelegatingHandler
18+
{
19+
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
20+
{
21+
ContractResolver = new CamelCasePropertyNamesContractResolver()
22+
};
23+
24+
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
25+
{
26+
var response = await base.SendAsync(request, cancellationToken);
27+
var contentType = response.Content.Headers.ContentType?.MediaType ?? "";
28+
if (!contentType.Equals("application/json")) return response;
29+
30+
dynamic result = null;
31+
var resultStr = await response.Content.ReadAsStringAsync();
32+
try
33+
{
34+
result = JsonConvert.DeserializeObject<dynamic>(resultStr);
35+
}
36+
catch (Exception)
37+
{
38+
return response;
39+
}
40+
41+
if (result != null && result.code == 500) resultStr = result.msg.ToString();
42+
43+
var apiResponse = new ApiResponse(StatusCode.CODE200).MessageModel;
44+
if (response.StatusCode != HttpStatusCode.OK || result.code == (int)HttpStatusCode.InternalServerError)
45+
{
46+
var exception = new Exception(resultStr);
47+
apiResponse = new ApiResponse(StatusCode.CODE500).MessageModel;
48+
}
49+
else if (result.code == (int)HttpStatusCode.Unauthorized)
50+
{
51+
apiResponse = new ApiResponse(StatusCode.CODE401).MessageModel;
52+
53+
}
54+
else if (result.code == (int)HttpStatusCode.Forbidden)
55+
{
56+
apiResponse = new ApiResponse(StatusCode.CODE403).MessageModel;
57+
58+
}
59+
else
60+
{
61+
62+
}
63+
64+
var statusCode = apiResponse.status == 500 ? HttpStatusCode.InternalServerError
65+
: apiResponse.status == 401 ? HttpStatusCode.Unauthorized
66+
: apiResponse.status == 403 ? HttpStatusCode.Forbidden
67+
: HttpStatusCode.OK;
68+
69+
response.StatusCode = statusCode;
70+
response.Content = new StringContent(JsonConvert.SerializeObject(apiResponse, jsonSerializerSettings), Encoding.UTF8, "application/json");
71+
72+
return response;
73+
}
74+
}
75+
76+
77+
}

0 commit comments

Comments
 (0)