2023-04-30 20:25:40 +00:00
|
|
|
using Elsa.EntityFrameworkCore.Extensions;
|
|
|
|
|
using Elsa.EntityFrameworkCore.Modules.Labels;
|
|
|
|
|
using Elsa.EntityFrameworkCore.Modules.Management;
|
|
|
|
|
using Elsa.EntityFrameworkCore.Modules.Runtime;
|
|
|
|
|
using Elsa.Extensions;
|
2023-08-29 21:29:31 +00:00
|
|
|
using Elsa.ProtoActor.ProtoBuf;
|
2023-04-30 20:25:40 +00:00
|
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
using Proto.Cluster.AzureContainerApps;
|
2023-08-28 20:44:43 +00:00
|
|
|
using Proto.Cluster.AzureContainerApps.ClusterProviders;
|
2023-07-14 17:29:43 +00:00
|
|
|
using Proto.Cluster.AzureContainerApps.Stores.Redis;
|
2023-08-28 20:44:43 +00:00
|
|
|
using Proto.Cluster.AzureContainerApps.Utils;
|
2023-04-30 20:25:40 +00:00
|
|
|
using Proto.Persistence.Sqlite;
|
|
|
|
|
using Proto.Remote;
|
|
|
|
|
using Proto.Remote.GrpcNet;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
var services = builder.Services;
|
|
|
|
|
var configuration = builder.Configuration;
|
|
|
|
|
var sqliteConnectionString = configuration.GetConnectionString("Sqlite")!;
|
2023-07-14 17:29:43 +00:00
|
|
|
var redisConnectionString = configuration.GetConnectionString("Redis")!;
|
2023-04-30 20:25:40 +00:00
|
|
|
var identitySection = configuration.GetSection("Identity");
|
|
|
|
|
var identityTokenSection = identitySection.GetSection("Tokens");
|
|
|
|
|
var protoActorSection = configuration.GetSection("ProtoActor");
|
|
|
|
|
var protoActorClusterSection = protoActorSection.GetSection("Cluster");
|
|
|
|
|
|
|
|
|
|
// Configure Proto Actor cluster provider services.
|
2023-07-14 17:29:43 +00:00
|
|
|
services.AddAzureContainerAppsProvider(
|
|
|
|
|
ArmClientProviders.DefaultAzureCredential,
|
|
|
|
|
sc => sc.AddRedisClusterMemberStore(redisConnectionString),
|
|
|
|
|
options => protoActorClusterSection.GetSection("AzureContainerApps").Bind(options));
|
2023-04-30 20:25:40 +00:00
|
|
|
|
|
|
|
|
// Add Elsa services.
|
|
|
|
|
services
|
|
|
|
|
.AddElsa(elsa => elsa
|
|
|
|
|
.AddActivitiesFrom<Program>()
|
|
|
|
|
.UseIdentity(identity =>
|
|
|
|
|
{
|
|
|
|
|
identity.TokenOptions = options => identityTokenSection.Bind(options);
|
|
|
|
|
identity.UseConfigurationBasedUserProvider(options => identitySection.Bind(options));
|
|
|
|
|
identity.UseConfigurationBasedApplicationProvider(options => identitySection.Bind(options));
|
|
|
|
|
identity.UseConfigurationBasedRoleProvider(options => identitySection.Bind(options));
|
|
|
|
|
})
|
|
|
|
|
.UseDefaultAuthentication()
|
|
|
|
|
.UseWorkflowManagement(management =>
|
|
|
|
|
{
|
|
|
|
|
// Use EF core for workflow definitions and instances.
|
|
|
|
|
management.UseEntityFrameworkCore(m => m.UseSqlite(sqliteConnectionString));
|
|
|
|
|
})
|
|
|
|
|
.UseWorkflowRuntime(runtime =>
|
|
|
|
|
{
|
|
|
|
|
// Use EF core for triggers and bookmarks.
|
|
|
|
|
runtime.UseEntityFrameworkCore(ef => ef.UseSqlite(sqliteConnectionString));
|
|
|
|
|
|
|
|
|
|
// Use Proto.Actor for workflow execution.
|
|
|
|
|
runtime.UseProtoActor(protoActor =>
|
|
|
|
|
{
|
2023-08-28 20:44:43 +00:00
|
|
|
var advertisedHost = IPUtils.FindSmallestIpAddress().ToString();
|
2023-04-30 20:25:40 +00:00
|
|
|
|
|
|
|
|
protoActor.ClusterProvider = sp => sp.GetRequiredService<AzureContainerAppsProvider>();
|
|
|
|
|
|
|
|
|
|
protoActor.RemoteConfig = _ => GrpcNetRemoteConfig
|
|
|
|
|
.BindTo(advertisedHost)
|
|
|
|
|
.WithProtoMessages(EmptyReflection.Descriptor)
|
2023-08-29 21:29:31 +00:00
|
|
|
.WithProtoMessages(SharedReflection.Descriptor)
|
2023-04-30 20:25:40 +00:00
|
|
|
.WithLogLevelForDeserializationErrors(LogLevel.Critical)
|
|
|
|
|
.WithRemoteDiagnostics(true); // required by proto.actor dashboard
|
|
|
|
|
|
|
|
|
|
protoActor.PersistenceProvider = _ => new SqliteProvider(new SqliteConnectionStringBuilder(sqliteConnectionString));
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.UseLabels(labels => labels.UseEntityFrameworkCore(ef => ef.UseSqlite(sqliteConnectionString)))
|
|
|
|
|
.UseScheduling()
|
|
|
|
|
.UseWorkflowsApi(api => api.AddFastEndpointsAssembly<Program>())
|
|
|
|
|
.UseJavaScript()
|
|
|
|
|
.UseLiquid()
|
|
|
|
|
.UseHttp()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
services.AddHealthChecks();
|
|
|
|
|
services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));
|
|
|
|
|
|
|
|
|
|
// Configure middleware pipeline.
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
// CORS.
|
|
|
|
|
app.UseCors();
|
|
|
|
|
|
|
|
|
|
// Health checks.
|
|
|
|
|
app.MapHealthChecks("/");
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
// Elsa API endpoints for designer.
|
|
|
|
|
app.UseWorkflowsApi();
|
|
|
|
|
|
|
|
|
|
// Captures unhandled exceptions and returns a JSON response.
|
|
|
|
|
app.UseJsonSerializationErrorHandler();
|
|
|
|
|
|
|
|
|
|
// Elsa HTTP Endpoint activities
|
|
|
|
|
app.UseWorkflows();
|
|
|
|
|
|
|
|
|
|
// Run.
|
|
|
|
|
app.Run();
|