Add project files.

This commit is contained in:
Jeff Leung 2024-08-05 09:11:03 -07:00
parent c76bd8f02b
commit 9a4487165b
10 changed files with 284 additions and 0 deletions

30
.dockerignore Normal file
View File

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS1024.NetworkQuality.Server", "AS1024.NetworkQuality.Server\AS1024.NetworkQuality.Server.csproj", "{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {418E03E1-4880-487E-8E24-0A3AF27ABE76}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,29 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
# Install clang/zlib1g-dev dependencies for publishing to native
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
clang zlib1g-dev
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["AS1024.NetworkQuality.Server/AS1024.NetworkQuality.Server.csproj", "AS1024.NetworkQuality.Server/"]
RUN dotnet restore "./AS1024.NetworkQuality.Server/./AS1024.NetworkQuality.Server.csproj"
COPY . .
WORKDIR "/src/AS1024.NetworkQuality.Server"
RUN dotnet build "./AS1024.NetworkQuality.Server.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./AS1024.NetworkQuality.Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final
WORKDIR /app
EXPOSE 8080
COPY --from=publish /app/publish .
ENTRYPOINT ["./AS1024.NetworkQuality.Server"]

View File

@ -0,0 +1,44 @@
internal class LargeStreamResult : System.IO.Stream
{
private long _position = 0;
private const long _length = 8L * 1024L * 1024L * 1024L; // 8 GB
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => _length;
public override long Position { get => _position; set => _position = value; }
public override void Flush() { }
public override int Read(byte[] buffer, int offset, int count)
{
int bytesToRead = (int)Math.Min(count, _length - _position);
if (bytesToRead <= 0)
return 0;
_position += bytesToRead;
return bytesToRead;
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
switch (origin)
{
case System.IO.SeekOrigin.Begin:
_position = offset;
break;
case System.IO.SeekOrigin.Current:
_position += offset;
break;
case System.IO.SeekOrigin.End:
_position = _length + offset;
break;
}
return _position;
}
public override void SetLength(long value) { }
public override void Write(byte[] buffer, int offset, int count) { }
}

View File

@ -0,0 +1,84 @@
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
{
}

View File

@ -0,0 +1,24 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/v1/config",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5294"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/v1/config",
"environmentVariables": {
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json"
}

View File

@ -0,0 +1,15 @@
namespace AS1024.NetworkQuality.Server
{
public class ConfigResponse
{
public int Version { get; set; }
public required Urls Urls { get; set; }
}
public class Urls
{
public required string SmallDownloadUrl { get; set; }
public required string LargeDownloadUrl { get; set; }
public required string UploadUrl { get; set; }
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}