Skip to content

Commit fe98040

Browse files
committedJan 23, 2019
web sample
1 parent e56e805 commit fe98040

17 files changed

+449
-0
lines changed
 

‎.dockerignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.dockerignore
2+
.env
3+
.git
4+
.gitignore
5+
.vs
6+
.vscode
7+
docker-compose.yml
8+
docker-compose.*.yml
9+
*/bin
10+
*/obj
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.dockerignore
2+
.env
3+
.git
4+
.gitignore
5+
.vs
6+
.vscode
7+
docker-compose.yml
8+
docker-compose.*.yml
9+
*/bin
10+
*/obj
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiSample", "WebApiSample\WebApiSample.csproj", "{14489389-A65D-4993-8DE2-F51701A5AF60}"
7+
EndProject
8+
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{14489389-A65D-4993-8DE2-F51701A5AF60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{14489389-A65D-4993-8DE2-F51701A5AF60}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{14489389-A65D-4993-8DE2-F51701A5AF60}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{14489389-A65D-4993-8DE2-F51701A5AF60}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {EEC447B3-434E-4573-A280-D4A6CC652274}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using Newtonsoft.Json.Linq;
8+
using WorkflowCore.Interface;
9+
using WorkflowCore.Models;
10+
11+
namespace WebApiSample.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class EventsController : Controller
16+
{
17+
private readonly IWorkflowController _workflowService;
18+
19+
public EventsController(IWorkflowController workflowService)
20+
{
21+
_workflowService = workflowService;
22+
}
23+
24+
[HttpPost("{eventName}/{eventKey}")]
25+
public async Task<IActionResult> Post(string eventName, string eventKey, [FromBody]object eventData)
26+
{
27+
await _workflowService.PublishEvent(eventName, eventKey, eventData);
28+
return Ok();
29+
}
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
using WorkflowCore.Interface;
10+
using WorkflowCore.Models;
11+
using WorkflowCore.Models.Search;
12+
13+
namespace WebApiSample.Controllers
14+
{
15+
[Route("api/[controller]")]
16+
[ApiController]
17+
public class WorkflowsController : Controller
18+
{
19+
private readonly IWorkflowController _workflowService;
20+
private readonly IWorkflowRegistry _registry;
21+
private readonly IPersistenceProvider _workflowStore;
22+
private readonly ISearchIndex _searchService;
23+
24+
public WorkflowsController(IWorkflowController workflowService, ISearchIndex searchService, IWorkflowRegistry registry, IPersistenceProvider workflowStore)
25+
{
26+
_workflowService = workflowService;
27+
_workflowStore = workflowStore;
28+
_registry = registry;
29+
_searchService = searchService;
30+
}
31+
32+
33+
[HttpGet]
34+
public async Task<IActionResult> Get(string terms, WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, int take = 10)
35+
{
36+
var filters = new List<SearchFilter>();
37+
38+
if (status.HasValue)
39+
filters.Add(StatusFilter.Equals(status.Value));
40+
41+
if (createdFrom.HasValue)
42+
filters.Add(DateRangeFilter.After(x => x.CreateTime, createdFrom.Value));
43+
44+
if (createdTo.HasValue)
45+
filters.Add(DateRangeFilter.Before(x => x.CreateTime, createdTo.Value));
46+
47+
if (!string.IsNullOrEmpty(type))
48+
filters.Add(ScalarFilter.Equals(x => x.WorkflowDefinitionId, type));
49+
50+
var result = await _searchService.Search(terms, skip, take, filters.ToArray());
51+
52+
return Json(result);
53+
}
54+
55+
[HttpGet("{id}")]
56+
public async Task<IActionResult> Get(string id)
57+
{
58+
var result = await _workflowStore.GetWorkflowInstance(id);
59+
return Json(result);
60+
}
61+
62+
[HttpPost("{id}")]
63+
[HttpPost("{id}/{version}")]
64+
public async Task<IActionResult> Post(string id, int? version, string reference, [FromBody]JObject data)
65+
{
66+
string workflowId = null;
67+
var def = _registry.GetDefinition(id, version);
68+
if (def == null)
69+
return BadRequest(String.Format("Workflow defintion {0} for version {1} not found", id, version));
70+
71+
if ((data != null) && (def.DataType != null))
72+
{
73+
var dataStr = JsonConvert.SerializeObject(data);
74+
var dataObj = JsonConvert.DeserializeObject(dataStr, def.DataType);
75+
workflowId = await _workflowService.StartWorkflow(id, version, dataObj, reference);
76+
}
77+
else
78+
{
79+
workflowId = await _workflowService.StartWorkflow(id, version, null, reference);
80+
}
81+
82+
return Ok(workflowId);
83+
}
84+
85+
[HttpPut("{id}/suspend")]
86+
public Task<bool> Suspend(string id)
87+
{
88+
return _workflowService.SuspendWorkflow(id);
89+
}
90+
91+
[HttpPut("{id}/resume")]
92+
public Task<bool> Resume(string id)
93+
{
94+
return _workflowService.ResumeWorkflow(id);
95+
}
96+
97+
[HttpDelete("{id}")]
98+
public Task<bool> Terminate(string id)
99+
{
100+
return _workflowService.TerminateWorkflow(id);
101+
}
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
2+
WORKDIR /app
3+
EXPOSE 80
4+
5+
FROM microsoft/dotnet:2.1-sdk AS build
6+
WORKDIR /src
7+
COPY WebApiSample/WebApiSample.csproj WebApiSample/
8+
RUN dotnet restore WebApiSample/WebApiSample.csproj
9+
COPY . .
10+
WORKDIR /src/WebApiSample
11+
RUN dotnet build WebApiSample.csproj -c Release -o /app
12+
13+
FROM build AS publish
14+
RUN dotnet publish WebApiSample.csproj -c Release -o /app
15+
16+
FROM base AS final
17+
WORKDIR /app
18+
COPY --from=publish /app .
19+
ENTRYPOINT ["dotnet", "WebApiSample.dll"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
2+
WORKDIR /app
3+
EXPOSE 80
4+
5+
FROM microsoft/dotnet:2.1-sdk AS build
6+
WORKDIR /src
7+
COPY ["WebApiSample/WebApiSample.csproj", "WebApiSample/"]
8+
RUN dotnet restore "WebApiSample/WebApiSample.csproj"
9+
COPY . .
10+
WORKDIR "/src/WebApiSample"
11+
RUN dotnet build "WebApiSample.csproj" -c Release -o /app
12+
13+
FROM build AS publish
14+
RUN dotnet publish "WebApiSample.csproj" -c Release -o /app
15+
16+
FROM base AS final
17+
WORKDIR /app
18+
COPY --from=publish /app .
19+
ENTRYPOINT ["dotnet", "WebApiSample.dll"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace WebApiSample
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
20+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:52884",
7+
"sslPort": 0
8+
}
9+
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "api/values",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"WebApiSample": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"launchUrl": "api/values",
24+
"environmentVariables": {
25+
"ASPNETCORE_ENVIRONMENT": "Development"
26+
},
27+
"applicationUrl": "http://localhost:5000"
28+
},
29+
"Docker": {
30+
"commandName": "Docker",
31+
"launchBrowser": true,
32+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values"
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
12+
using Nest;
13+
using WebApiSample.Workflows;
14+
using WorkflowCore.Interface;
15+
16+
namespace WebApiSample
17+
{
18+
public class Startup
19+
{
20+
public Startup(IConfiguration configuration)
21+
{
22+
Configuration = configuration;
23+
}
24+
25+
public IConfiguration Configuration { get; }
26+
27+
public void ConfigureServices(IServiceCollection services)
28+
{
29+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
30+
services.AddWorkflow(cfg =>
31+
{
32+
cfg.UseMongoDB(@"mongodb://mongo:27017", "workflow");
33+
cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows");
34+
});
35+
}
36+
37+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
38+
{
39+
if (env.IsDevelopment())
40+
{
41+
app.UseDeveloperExceptionPage();
42+
}
43+
44+
app.UseMvc();
45+
46+
var host = app.ApplicationServices.GetService<IWorkflowHost>();
47+
host.RegisterWorkflow<TestWorkflow, MyDataClass>();
48+
host.Start();
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
6+
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Folder Include="wwwroot\" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
16+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
17+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
18+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
19+
<PackageReference Include="WorkflowCore" Version="1.8.0" />
20+
<PackageReference Include="WorkflowCore.Persistence.MongoDB" Version="1.7.0" />
21+
<PackageReference Include="WorkflowCore.Providers.Elasticsearch" Version="1.8.0" />
22+
</ItemGroup>
23+
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using WorkflowCore.Interface;
6+
using WorkflowCore.Models;
7+
8+
namespace WebApiSample.Workflows
9+
{
10+
public class TestWorkflow : IWorkflow<MyDataClass>
11+
{
12+
public string Id => "TestWorkflow";
13+
14+
public int Version => 1;
15+
16+
public void Build(IWorkflowBuilder<MyDataClass> builder)
17+
{
18+
builder
19+
.StartWith(context => ExecutionResult.Next())
20+
.WaitFor("MyEvent", (data, context) => context.Workflow.Id, data => DateTime.Now)
21+
.Output(data => data.Value1, step => step.EventData)
22+
.Then(context => Console.WriteLine("workflow complete"));
23+
}
24+
}
25+
26+
public class MyDataClass
27+
{
28+
public string Value1 { get; set; }
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning"
5+
}
6+
},
7+
"AllowedHosts": "*"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
3+
<PropertyGroup Label="Globals">
4+
<ProjectVersion>2.1</ProjectVersion>
5+
<DockerTargetOS>Linux</DockerTargetOS>
6+
<ProjectGuid>2d5d708d-7ea1-48a9-abf0-64ccc6026435</ProjectGuid>
7+
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
8+
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/api/workflows</DockerServiceUrl>
9+
<DockerServiceName>webapisample</DockerServiceName>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<None Include="docker-compose.override.yml">
13+
<DependentUpon>docker-compose.yml</DependentUpon>
14+
</None>
15+
<None Include="docker-compose.yml" />
16+
<None Include=".dockerignore" />
17+
</ItemGroup>
18+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3.4'
2+
3+
services:
4+
webapisample:
5+
environment:
6+
- ASPNETCORE_ENVIRONMENT=Development
7+
ports:
8+
- "80"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.4'
2+
3+
services:
4+
webapisample:
5+
image: ${DOCKER_REGISTRY-}webapisample
6+
build:
7+
context: .
8+
dockerfile: WebApiSample/Dockerfile
9+
10+
elastic:
11+
image: elasticsearch:6.5.4
12+
expose:
13+
- 9200
14+
15+
mongo:
16+
image: mongo:3.6
17+
expose:
18+
- 27017

0 commit comments

Comments
 (0)
Please sign in to comment.