first version
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>ff020750-eacc-49ce-880e-1ecbc30605ac</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\S8n.Components.Packages\S8n.Components.Packages.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
|
||||
// Ensure S8n.Components.Packages assembly is loaded
|
||||
_ = typeof(S8n.Components.Basics.Calculator).Assembly;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
@@ -33,9 +39,126 @@ app.MapGet("/weatherforecast", () =>
|
||||
})
|
||||
.WithName("GetWeatherForecast");
|
||||
|
||||
// Runtime execution endpoint
|
||||
app.MapPost("/api/runtime/execute", (RuntimeRequest request) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the type from the class name
|
||||
var type = Type.GetType(request.ClassName);
|
||||
if (type == null)
|
||||
{
|
||||
// Try to find in loaded assemblies
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
type = assembly.GetType(request.ClassName);
|
||||
if (type != null) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = $"Class not found: {request.ClassName}"
|
||||
});
|
||||
}
|
||||
|
||||
// Create instance
|
||||
var instance = Activator.CreateInstance(type);
|
||||
if (instance == null)
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = $"Failed to create instance of: {request.ClassName}"
|
||||
});
|
||||
}
|
||||
|
||||
// Get the method
|
||||
var method = type.GetMethod(request.MethodName);
|
||||
if (method == null)
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = $"Method not found: {request.MethodName}"
|
||||
});
|
||||
}
|
||||
|
||||
// Parse inputs and invoke method
|
||||
var inputs = request.Inputs as JsonElement?;
|
||||
object? result;
|
||||
|
||||
if (inputs.HasValue)
|
||||
{
|
||||
// Get method parameters
|
||||
var parameters = method.GetParameters();
|
||||
var args = new List<object?>();
|
||||
|
||||
foreach (var param in parameters)
|
||||
{
|
||||
if (inputs.Value.TryGetProperty(param.Name!, out var property))
|
||||
{
|
||||
var value = JsonSerializer.Deserialize(property.GetRawText(), param.ParameterType);
|
||||
args.Add(value);
|
||||
}
|
||||
else if (param.IsOptional)
|
||||
{
|
||||
args.Add(param.DefaultValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = $"Missing required parameter: {param.Name}"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
result = method.Invoke(instance, args.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
result = method.Invoke(instance, null);
|
||||
}
|
||||
|
||||
return Results.Ok(new RuntimeResponse
|
||||
{
|
||||
Outputs = result
|
||||
});
|
||||
}
|
||||
catch (TargetInvocationException tie) when (tie.InnerException != null)
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = tie.InnerException.Message
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Results.BadRequest(new RuntimeResponse
|
||||
{
|
||||
Error = ex.Message
|
||||
});
|
||||
}
|
||||
})
|
||||
.WithName("ExecuteRuntime");
|
||||
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
|
||||
record RuntimeRequest
|
||||
{
|
||||
public string ClassName { get; set; } = string.Empty;
|
||||
public string MethodName { get; set; } = string.Empty;
|
||||
public object? Inputs { get; set; }
|
||||
}
|
||||
|
||||
record RuntimeResponse
|
||||
{
|
||||
public object? Outputs { get; set; }
|
||||
public string? Error { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user