2023-08-30 17:09:05 +00:00
using System.Text.Encodings.Web ;
2023-10-07 10:47:36 +00:00
using Elsa.Alterations.Extensions ;
2023-10-17 06:51:45 +00:00
using Elsa.Alterations.MassTransit.Extensions ;
2023-08-30 20:01:25 +00:00
using Elsa.Dapper.Extensions ;
using Elsa.Dapper.Services ;
2023-09-23 17:41:37 +00:00
using Elsa.DropIns.Extensions ;
2023-08-29 21:29:31 +00:00
using Elsa.EntityFrameworkCore.Extensions ;
2023-10-07 10:47:36 +00:00
using Elsa.EntityFrameworkCore.Modules.Alterations ;
2023-06-28 20:46:58 +00:00
using Elsa.EntityFrameworkCore.Modules.Identity ;
using Elsa.EntityFrameworkCore.Modules.Management ;
using Elsa.EntityFrameworkCore.Modules.Runtime ;
2022-05-26 10:47:31 +00:00
using Elsa.Extensions ;
2023-09-24 22:38:06 +00:00
using Elsa.Http.Options ;
2023-06-28 20:44:51 +00:00
using Elsa.MongoDb.Extensions ;
2023-08-05 08:34:10 +00:00
using Elsa.MongoDb.Modules.Identity ;
using Elsa.MongoDb.Modules.Management ;
using Elsa.MongoDb.Modules.Runtime ;
2023-07-23 19:05:31 +00:00
using Microsoft.Data.Sqlite ;
2023-09-24 22:38:06 +00:00
using Microsoft.Extensions.Options ;
2023-07-23 19:05:31 +00:00
using Proto.Persistence.Sqlite ;
2023-08-29 21:29:31 +00:00
using Proto.Persistence.SqlServer ;
2022-05-26 10:47:31 +00:00
2023-08-05 08:34:10 +00:00
const bool useMongoDb = false ;
2023-08-30 13:42:16 +00:00
const bool useSqlServer = false ;
2023-09-06 20:04:58 +00:00
const bool useDapper = false ;
2024-01-03 08:30:24 +00:00
const bool useProtoActor = false ;
2023-08-13 10:31:58 +00:00
const bool useHangfire = false ;
2023-10-29 15:17:38 +00:00
const bool useQuartz = true ;
2024-01-30 20:22:32 +00:00
const bool useMassTransit = false ;
const bool useMassTransitAzureServiceBus = true ;
2023-11-15 19:47:11 +00:00
const bool useMassTransitRabbitMq = false ;
2023-05-16 18:03:12 +00:00
2022-05-26 10:47:31 +00:00
var builder = WebApplication . CreateBuilder ( args ) ;
var services = builder . Services ;
2022-08-31 13:47:10 +00:00
var configuration = builder . Configuration ;
var identitySection = configuration . GetSection ( "Identity" ) ;
2023-01-02 21:24:05 +00:00
var identityTokenSection = identitySection . GetSection ( "Tokens" ) ;
2023-08-30 20:01:25 +00:00
var sqliteConnectionString = configuration . GetConnectionString ( "Sqlite" ) ! ;
var sqlServerConnectionString = configuration . GetConnectionString ( "SqlServer" ) ! ;
2023-08-05 08:34:10 +00:00
var mongoDbConnectionString = configuration . GetConnectionString ( "MongoDb" ) ! ;
2023-11-15 19:47:11 +00:00
var azureServiceBusConnectionString = configuration . GetConnectionString ( "AzureServiceBus" ) ! ;
var rabbitMqConnectionString = configuration . GetConnectionString ( "RabbitMq" ) ! ;
2022-08-27 19:58:22 +00:00
2022-05-26 10:47:31 +00:00
// Add Elsa services.
services
2023-08-05 08:34:10 +00:00
. AddElsa ( elsa = >
{
2023-09-23 17:41:37 +00:00
if ( useMongoDb )
2023-08-05 08:34:10 +00:00
elsa . UseMongoDb ( mongoDbConnectionString ) ;
2023-09-23 17:41:37 +00:00
if ( useDapper )
2023-08-30 20:01:25 +00:00
elsa . UseDapper ( dapper = >
{
dapper . UseMigrations ( ) ;
dapper . DbConnectionProvider = sp = >
{
2023-09-23 17:41:37 +00:00
if ( useSqlServer )
2023-08-30 20:01:25 +00:00
return new SqlServerDbConnectionProvider ( sqlServerConnectionString ! ) ;
else
return new SqliteDbConnectionProvider ( sqliteConnectionString ) ;
} ;
} ) ;
2023-09-23 17:41:37 +00:00
2023-08-12 20:22:40 +00:00
if ( useHangfire )
elsa . UseHangfire ( ) ;
2023-09-23 17:41:37 +00:00
2023-08-05 08:34:10 +00:00
elsa
. AddActivitiesFrom < Program > ( )
2023-08-06 18:55:24 +00:00
. AddWorkflowsFrom < Program > ( )
2023-08-05 08:34:10 +00:00
. UseFluentStorageProvider ( )
2023-10-30 20:48:36 +00:00
. UseFileStorage ( )
// .UseFileStorage(sp => StorageFactory.Blobs.AzureBlobStorageWithSas(configuration.GetConnectionString("AzureStorageSasUrl")))
2023-08-05 08:34:10 +00:00
. UseIdentity ( identity = >
{
2023-09-23 17:41:37 +00:00
if ( useMongoDb )
2023-08-05 08:34:10 +00:00
identity . UseMongoDb ( ) ;
2023-08-30 20:01:25 +00:00
else if ( useDapper )
identity . UseDapper ( ) ;
2023-08-05 08:34:10 +00:00
else
2023-08-29 21:29:31 +00:00
identity . UseEntityFrameworkCore ( ef = >
{
2023-09-23 17:41:37 +00:00
if ( useSqlServer )
2023-08-29 21:29:31 +00:00
ef . UseSqlServer ( sqlServerConnectionString ! ) ;
else
ef . UseSqlite ( sqliteConnectionString ) ;
} ) ;
2023-09-23 17:41:37 +00:00
2023-08-05 08:34:10 +00:00
identity . IdentityOptions = options = > identitySection . Bind ( options ) ;
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 = >
{
2023-09-23 17:41:37 +00:00
if ( useMongoDb )
2023-08-05 08:34:10 +00:00
management . UseMongoDb ( ) ;
2023-08-30 20:01:25 +00:00
else if ( useDapper )
management . UseDapper ( ) ;
2023-08-05 08:34:10 +00:00
else
2023-08-29 21:29:31 +00:00
management . UseEntityFrameworkCore ( ef = >
{
2023-09-23 17:41:37 +00:00
if ( useSqlServer )
2023-08-29 21:29:31 +00:00
ef . UseSqlServer ( sqlServerConnectionString ! ) ;
else
ef . UseSqlite ( sqliteConnectionString ) ;
} ) ;
2023-08-05 08:34:10 +00:00
} )
. UseWorkflowRuntime ( runtime = >
{
2023-09-23 17:41:37 +00:00
if ( useMongoDb )
2023-08-05 08:34:10 +00:00
runtime . UseMongoDb ( ) ;
2023-08-30 20:01:25 +00:00
else if ( useDapper )
runtime . UseDapper ( ) ;
2023-08-05 08:34:10 +00:00
else
2023-08-29 21:29:31 +00:00
runtime . UseEntityFrameworkCore ( ef = >
{
2023-09-23 17:41:37 +00:00
if ( useSqlServer )
2023-08-29 21:29:31 +00:00
ef . UseSqlServer ( sqlServerConnectionString ! ) ;
else
ef . UseSqlite ( sqliteConnectionString ) ;
} ) ;
2023-09-23 17:41:37 +00:00
if ( useProtoActor )
2023-08-29 21:29:31 +00:00
{
runtime . UseProtoActor ( proto = > proto . PersistenceProvider = _ = >
{
2023-09-23 17:41:37 +00:00
if ( useSqlServer )
2023-08-29 21:29:31 +00:00
return new SqlServerProvider ( sqlServerConnectionString ! , true , "" , "proto_actor" ) ;
else
return new SqliteProvider ( new SqliteConnectionStringBuilder ( sqliteConnectionString ) ) ;
} ) ;
}
2023-09-23 17:41:37 +00:00
2024-01-30 20:22:32 +00:00
if ( useMassTransit )
{
runtime . UseMassTransitDispatcher ( ) ;
}
2023-08-13 10:31:58 +00:00
runtime . WorkflowInboxCleanupOptions = options = > configuration . GetSection ( "Runtime:WorkflowInboxCleanup" ) . Bind ( options ) ;
2023-08-05 08:34:10 +00:00
} )
. UseEnvironments ( environments = > environments . EnvironmentsOptions = options = > configuration . GetSection ( "Environments" ) . Bind ( options ) )
2023-08-12 20:22:40 +00:00
. UseScheduling ( scheduling = >
{
if ( useHangfire )
scheduling . UseHangfireScheduler ( ) ;
2023-10-29 15:17:38 +00:00
if ( useQuartz )
scheduling . UseQuartzScheduler ( ) ;
2023-08-12 20:22:40 +00:00
} )
2023-08-05 08:34:10 +00:00
. UseWorkflowsApi ( api = > api . AddFastEndpointsAssembly < Program > ( ) )
. UseRealTimeWorkflows ( )
2023-11-03 19:28:10 +00:00
. UseCSharp ( options = >
{
options . AppendScript ( "string Greet(string name) => $\"Hello {name}!\";" ) ;
options . AppendScript ( "string SayHelloWorld() => Greet(\"World\");" ) ;
} )
2023-11-04 17:15:56 +00:00
. UseJavaScript ( options = >
{
options . AllowClrAccess = true ;
options . ConfigureEngine ( engine = >
{
engine . Execute ( "function greet(name) { return `Hello ${name}!`; }" ) ;
engine . Execute ( "function sayHelloWorld() { return greet('World'); }" ) ;
} ) ;
} )
2023-11-19 20:13:19 +00:00
. UsePython ( python = >
2023-11-04 17:15:56 +00:00
{
2023-11-19 20:13:19 +00:00
python . PythonOptions + = options = >
{
// Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/bin/python3.11
// alternatively, you can set the PYTHONNET_PYDLL environment variable.
configuration . GetSection ( "Scripting:Python" ) . Bind ( options ) ;
} ;
2023-11-04 17:15:56 +00:00
} )
2023-08-30 20:01:25 +00:00
. UseLiquid ( liquid = > liquid . FluidOptions = options = > options . Encoder = HtmlEncoder . Default )
2023-09-24 22:38:06 +00:00
. UseHttp ( http = >
{
http . ConfigureHttpOptions = options = > configuration . GetSection ( "Http" ) . Bind ( options ) ;
} )
2023-10-07 10:47:36 +00:00
. UseEmail ( email = > email . ConfigureOptions = options = > configuration . GetSection ( "Smtp" ) . Bind ( options ) )
. UseAlterations ( alterations = >
{
alterations . UseEntityFrameworkCore ( ef = >
{
if ( useSqlServer )
ef . UseSqlServer ( sqlServerConnectionString ) ;
else
ef . UseSqlite ( sqliteConnectionString ) ;
} ) ;
2023-10-17 06:51:45 +00:00
2024-01-30 20:22:32 +00:00
if ( useMassTransit )
{
alterations . UseMassTransitDispatcher ( ) ;
}
2023-10-29 10:36:01 +00:00
} )
. UseWorkflowContexts ( ) ;
2023-09-23 17:41:37 +00:00
2023-10-29 15:17:38 +00:00
if ( useQuartz )
{
2023-11-15 19:47:11 +00:00
elsa . UseQuartz ( quartz = > { quartz . UseSqlite ( sqliteConnectionString ) ; } ) ;
2023-10-29 15:17:38 +00:00
}
2023-11-19 20:13:19 +00:00
2023-12-08 08:07:28 +00:00
if ( useMassTransit )
2023-11-15 19:47:11 +00:00
{
2023-12-08 08:07:28 +00:00
elsa . UseMassTransit ( massTransit = >
{
if ( useMassTransitAzureServiceBus )
2023-12-11 18:59:13 +00:00
{
2023-12-11 19:49:59 +00:00
massTransit . UseAzureServiceBus ( azureServiceBusConnectionString , serviceBusFeature = > serviceBusFeature . ConfigureServiceBus = bus = >
{
bus . PrefetchCount = 4 ;
bus . LockDuration = TimeSpan . FromMinutes ( 5 ) ;
bus . MaxConcurrentCalls = 32 ;
bus . MaxDeliveryCount = 8 ;
// etc.
} ) ;
2023-12-11 18:59:13 +00:00
}
2023-12-08 08:07:28 +00:00
if ( useMassTransitRabbitMq )
2023-12-11 18:59:13 +00:00
{
2023-12-11 19:49:59 +00:00
massTransit . UseRabbitMq ( rabbitMqConnectionString , rabbit = > rabbit . ConfigureServiceBus = bus = >
{
bus . PrefetchCount = 4 ;
bus . Durable = true ;
bus . AutoDelete = false ;
bus . ConcurrentMessageLimit = 32 ;
// etc.
} ) ;
2023-12-11 18:59:13 +00:00
}
2023-12-08 08:07:28 +00:00
} ) ;
}
2023-10-15 10:24:45 +00:00
2023-11-15 19:47:11 +00:00
elsa . InstallDropIns ( options = > options . DropInRootDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , "App_Data" , "DropIns" ) ) ;
2023-10-15 10:24:45 +00:00
elsa . AddSwagger ( ) ;
2024-01-04 21:07:55 +00:00
elsa . AddFastEndpointsAssembly < Program > ( ) ;
2023-08-05 08:34:10 +00:00
} ) ;
2022-05-26 10:47:31 +00:00
services . AddHealthChecks ( ) ;
2023-08-14 12:34:11 +00:00
services . AddControllers ( ) ;
2023-07-25 19:02:07 +00:00
services . AddCors ( cors = > cors . AddDefaultPolicy ( policy = > policy . AllowAnyHeader ( ) . AllowAnyMethod ( ) . AllowAnyOrigin ( ) . WithExposedHeaders ( "*" ) ) ) ;
2022-05-26 10:47:31 +00:00
2023-09-23 17:41:37 +00:00
// Build the web application.
2022-05-26 10:47:31 +00:00
var app = builder . Build ( ) ;
2022-07-26 21:30:42 +00:00
2023-09-23 17:41:37 +00:00
// Configure the pipeline.
2022-05-26 10:47:31 +00:00
if ( app . Environment . IsDevelopment ( ) )
app . UseDeveloperExceptionPage ( ) ;
// CORS.
app . UseCors ( ) ;
// Health checks.
app . MapHealthChecks ( "/" ) ;
2023-08-05 08:34:10 +00:00
// Routing used for SignalR.
app . UseRouting ( ) ;
// Security.
2022-07-21 14:18:42 +00:00
app . UseAuthentication ( ) ;
app . UseAuthorization ( ) ;
2023-04-17 13:23:05 +00:00
// Elsa API endpoints for designer.
2023-09-24 22:38:06 +00:00
var routePrefix = app . Services . GetRequiredService < IOptions < HttpActivityOptions > > ( ) . Value . ApiRoutePrefix ;
app . UseWorkflowsApi ( routePrefix ) ;
2023-04-17 13:23:05 +00:00
// Captures unhandled exceptions and returns a JSON response.
2022-05-26 10:47:31 +00:00
app . UseJsonSerializationErrorHandler ( ) ;
2023-04-17 13:23:05 +00:00
2023-11-03 19:28:10 +00:00
// Elsa HTTP Endpoint activities.
2022-12-30 12:11:29 +00:00
app . UseWorkflows ( ) ;
2022-05-26 10:47:31 +00:00
2023-11-03 19:28:10 +00:00
// Swagger API documentation.
2023-10-15 10:24:45 +00:00
if ( app . Environment . IsDevelopment ( ) )
2023-11-27 10:26:16 +00:00
{
2023-10-15 10:24:45 +00:00
app . UseSwaggerUI ( ) ;
2023-11-27 10:26:16 +00:00
}
2023-10-15 10:24:45 +00:00
2023-07-25 13:12:27 +00:00
// SignalR.
app . UseWorkflowsSignalRHubs ( ) ;
2022-05-26 10:47:31 +00:00
// Run.
2022-08-31 13:47:10 +00:00
app . Run ( ) ;