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

feat: Add dockerized email inbound webhook consumer example #789

Merged
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
9 changes: 9 additions & 0 deletions examples/inbound-webhook-handler/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# directories
**/bin/
**/obj/
**/out/

# files
Dockerfile*
**/*.trx
**/*.md
45 changes: 45 additions & 0 deletions examples/inbound-webhook-handler/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/inbound/bin/Debug/netcoreapp2.1/inbound.dll",
"args": [],
"cwd": "${workspaceFolder}/src/inbound",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
15 changes: 15 additions & 0 deletions examples/inbound-webhook-handler/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/inbound/inbound.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
21 changes: 21 additions & 0 deletions examples/inbound-webhook-handler/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /App

# copy csproj and restore as distinct layers
COPY *.sln .
COPY Src/Inbound/*.csproj ./Src/Inbound/
COPY Tests/Inbound.Tests/*.csproj ./Tests/Inbound.Tests/
RUN dotnet restore

# copy everything else and build app
COPY Src/Inbound/. ./Src/Inbound/
WORKDIR /App/Src/Inbound
RUN dotnet publish -c Release -o Out

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /App
COPY --from=build /App/Src/Inbound/Out ./

RUN echo "ASPNETCORE_URLS=http://0.0.0.0:\$PORT\nDOTNET_RUNNING_IN_CONTAINER=true" > /App/SetupHerokuEnv.sh && chmod +x /App/SetupHerokuEnv.sh

CMD /bin/bash -c "source /App/SetupHerokuEnv.sh && dotnet Inbound.dll"
93 changes: 93 additions & 0 deletions examples/inbound-webhook-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Overview

SendGrid has an Email Inbound Parse Webhook which posts data from a specified incoming email address to a URL of your choice. This library allows you to quickly and easily deployable solution that help you easily get up and running processing (parse and complete some action) your inbound parse webhooks.

This is docker-based solution which can be deployed on cloud services like Heroku out of the box.

# Table of Content
- [Prerequisite](#prerequisite)
- [Deploy locally](#deploy_locally)
- [Deploy Heroku](#deploy_heroku)
- [Testing the Source Code](#testing_the_source_code)

<a name="prerequisite"></a>
## Prerequisite
Clone the repository
```
git clone https://github.com/sendgrid/sendgrid-csharp.git
```
Move into the clonned repository
```
cd sendgrid-csharp/examples/inbound-webhook-handler
```
Restore the Packages
```
dotnet restore
```

<a name="deploy_locally"></a>
## Deploy locally
Setup your MX records. Depending on your domain name host, you may need to wait up to 48 hours for the settings to propagate.

Run the Inbound Parse listener in your terminal:
```
git clone https://github.com/sendgrid/sendgrid-csharp.git

cd sendgrid-csharp/examples/inbound-webhook-handler

dotnet restore

dotnet run --project .\Src\Inbound\Inbound.csproj
```
Above will start server listening on a random port like below

In another terminal, use ngrok to allow external access to your machine:
```
ngrok http PORT_NUMBER
```
Update your SendGrid Incoming Parse settings: Settings Page | Docs

- For the HOSTNAME field, use the domain that you changed the MX records (e.g. inbound.yourdomain.com)
- For the URL field, use the URL generated by ngrok + /inbound, e.g http://XXXXXXX.ngrok.io/inbound

Next, send an email to [anything]@inbound.yourdomain.com, then look at the terminal where you started the Inbound Parse listener.

<a name="deploy_heroku"></a>
## Deploy to Heroku

[Create](https://signup.heroku.com/) Heruko account if not already present

Install the Heroku CLI

Download and install the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-command-line).

If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.
```
$ heroku login
```

Now you can sign into Container Registry.
```
$ heroku container:login
```

Create app in heroku
```
$ heroku apps:create UNIQUE_APP_NAME
```

Push your Docker-based app
Build the Dockerfile in the current directory and push the Docker image.
```
$ heroku container:push web --app UNIQUE_APP_NAME
```

Deploy the changes
Release the newly pushed images to deploy your app.
```
$ heroku container:release web --app UNIQUE_APP_NAME
```

<a name="testing_the_source_code"></a>
## Testing the Source Code
You can get all the test cases inside the `Tests` folder.
59 changes: 59 additions & 0 deletions examples/inbound-webhook-handler/SendGridInbound.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{5E71A0CA-F2E2-4762-B020-29F1D8682F75}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inbound", "Src\Inbound\Inbound.csproj", "{9449C214-54EF-40A9-AAB3-4FE212BECA23}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1446F41E-1766-4B3E-B7AC-C8766A3E1751}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inbound.Tests", "Tests\Inbound.Tests\Inbound.Tests.csproj", "{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|x64.ActiveCfg = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|x64.Build.0 = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|x86.ActiveCfg = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Debug|x86.Build.0 = Debug|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|Any CPU.Build.0 = Release|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|x64.ActiveCfg = Release|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|x64.Build.0 = Release|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|x86.ActiveCfg = Release|Any CPU
{9449C214-54EF-40A9-AAB3-4FE212BECA23}.Release|x86.Build.0 = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|x64.ActiveCfg = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|x64.Build.0 = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|x86.ActiveCfg = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Debug|x86.Build.0 = Debug|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|Any CPU.Build.0 = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|x64.ActiveCfg = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|x64.Build.0 = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|x86.ActiveCfg = Release|Any CPU
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9449C214-54EF-40A9-AAB3-4FE212BECA23} = {5E71A0CA-F2E2-4762-B020-29F1D8682F75}
{0AF26ED1-3F2D-40F5-9A01-FE5955A8F927} = {1446F41E-1766-4B3E-B7AC-C8766A3E1751}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D6E6C0FF-04E9-4D98-B958-612DDD7FBB0A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Inbound.Parsers;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Inbound.Controllers
{
[Route("/")]
[ApiController]
public class InboundController : Controller
{
[HttpGet]
public IActionResult Index()
{
return View();
}

// Process POST from Inbound Parse and print received data.
[HttpPost]
[Route("inbound")]
public IActionResult InboundParse()
{
InboundWebhookParser _inboundParser = new InboundWebhookParser(Request.Body);

var inboundEmail = _inboundParser.Parse();

return Ok();
}

private void Log(IDictionary<string, string> keyValues)
{
if(keyValues == null)
{
return;
}
Console.WriteLine(JsonConvert.SerializeObject(keyValues));
}
}
}
16 changes: 16 additions & 0 deletions examples/inbound-webhook-handler/Src/Inbound/Inbound.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="HttpMultipartParser" Version="2.2.4" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
Loading