Skip to content

Commit

Permalink
adding RhinoCompute.cs and link to csharp sdk endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
andyopayne committed Apr 22, 2024
1 parent ca2668b commit bd2e76c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
26 changes: 0 additions & 26 deletions src/compute.geometry/FixedEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void AddRoutes(IEndpointRouteBuilder app)
app.MapGet("servertime", ServerTime);
app.MapGet("plugins/rhino/installed", GetInstalledPluginsRhino);
app.MapGet("plugins/gh/installed", GetInstalledPluginsGrasshopper);
//Get["sdk/csharp"] = _ => CSharpSdk(Context);
}

static void HomePage(HttpContext context)
Expand Down Expand Up @@ -81,31 +80,6 @@ static async Task GetInstalledPluginsGrasshopper(HttpContext ctx)
ctx.Response.ContentType = "application/json";
await ctx.Response.WriteAsJsonAsync(ghPluginInfo);
}

/*
static async Task CSharpSdk(HttpContext ctx)
{
string content = "";
using (var resourceStream = typeof(FixedEndPointsModule).Assembly.GetManifestResourceStream("compute.geometry.RhinoCompute.cs"))
{
var stream = new System.IO.StreamReader(resourceStream);
content = stream.ReadToEnd();
stream.Close();
}
var response = new Response();
response.Headers.Add("Content-Disposition", "attachment; filename=RhinoCompute.cs");
response.ContentType = "text/plain";
response.Contents = stream => {
using (var writer = new System.IO.StreamWriter(stream))
{
writer.Write(content);
}
};
return response.AsAttachment("RhinoCompute.cs", "text/plain" );
}
*/
}
}

39 changes: 30 additions & 9 deletions src/compute.geometry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Serilog;
using Carter;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using System.Runtime.InteropServices;

using System.IO;


namespace compute.geometry
{
Expand Down Expand Up @@ -125,22 +126,47 @@ static void ParseCommandLineArgs(string[] args)
}
}


public class RhinoGetModule : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{
app.MapGet("/sdk", context => SdkEndpoint(context, app));

app.MapGet("/sdk/csharp", context => CSharpSdk(context));

foreach (var endpoint in GeometryEndPoint.AllEndPoints)
{
app.MapGet(endpoint.PathURL, endpoint.Get);
}
}

static async Task CSharpSdk(HttpContext context)
{
context.Response.ContentType = "text/plain";
string fileContents;
using (Stream resourceStream = typeof(FixedEndPointsModule).Assembly.GetManifestResourceStream("compute.geometry.RhinoCompute.cs"))
{
if (resourceStream != null)
{
using (StreamReader reader = new StreamReader(resourceStream))
{
fileContents = await reader.ReadToEndAsync();
}
}
else
{
context.Response.StatusCode = 404;
return;
}
}
var result = new StringBuilder();
result.AppendLine(fileContents);
await context.Response.WriteAsync(result.ToString());
}

static async Task SdkEndpoint(HttpContext context, IEndpointRouteBuilder app)
{
var result = new StringBuilder("<!DOCTYPE html><html><body>");
result.AppendLine($" <a href=\"/sdk/csharp\">C# SDK</a><BR>");
result.AppendLine("<p>API<br>");
int route_index = 0;
var sources = app.DataSources;
Expand All @@ -166,11 +192,6 @@ static async Task SdkEndpoint(HttpContext context, IEndpointRouteBuilder app)
result.AppendLine($"{route_index} <a href='{path}'>{displayName}</a><BR>");
}
}
//foreach (var endpoint in GeometryEndPoint.AllEndPoints)
//{
// route_index += 1;
// result.AppendLine($"{route_index} <a href='{endpoint.PathURL}'>{endpoint.Path}</a><BR>");
//}
result.AppendLine("</p></body></html>");
await context.Response.WriteAsync(result.ToString());
}
Expand Down
8 changes: 8 additions & 0 deletions src/compute.geometry/compute.geometry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Remove="RhinoCompute.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="RhinoCompute.cs">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Carter" Version="7.0.0" />
Expand Down

0 comments on commit bd2e76c

Please sign in to comment.