Initial commit to match the tool provided by Apple on GitHub
This commit is contained in:
parent
caaa8dcccf
commit
65f50ee840
|
|
@ -1,19 +1,22 @@
|
||||||
using AS1024.NetworkQuality.Server;
|
using AS1024.NetworkQuality.Server;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
const string schemeNetQuality = "https";
|
const string schemeNetQuality = "https";
|
||||||
var builder = WebApplication.CreateSlimBuilder(args);
|
var builder = WebApplication.CreateSlimBuilder(args);
|
||||||
|
|
||||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
builder.WebHost.ConfigureKestrel(options => {
|
||||||
{
|
options.Limits.MaxRequestBodySize = long.MaxValue;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.Services.AddLogging();
|
||||||
|
builder.Logging.AddConsole();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.MapGet("/api/v1/config", (HttpRequest request) =>
|
app.MapGet("/api/v1/config", (HttpRequest request) =>
|
||||||
{
|
{
|
||||||
string host = request.Host.Host;
|
string host = request.Host.Host;
|
||||||
int port = request.Host.Port ??
|
int port = request.Host.Port ?? 443;
|
||||||
(request.IsHttps ? 443 : 80);
|
|
||||||
Uri smallDownloadUrl = new UriBuilder
|
Uri smallDownloadUrl = new UriBuilder
|
||||||
{
|
{
|
||||||
Scheme = schemeNetQuality,
|
Scheme = schemeNetQuality,
|
||||||
|
|
@ -49,8 +52,7 @@ app.MapGet("/api/v1/config", (HttpRequest request) =>
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return Results.Json(config,
|
return Results.Json(config, AppJsonSerializerContext.Default);
|
||||||
AppJsonSerializerContext.Default);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.MapGet("/api/v1/small", (HttpResponse response) =>
|
app.MapGet("/api/v1/small", (HttpResponse response) =>
|
||||||
|
|
@ -59,25 +61,57 @@ app.MapGet("/api/v1/small", (HttpResponse response) =>
|
||||||
return Results.File(new byte[] { 0 });
|
return Results.File(new byte[] { 0 });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.MapGet("/api/v1/large", (HttpResponse response) =>
|
app.MapGet("/api/v1/large", async (HttpRequest request, HttpResponse response, ILogger<Program> logger) =>
|
||||||
{
|
{
|
||||||
response.ContentType = "application/octet-stream";
|
response.ContentType = "application/octet-stream";
|
||||||
return Results.Stream(new LargeStreamResult());
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var largeStream = new LargeStreamResult();
|
||||||
|
await largeStream.CopyToAsync(response.Body);
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "An error occurred while writing the large file content.");
|
||||||
|
response.StatusCode = 500;
|
||||||
|
await response.WriteAsync("An error occurred while writing the large file content.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "An unexpected error occurred.");
|
||||||
|
response.StatusCode = 500;
|
||||||
|
await response.WriteAsync("An unexpected error occurred.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.MapPost("/api/v1/upload", async (HttpRequest request) =>
|
app.MapPost("/api/v1/upload", async (HttpRequest request, ILogger<Program> logger) =>
|
||||||
{
|
{
|
||||||
using (var stream = new System.IO.MemoryStream())
|
try
|
||||||
{
|
{
|
||||||
await request.Body.CopyToAsync(stream);
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
await request.Body.CopyToAsync(stream);
|
||||||
|
}
|
||||||
|
return Results.Ok();
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "An error occurred while reading the request body.");
|
||||||
|
return Results.Problem("An error occurred while reading the request body.", statusCode: 500);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "An unexpected error occurred.");
|
||||||
|
return Results.Problem("An unexpected error occurred.", statusCode: 500);
|
||||||
}
|
}
|
||||||
return Results.Ok();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
|
||||||
[JsonSerializable(typeof(ConfigResponse))]
|
[JsonSerializable(typeof(ConfigResponse))]
|
||||||
|
[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ProblemDetails))]
|
||||||
|
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default,
|
||||||
|
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
||||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@
|
||||||
|
|
||||||
public class Urls
|
public class Urls
|
||||||
{
|
{
|
||||||
|
public string SmallHttpsDownloadUrl => SmallDownloadUrl;
|
||||||
|
public string LargeHttpsDownloadUrl => LargeDownloadUrl;
|
||||||
|
public string HttpsUploadUrl => UploadUrl;
|
||||||
public required string SmallDownloadUrl { get; set; }
|
public required string SmallDownloadUrl { get; set; }
|
||||||
public required string LargeDownloadUrl { get; set; }
|
public required string LargeDownloadUrl { get; set; }
|
||||||
public required string UploadUrl { get; set; }
|
public required string UploadUrl { get; set; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue