114 lines
3.1 KiB
C#
114 lines
3.1 KiB
C#
using AS1024.NetworkQuality.Server;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
const string schemeNetQuality = "https";
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.WebHost.ConfigureKestrel(options => {
|
|
options.Limits.MaxRequestBodySize = long.MaxValue;
|
|
}).UseKestrelHttpsConfiguration();
|
|
|
|
builder.Services.AddLogging();
|
|
builder.Logging.AddConsole();
|
|
|
|
var app = builder.Build();
|
|
|
|
app.MapGet("/api/v1/config", (HttpRequest request) =>
|
|
{
|
|
string host = request.Host.Host;
|
|
int port = request.Host.Port ?? 443;
|
|
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, ILogger<Program> logger) =>
|
|
{
|
|
response.ContentType = "application/octet-stream";
|
|
return Results.Ok();
|
|
});
|
|
|
|
app.MapGet("/api/v1/large", async (HttpRequest request, HttpResponse response, ILogger<Program> logger) =>
|
|
{
|
|
response.ContentType = "application/octet-stream";
|
|
|
|
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", (HttpRequest request, ILogger<Program> logger) =>
|
|
{
|
|
try
|
|
{
|
|
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);
|
|
}
|
|
});
|
|
|
|
app.Run();
|
|
|
|
[JsonSerializable(typeof(ConfigResponse))]
|
|
[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ProblemDetails))]
|
|
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default,
|
|
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
{
|
|
|
|
} |