-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (38 loc) · 1.37 KB
/
Copy pathProgram.cs
File metadata and controls
46 lines (38 loc) · 1.37 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
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors();
var app = builder.Build();
app.UseCors((builder) => {
builder.SetIsOriginAllowed((origin) => true);
builder.AllowAnyMethod();
builder.AllowAnyHeader();
builder.AllowCredentials();
});
app.MapGet("/channels/{channelId}", async (ctx) => {
var channelId = ctx.Request.RouteValues["channelId"]?.ToString();
if (channelId == null)
{
ctx.Response.StatusCode = 404;
await ctx.Response.CompleteAsync();
return;
}
var (contentType, data) = await ChannelService.ReadData(channelId, ctx.RequestAborted);
ctx.Response.ContentType = contentType;
await ctx.Response.Body.WriteAsync(data, 0, data.Length);
await ctx.Response.CompleteAsync();
});
app.MapPost("/channels/{channelId}", async (ctx) => {
var channelId = ctx.Request.RouteValues["channelId"]?.ToString();
if (channelId == null)
{
ctx.Response.StatusCode = 404;
await ctx.Response.CompleteAsync();
return;
}
using var ms = new MemoryStream();
await ctx.Request.Body.CopyToAsync(ms);
ms.Seek(0, SeekOrigin.Begin);
await ChannelService.WriteData(channelId, ctx.Request.ContentType ?? "text/plain", ms.ToArray());
ctx.Response.StatusCode = 200;
await ctx.Response.CompleteAsync();
});
app.Run();