Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions frameworks/CSharp/sisk/sisk-cadente/Program.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
using System.Net;
using System.Buffers.Text;
using System.Net;
using System.Text;
using System.Text.Json;
using Sisk.Cadente;

HttpHost.QueueSize = 4096;

var host = new HttpHost ( new IPEndPoint ( IPAddress.Any, 8080 ) );
host.ContextCreated += Host_ContextCreated;
using var host = new HttpHost ( new IPEndPoint ( IPAddress.Any, 8080 ) );
host.Handler = new DefaultHandler();

host.Start ();
Thread.Sleep ( Timeout.Infinite );

void Host_ContextCreated ( HttpHost sender, HttpHostContext session ) {
var request = session.Request;

if (request.Path == "/plaintext") {
SerializePlainTextResponse ( session.Response );
}
else if (request.Path == "/json") {
SerializeJsonResponse ( session.Response );
}
else {
session.Response.StatusCode = 404;
}
}

static void SerializePlainTextResponse ( HttpHostContext.HttpResponse response ) {

var messageBytes = Encoding.UTF8.GetBytes ( "Hello, World!" );

response.Headers.Add ( new HttpHeader ( "Content-Type", "text/plain; charset=UTF-8" ) );
response.ResponseStream = new MemoryStream ( messageBytes );
}
class DefaultHandler : HttpHostHandler
{
public override async Task OnContextCreatedAsync(HttpHost host, HttpHostContext context)
{
var request = context.Request;
var response = context.Response;

if (request.Path == "/plaintext")
{
var contentBytes = Encoding.UTF8.GetBytes("Hello, World!");

await SerializeResponseAsync(response, contentBytes, "text/plain; charset=utf-8");
}
else if (request.Path == "/json")
{
var contentBytes = JsonSerializer.SerializeToUtf8Bytes(new
{
message = "Hello, World!"
});

await SerializeResponseAsync(response, contentBytes, "application/json; charset=utf-8");
}
else
{
response.StatusCode = 404;
}
}

static void SerializeJsonResponse ( HttpHostContext.HttpResponse response ) {
static async ValueTask SerializeResponseAsync(HttpHostContext.HttpResponse response, Memory<byte> content, string contentType)
{
response.Headers.Add(new HttpHeader("Content-Type", contentType));
response.Headers.Add(new HttpHeader("Content-Length", content.Length.ToString()));

var contentBytes = JsonSerializer.SerializeToUtf8Bytes ( new {
message = "Hello, World!"
} );

response.Headers.Add ( new HttpHeader ( "Content-Type", "application/json" ) );
response.ResponseStream = new MemoryStream ( contentBytes );
using var responseStream = await response.GetResponseStreamAsync();
await responseStream.WriteAsync(content);
}
}
2 changes: 1 addition & 1 deletion frameworks/CSharp/sisk/sisk-cadente/sisk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sisk.Cadente" Version="0.1.64-alpha4" />
<PackageReference Include="Sisk.Cadente" Version="1.0.0-beta4" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions frameworks/CSharp/sisk/sisk.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ RUN dotnet restore -r linux-musl-x64
COPY sisk/ .
RUN dotnet publish -c release -o /app -r linux-musl-x64

ENV DOTNET_GCDynamicAdaptationMode=0
ENV DOTNET_ReadyToRun=0
ENV DOTNET_HillClimbing_Disable=1

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
Expand Down
Loading