Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Created October 15, 2018 02:47
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 bcnzer/c3d45a7bb55255ceb153a607b0bf702d to your computer and use it in GitHub Desktop.
Save bcnzer/c3d45a7bb55255ceb153a607b0bf702d to your computer and use it in GitHub Desktop.
Cut down version of a Azure Functions HTTP trigger
namespace EventMgr.Functions.Events
{
[DependencyInjectionConfig(typeof(DIConfig))]
public static class Attendee
{
[FunctionName("Attendee")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", Route = "attendee")]
HttpRequest request, ILogger log, ExecutionContext context,
[Inject] IAttendeeRepository attendeeRepository,
[Inject] IExceptionReporting exceptionReporting,
[Inject] IManagementApiClient mgmtApiClient)
{
try
{
log.LogInformation("{FunctionName} starting", context.FunctionName);
// Azure Functions v2, as of tis writing, doesn't have proper dependency
// injection. I'm using a library that allows me to use Autofac but the
// request, log and context don't get injected :( Hence this new instance below
// Also mgmtApiClient is from the Auth0 SDK. Used to access authentication data
var headerValidation = new Auth0Helper(request, log, exceptionReporting, mgmtApiClient);
var errorResult = await headerValidation.ValidateHeaderAndAuthLevel(RoleTypes.Admin, request.Method);
if (errorResult != null)
{
log.LogWarning("{FunctionName} returning an error", context.FunctionName);
return errorResult;
}
// Do stuff here...
}
catch (Exception ex)
{
log.LogError("{FunctionName} exception thrown", ex, context.FunctionName);
await exceptionReporting.LogException(ex);
return new InternalServerErrorResult();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment