Skip to content

Instantly share code, notes, and snippets.

@dbarkol
Created May 17, 2019 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarkol/38156f4ee6c3f3ee71a1723478ac3ca4 to your computer and use it in GitHub Desktop.
Save dbarkol/38156f4ee6c3f3ee71a1723478ac3ca4 to your computer and use it in GitHub Desktop.
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 Confluent.Kafka;
namespace Zohan.KafkaDemo
{
public class SendEvent
{
private readonly IKafkaProducer _producer;
public SendEvent(IKafkaProducer producer)
{
// Save an instance of the Kafka producer
_producer = producer;
}
[FunctionName("sendevent")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Send event function triggered");
// Retrieve the topic name
var topicName = Environment.GetEnvironmentVariable("EventHubName");
// Read the message body from the incoming request
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// Send the event to the topic
await _producer.SendEvent(topicName, // topic name
null, // key
requestBody); // value
return (ActionResult)new OkObjectResult($"Ok");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment