Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyongshim committed Feb 18, 2024
1 parent 130d8f0 commit d165862
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,53 @@ public class ResponseAddTraceIdFilter : IEndpointFilter
contentType: v switch
{
IContentTypeHttpResult c => c.ContentType,
_ => "application/json"
_ => MediaTypeNames.Application.Json
},
statusCode: v switch
{
IStatusCodeHttpResult c => c.StatusCode,
_ => 200
}
),
ContentHttpResult { ResponseContent: not null } v => Results.Text
(
AddTraceStringId(v.ResponseContent).ToJsonString(serializerOptions),
contentType: v.ContentType ?? MediaTypeNames.Application.Json,
statusCode: v.StatusCode
),
string v => Results.Text
(
AddTraceStringId(v).ToJsonString(serializerOptions),
contentType: MediaTypeNames.Application.Json,
statusCode: 200
),
IResult v => Results.Text
(
$$"""{"traceId":"{{id}}"}""",
contentType: "application/json",
contentType: MediaTypeNames.Application.Json,
statusCode: v switch
{
IStatusCodeHttpResult c => c.StatusCode,
_ => 200
}
),
{ } v => Results.Text(AddTraceId(v).ToJsonString(serializerOptions), "application/json", statusCode: 200),
{ } v => Results.Text
(
AddTraceId(v).ToJsonString(serializerOptions),
MediaTypeNames.Application.Json,
statusCode: 200
),
_ => ret
};

JsonNode AddTraceStringId(string value)
{
var root = JsonNode.Parse(value)!.Root;
root["traceId"] = id;

return root;
}

JsonNode AddTraceId(object? value)
{
var root = JsonSerializer.SerializeToNode(value, serializerOptions)!.Root;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;

namespace WebApplicationMinimalApi8.ExceptionHandlers;
Expand All @@ -10,7 +11,11 @@ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception e

await Results.Problem(
title: exception.Message,
statusCode: StatusCodes.Status500InternalServerError
statusCode: StatusCodes.Status500InternalServerError,
extensions: new Dictionary<string, object?>
{
["traceId"] = Activity.Current?.Id ?? httpContext.TraceIdentifier
}
).ExecuteAsync(httpContext);

return true;
Expand Down
21 changes: 19 additions & 2 deletions src/WebApplicationMinimalApi8/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
};
});
builder.Services.AddValidatorsFromAssemblyContaining<Program>();

builder.AddFluentValidationEndpointFilter();


builder.Services.ConfigureHttpClientDefaults(builder =>
{
builder.AddStandardResilienceHandler();
});
builder.Services.AddHttpClient("echo", client =>
{
client.BaseAddress = new("https://httpbin.org");
Expand Down Expand Up @@ -92,6 +94,21 @@
.WithDescription("사람을 생성합니다.")
.WithOpenApi();

root.MapPost("/echo", async (HttpContext ctx, JsonDocument json) =>
{
using var httpclient = ctx.RequestServices.GetRequiredService<IHttpClientFactory>().CreateClient("echo");
using var res = await httpclient.PostAsJsonAsync("post", json);
return await res.Content.ReadAsStringAsync();
//return Results.Text
//(
// await res.Content.ReadAsStringAsync(),
// contentType: res.Content.Headers.ContentType?.MediaType,
// statusCode: (int)res.StatusCode
//);
});

app.Run();

static void InterceptNullSetter(JsonTypeInfo typeInfo)
Expand Down

0 comments on commit d165862

Please sign in to comment.