84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
using AS1024.NetworkQuality.Server;
|
|
using System.Text.Json.Serialization;
|
|
const string schemeNetQuality = "https";
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
|
{
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.MapGet("/api/v1/config", (HttpRequest request) =>
|
|
{
|
|
string host = request.Host.Host;
|
|
int port = request.Host.Port ??
|
|
(request.IsHttps ? 443 : 80);
|
|
Uri smallDownloadUrl = new UriBuilder
|
|
{
|
|
Scheme = schemeNetQuality,
|
|
Host = host,
|
|
Port = port,
|
|
Path = "/api/v1/small"
|
|
}.Uri;
|
|
|
|
Uri largeDownloadUrl = new UriBuilder
|
|
{
|
|
Scheme = schemeNetQuality,
|
|
Host = host,
|
|
Port = port,
|
|
Path = "/api/v1/large"
|
|
}.Uri;
|
|
|
|
Uri uploadUrl = new UriBuilder
|
|
{
|
|
Scheme = schemeNetQuality,
|
|
Host = host,
|
|
Port = port,
|
|
Path = "/api/v1/upload"
|
|
}.Uri;
|
|
|
|
ConfigResponse config = new()
|
|
{
|
|
Version = 1,
|
|
Urls = new Urls
|
|
{
|
|
SmallDownloadUrl = smallDownloadUrl.AbsoluteUri,
|
|
LargeDownloadUrl = largeDownloadUrl.AbsoluteUri,
|
|
UploadUrl = uploadUrl.AbsoluteUri
|
|
}
|
|
};
|
|
|
|
return Results.Json(config,
|
|
AppJsonSerializerContext.Default);
|
|
});
|
|
|
|
app.MapGet("/api/v1/small", (HttpResponse response) =>
|
|
{
|
|
response.ContentType = "application/octet-stream";
|
|
return Results.File(new byte[] { 0 });
|
|
});
|
|
|
|
app.MapGet("/api/v1/large", (HttpResponse response) =>
|
|
{
|
|
response.ContentType = "application/octet-stream";
|
|
return Results.Stream(new LargeStreamResult());
|
|
});
|
|
|
|
app.MapPost("/api/v1/upload", async (HttpRequest request) =>
|
|
{
|
|
using (var stream = new System.IO.MemoryStream())
|
|
{
|
|
await request.Body.CopyToAsync(stream);
|
|
}
|
|
return Results.Ok();
|
|
});
|
|
|
|
app.Run();
|
|
|
|
|
|
[JsonSerializable(typeof(ConfigResponse))]
|
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
{
|
|
|
|
} |