/
WriteEvent.cs
43 lines (39 loc) · 1.33 KB
/
WriteEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using DistributedModels;
namespace DistributedAPI
{
public static class WriteEvent
{
[FunctionName("WriteEvent")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] Event eventData,
[CosmosDB(
databaseName: "eventsDb",
collectionName: "events",
PreferredLocations = "%REGION%",
UseMultipleWriteLocations = true,
ConnectionStringSetting = "CosmosDBAzureFriday"
)] IAsyncCollector<Event> eventCollector,
ILogger log)
{
// Validating
if (eventData == null)
{
return new BadRequestResult();
}
eventData.region = Environment.GetEnvironmentVariable("REGION");
log.LogInformation($"Received event on {eventData.region} for device {eventData.deviceId}");
// Saving
await eventCollector.AddAsync(eventData);
return new OkResult();
}
}
}