Skip to content

Commit 73eafdb

Browse files
committedOct 12, 2021
♻️ refector & and some ut
1 parent 1aee704 commit 73eafdb

36 files changed

+1286
-203
lines changed
 

‎.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.cs]
2+
3+
# CS1591: Missing XML comment for publicly visible type or member
4+
dotnet_diagnostic.CS1591.severity = none

‎grpc-http-api.sln

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{2011C5
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.HttpApi.Sample", "sample\Grpc.HttpApi.Sample\Grpc.HttpApi.Sample.csproj", "{5D61A06C-4D03-40D5-BB37-FC1196A2D84C}"
1717
EndProject
18+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C8C24F4E-CFF0-44E8-9C98-16790FD621F0}"
19+
ProjectSection(SolutionItems) = preProject
20+
.editorconfig = .editorconfig
21+
EndProjectSection
22+
EndProject
1823
Global
1924
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2025
Debug|Any CPU = Debug|Any CPU

‎sample/Grpc.HttpApi.Sample/Protos/greet.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package greet;
66

77

88
import "google/protobuf/wrappers.proto";
9-
import "google/protobuf/any.proto";
10-
119
import public "annotations.proto";
1210

1311

@@ -48,7 +46,7 @@ message HelloRequest {
4846
NEG = -1; // Intentionally negative.
4947
}
5048

51-
int32 single_int32 = 1; //comment single_int32
49+
int32 single_int32 = 1; //comment single_int32
5250
int64 single_int64 = 2; //comment single_int64
5351
uint32 single_uint32 = 3; //comment single_uint32
5452
uint64 single_uint64 = 4; //comment single_uint64
@@ -74,5 +72,5 @@ message HelloReply {
7472
string message = 1; //comment message
7573
repeated string values = 2; //comment values
7674
google.protobuf.StringValue nullable_message = 3; //comment nullable_message
77-
google.protobuf.Any any_message = 4; //comment any_message
75+
repeated string list_message = 10;
7876
}

‎sample/Grpc.HttpApi.Sample/Services/GrpcHttpApiGreeterService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Grpc.HttpApi.Sample
99
{
10+
1011
public class GrpcHttpApiGreeterService : HttpApiGreeterService.HttpApiGreeterServiceBase
1112
{
1213
private readonly ILogger<GrpcHttpApiGreeterService> _logger;

‎sample/Grpc.HttpApi.Sample/Startup.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,40 @@
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
6-
using Microsoft.OpenApi.Models;
7-
using System;
86
using System.IO;
97

108
namespace Grpc.HttpApi.Sample
119
{
10+
/// <summary>
11+
/// startup
12+
/// </summary>
1213
public class Startup
1314
{
14-
// This method gets called by the runtime. Use this method to add services to the container.
15-
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
15+
16+
/// <summary>
17+
/// This method gets called by the runtime. Use this method to add services to the container.
18+
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
19+
/// </summary>
20+
/// <param name="services"></param>
1621
public void ConfigureServices(IServiceCollection services)
1722
{
1823
services.AddGrpc();
1924

2025
services.AddGrpcHttpApi();
2126

22-
services.AddSwaggerGen(c =>
27+
28+
services.AddGrpcSwagger((option) =>
2329
{
24-
25-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "gRPC HTTP API Example", Version = "v1" });
26-
27-
var xml = $"{typeof(Startup).Assembly.GetName().Name}.xml";
28-
var path = Path.Combine(AppContext.BaseDirectory, xml);
29-
if (File.Exists(path))
30-
{
31-
c.IncludeXmlComments(path);
32-
}
30+
option.ApiInfo = new Swagger.SwaggerApiInfo() { Title="Grpc HttpApi Swagger Sample" };
3331
});
34-
35-
services.AddGrpcSwagger();
3632
}
3733

3834
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
35+
/// <summary>
36+
/// gen
37+
/// </summary>
38+
/// <param name="app"></param>
39+
/// <param name="env"></param>
3940
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4041
{
4142
if (env.IsDevelopment())
@@ -45,10 +46,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4546

4647
app.UseRouting();
4748

48-
app.UseSwagger(option=> {
49-
option.SerializeAsV2 = true;
50-
});
49+
app.UseSwagger();
5150

51+
app.UseSwaggerUI();
5252
/*
5353
app.UseSwaggerUI(c =>
5454
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Grpc.HttpApi.Swagger;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.Options;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Microsoft.Extensions.DependencyInjection
9+
{
10+
public static class ApplicationBuilderExtensions
11+
{
12+
public static IApplicationBuilder UseSwagger(this IApplicationBuilder builder)
13+
{
14+
15+
var optionsAccessor = builder.ApplicationServices.GetRequiredService<IOptions<SwaggerOptions>>();
16+
17+
18+
var options = optionsAccessor.Value ?? new SwaggerOptions();
19+
20+
21+
//var swaggerProvider = builder.ApplicationServices.GetRequiredService<ISwaggerApiInfoProvider>();
22+
//swaggerProvider.ExtractApiInfo();
23+
24+
builder.UseMiddleware<SwaggerMiddleware>(options);
25+
26+
return builder;
27+
}
28+
29+
public static IApplicationBuilder UseSwaggerUI(this IApplicationBuilder app, Action<SwaggerUIOptions> setupAction = null)
30+
{
31+
if (setupAction == null)
32+
{
33+
// Don't pass options so it can be configured/injected via DI container instead
34+
app.UseMiddleware<SwaggerUIMiddleware>();
35+
}
36+
else
37+
{
38+
// Configure an options instance here and pass directly to the middleware
39+
var options = new SwaggerUIOptions();
40+
setupAction.Invoke(options);
41+
42+
app.UseMiddleware<SwaggerUIMiddleware>(options);
43+
}
44+
45+
return app;
46+
}
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace System.Collections.Generic
6+
{
7+
public static class EnumerableExtensions
8+
{
9+
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
10+
{
11+
if (collection == null || action == null)
12+
return;
13+
14+
foreach (var item in collection)
15+
action(item);
16+
}
17+
}
18+
}

‎src/Grpc.HttpApi/Extensions/GrpcSwaggerServiceExtensions.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ public static class GrpcSwaggerServiceExtensions
1818
{
1919
/// <summary>
2020
/// Adds gRPC HTTP API services to the specified <see cref="IServiceCollection" />.
21-
/// </summary>
22-
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
23-
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
24-
public static IServiceCollection AddGrpcSwagger(this IServiceCollection services)
21+
/// </summary>
22+
public static IServiceCollection AddGrpcSwagger(this IServiceCollection services, Action<SwaggerOptions> configureOptions =null)
2523
{
2624
if (services == null)
2725
{
@@ -42,7 +40,11 @@ public static IServiceCollection AddGrpcSwagger(this IServiceCollection services
4240
actionDescriptorCollectionProvider ?? new EmptyActionDescriptorCollectionProvider(),
4341
apiDescriptionProvider);
4442
});
45-
43+
44+
if(configureOptions !=null)
45+
services.Configure(configureOptions);
46+
47+
services.TryAddSingleton<ISwaggerProvider, DefaultSwaggerProvider>();
4648

4749
return services;
4850
}

‎src/Grpc.HttpApi/Extensions/StringExtensions.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ namespace System
88
public static class StringExtensions
99
{
1010
private static readonly Regex _splitNameRegex = new Regex(@"[\W_]+");
11+
12+
/// <summary>
13+
/// Converts a string to use camelCase.
14+
/// </summary>
15+
/// <param name="value">The value.</param>
16+
/// <returns>The to camel case. </returns>
17+
public static string ToCamelCase(this string value)
18+
{
19+
if (string.IsNullOrEmpty(value))
20+
return value;
21+
22+
string output = ToPascalCase(value);
23+
if (output.Length > 2)
24+
return char.ToLower(output[0]) + output.Substring(1);
25+
26+
return output.ToLower();
27+
}
28+
1129
/// <summary>
1230
/// Converts a string to use PascalCase.
1331
/// </summary>
@@ -61,6 +79,7 @@ public static string ToPascalCase(this string value, Regex splitRegex)
6179
return output.ToString();
6280
}
6381

82+
6483
/// <summary>
6584
/// Does string contain both uppercase and lowercase characters?
6685
/// </summary>
@@ -76,10 +95,10 @@ public static bool IsMixedCase(this string s)
7695

7796
return containsLower && containsUpper;
7897
}
98+
7999
public static bool IsNullOrEmpty(this string item)
80100
{
81101
return String.IsNullOrEmpty(item);
82102
}
83-
84103
}
85104
}

‎src/Grpc.HttpApi/Grpc.HttpApi.csproj

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,5 @@
1818
</ItemGroup>
1919
<ItemGroup>
2020
<EmbeddedResource Include="Swagger/UI/**/*" />
21-
</ItemGroup>
22-
<ItemGroup>
23-
<None Remove="Protos\error.proto" />
24-
</ItemGroup>
21+
</ItemGroup>
2522
</Project>

‎src/Grpc.HttpApi/GrpcHttpMetadata.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Google.Protobuf.Reflection;
22
using System;
33
using System.Collections.Generic;
4+
using System.Reflection;
45
using System.Text;
56

67
namespace Grpc.HttpApi
@@ -14,14 +15,24 @@ public class GrpcHttpMetadata
1415
/// Creates a new instance of <see cref="GrpcHttpMetadata"/> with the provided Protobuf
1516
/// <see cref="Google.Protobuf.Reflection.MethodDescriptor"/> and <see cref="Google.Api.HttpRule"/>.
1617
/// </summary>
18+
/// <param name="handerMethod">method to handler the request.</param>
1719
/// <param name="methodDescriptor">The Protobuf <see cref="Google.Protobuf.Reflection.MethodDescriptor"/>.</param>
1820
/// <param name="httpApiOption">The <see cref="Grpc.HttpApi.HttpApiOption"/>.</param>
19-
public GrpcHttpMetadata(MethodDescriptor methodDescriptor, HttpApiOption httpApiOption)
21+
public GrpcHttpMetadata(MethodInfo handerMethod, MethodDescriptor methodDescriptor, HttpApiOption httpApiOption)
2022
{
23+
HanderMethod = handerMethod;
2124
MethodDescriptor = methodDescriptor;
2225
HttpApiOption = httpApiOption;
2326
}
2427

28+
public Type HanderServiceType {
29+
get {
30+
return this.HanderMethod?.DeclaringType;
31+
}
32+
}
33+
34+
public MethodInfo HanderMethod{ get; }
35+
2536
/// <summary>
2637
/// Gets the Protobuf <see cref="Google.Protobuf.Reflection.MethodDescriptor"/>.
2738
/// </summary>

‎src/Grpc.HttpApi/HttpApiServiceMethodProvider.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Google.Protobuf.Reflection;
22
using Grpc.AspNetCore.Server;
33
using Grpc.AspNetCore.Server.Model;
4-
using Grpc.HttpApi.Contracts;
54
using Grpc.HttpApi.Internal;
65
using Microsoft.Extensions.Logging;
76
using Microsoft.Extensions.Options;

‎src/Grpc.HttpApi/Internal/HttpApiProviderServiceBinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private bool TryGetMethodDescriptor(string methodName, [NotNullWhen(true)]out Me
161161

162162
// Add protobuf service method descriptor.
163163
// Is used by swagger generation to identify gRPC HTTP APIs.
164-
metadata.Add(new GrpcHttpMetadata(methodDescriptor, httpApiOption));
164+
metadata.Add(new GrpcHttpMetadata(handlerMethod, methodDescriptor, httpApiOption));
165165

166166
return (invoker, metadata);
167167
}

0 commit comments

Comments
 (0)
Please sign in to comment.