-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (52 loc) · 2.33 KB
/
Copy pathProgram.cs
File metadata and controls
64 lines (52 loc) · 2.33 KB
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
namespace TemporalioSamples.Encryption.CodecServer;
using Google.Protobuf;
using Temporalio.Api.Common.V1;
using Temporalio.Converters;
using TemporalioSamples.Encryption.Codec;
public sealed class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Setup console logging, codec, and cors
builder.Logging.AddSimpleConsole().SetMinimumLevel(LogLevel.Information);
builder.Services.AddSingleton<IPayloadCodec>(ctx => new EncryptionCodec());
builder.Services.AddCors();
var app = builder.Build();
// We need CORS so that the browser can access this endpoint from a
// different origin
app.UseCors(
builder => builder.
WithHeaders("content-type", "x-namespace").
WithMethods("POST").
// This list may need to be customized based on where the UI
// is communicating from
WithOrigins("http://localhost:8080", "http://localhost:8233", "https://cloud.temporal.io"));
// These are the endpoints called for encrypt/decrypt
app.MapPost("/encode", EncodeAsync);
app.MapPost("/decode", DecodeAsync);
app.Run();
}
private static Task<IResult> EncodeAsync(
HttpContext ctx, IPayloadCodec codec) => ApplyCodecFuncAsync(ctx, codec.EncodeAsync);
private static Task<IResult> DecodeAsync(
HttpContext ctx, IPayloadCodec codec) => ApplyCodecFuncAsync(ctx, codec.DecodeAsync);
private static async Task<IResult> ApplyCodecFuncAsync(
HttpContext ctx, Func<IReadOnlyCollection<Payload>, Task<IReadOnlyCollection<Payload>>> func)
{
// Read payloads as JSON
if (ctx.Request.ContentType?.StartsWith("application/json") != true)
{
return Results.StatusCode(StatusCodes.Status415UnsupportedMediaType);
}
Payloads inPayloads;
using (var reader = new StreamReader(ctx.Request.Body))
{
inPayloads = JsonParser.Default.Parse<Payloads>(await reader.ReadToEndAsync());
}
// Apply codec func
var outPayloads = new Payloads() { Payloads_ = { await func(inPayloads.Payloads_) } };
// Return JSON
return Results.Text(JsonFormatter.Default.Format(outPayloads), "application/json");
}
}