Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow DSL sample in WebApiSample and show case of Dictionary type of input #1042

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/samples/WebApiSample/WebApiSample.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
# Visual Studio Version 17
VisualStudioVersion = 17.2.32526.322
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiSample", "WebApiSample\WebApiSample.csproj", "{14489389-A65D-4993-8DE2-F51701A5AF60}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WebApiSample.DataTypes
{
public class MyDataType
{
public int Number { get; set; }

public string Home { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace WebApiSample.Providers
{
public interface IDefinitionProvider
{
IEnumerable<string> GetDefinitions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace WebApiSample.Providers
{
public class WorkflowDefinitionFileProvider : IDefinitionProvider
{
private const string DefaultTemplateSearchPattern = "*.yml";

public IEnumerable<string> GetDefinitions()
{
return LoadDefinitionFromDirectory();
}

private IEnumerable<string> LoadDefinitionFromDirectory()
{
var directory = new DirectoryInfo("./WorkflowDSL");

foreach (var file in directory.GetFiles(DefaultTemplateSearchPattern, SearchOption.AllDirectories)
.OrderBy(f => f.FullName))
{
var name = Path.GetRelativePath(directory.FullName, file.FullName);
var rawContent = string.Empty;

try
{
using var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
using var streamReader = new StreamReader(stream);
rawContent = streamReader.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception($"Fail to load template file {file.FullName}.", ex);
}

if (!string.IsNullOrEmpty(rawContent))
{
yield return rawContent;
}
}
}
}
}
77 changes: 59 additions & 18 deletions src/samples/WebApiSample/WebApiSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;

using Nest;
using Swashbuckle.AspNetCore.Swagger;
using WebApiSample.Workflows;

using WorkflowCore.Interface;
using WorkflowCore.Services.DefinitionStorage;

using WebApiSample.Steps;
using WebApiSample.Providers;
using WebApiSample.Workflows;

namespace WebApiSample
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}

public IConfiguration Configuration { get; }

public IWebHostEnvironment Environment { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.AddWorkflow(cfg =>
if (this.Environment.IsDevelopment())
{
cfg.UseMongoDB(@"mongodb://mongo:27017", "workflow");
cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows");
});
services.AddWorkflow();
}
else
{
services.AddWorkflow(cfg =>
{
cfg.UseMongoDB(@"mongodb://mongo:27017", "workflow");
cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows");
});
}

services.AddSwaggerGen(c => c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }));
services.AddSingleton<IDefinitionProvider, WorkflowDefinitionFileProvider>();

services.AddControllers();
services.AddMvc().AddNewtonsoftJson();

services.AddWorkflowDSL();

// Add workflow steps
services.AddTransient<DSLHelloWorldStep>();


services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "RunneraaS API", Version = "v1" }));
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand All @@ -48,10 +68,31 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

app.UseMvc();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

// DSL load
var loader = app.ApplicationServices.GetService<IDefinitionLoader>();
var definitionProvider = app.ApplicationServices.GetService<IDefinitionProvider>();
foreach (var def in definitionProvider.GetDefinitions())
{
try
{
loader.LoadDefinition(def, Deserializers.Yaml);
}
catch (Exception)
{
throw;
}
}

var host = app.ApplicationServices.GetService<IWorkflowHost>();
host.RegisterWorkflow<TestWorkflow, MyDataClass>();

host.Start();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace WebApiSample.Steps
{
public class DSLHelloWorldStep : StepBody
{
public JObject Home { get; set; }

public string Result { get; set; }

public override ExecutionResult Run(IStepExecutionContext context)
{
var home = Home.ToObject<Dictionary<string, string>>();

Result = string.Join(";", home.Select(x => x.Key + "=" + x.Value).ToArray());

return ExecutionResult.Next();
}
}
}
35 changes: 26 additions & 9 deletions src/samples/WebApiSample/WebApiSample/WebApiSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@
<PropertyGroup>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<None Update="WorkflowDSL/**" LinkBase="WorkflowDSL/">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="WorkflowCore" Version="1.8.0" />
<PackageReference Include="WorkflowCore.Persistence.MongoDB" Version="1.7.0" />
<PackageReference Include="WorkflowCore.Providers.Elasticsearch" Version="1.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
<PackageReference Include="WorkflowCore" Version="3.6.3" />
<PackageReference Include="WorkflowCore.DSL" Version="3.6.3" />
<PackageReference Include="WorkflowCore.Persistence.MongoDB" Version="3.6.3" />
<PackageReference Include="WorkflowCore.Providers.Elasticsearch" Version="3.6.3" />
</ItemGroup>

<ItemGroup>
<None Update="WorkflowDSL\TestDSLWorkflow.yml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson">
<Version>6.0.5</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson">
<Version>3.1.9</Version>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Id: TestDSLHelloWorld
Version: 1
DataType: WebApiSample.DataTypes.MyDataType, WebApiSample
Steps:
- Id: HelloWorldId
Name: HelloWorld
StepType: WebApiSample.Steps.DSLHelloWorldStep, WebApiSample
Inputs:
Home:
City: '"HelloWorld City"'
Street: '"HelloWorld Street"'
"@Number": data.Number
Outputs:
Home: step.Result