Created
May 28, 2020 20:26
-
-
Save vanbukin/8556ece3d37a1618a6ea061c3d0e02c6 to your computer and use it in GitHub Desktop.
EventsGateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Dodo.EventStream | |
open System | |
open System.IO | |
open System.Threading.Tasks | |
open Microsoft.Azure.WebJobs | |
open Microsoft.Azure.WebJobs.Extensions.Http | |
open Microsoft.AspNetCore.Http | |
open Microsoft.Extensions.Logging | |
open System.Text.Json | |
open System.Text | |
module EventsGateway = | |
[<FunctionName("events")>] | |
let run ([<HttpTrigger(AuthorizationLevel.Function, "post", Route = null)>] req: HttpRequest, log: ILogger): | |
[<EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")>] Task<string> = | |
async { | |
let! document = JsonDocument.ParseAsync req.Body |> Async.AwaitTask | |
if log.IsEnabled LogLevel.Trace then log.LogTrace("Request body: {0}", document) | |
let output = new MemoryStream() | |
let writer = new Utf8JsonWriter(output) | |
writer.WriteStartObject() | |
writer.WriteString("Timestamp", DateTime.UtcNow.ToString("O")) | |
writer.WriteStartObject("Event") | |
for property in document.RootElement.EnumerateObject() do | |
property.WriteTo(writer) | |
writer.WriteEndObject() | |
writer.WriteEndObject() | |
writer.Flush() | |
if log.IsEnabled LogLevel.Trace then | |
log.LogTrace("Response body: {0}", Encoding.UTF8.GetString(output.ToArray())) | |
return Encoding.UTF8.GetString(output.ToArray()) | |
} | |
|> Async.StartAsTask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment