165 lines
4.3 KiB
C#
165 lines
4.3 KiB
C#
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.
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddOpenApi();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
var summaries = new[]
|
|
{
|
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
};
|
|
|
|
app.MapGet("/weatherforecast", () =>
|
|
{
|
|
var forecast = Enumerable.Range(1, 5).Select(index =>
|
|
new WeatherForecast
|
|
(
|
|
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
Random.Shared.Next(-20, 55),
|
|
summaries[Random.Shared.Next(summaries.Length)]
|
|
))
|
|
.ToArray();
|
|
return forecast;
|
|
})
|
|
.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; }
|
|
}
|