using System;
using System.Threading.Tasks;
using EmbedIO.Utilities;
namespace EmbedIO.Actions
{
///
/// A module that passes requests to a callback.
///
///
public class ActionModule : WebModuleBase
{
private readonly HttpVerbs _verb;
private readonly RequestHandlerCallback _handler;
///
/// Initializes a new instance of the class.
///
/// The base route.
/// The HTTP verb that will be served by this module.
/// The callback used to handle requests.
/// is .
///
public ActionModule(string baseRoute, HttpVerbs verb, RequestHandlerCallback handler)
: base(baseRoute)
{
_verb = verb;
_handler = Validate.NotNull(nameof(handler), handler);
}
///
/// Initializes a new instance of the class.
///
/// The handler.
public ActionModule(RequestHandlerCallback handler)
: this("/", HttpVerbs.Any, handler)
{
}
///
public override bool IsFinalHandler => false;
///
protected override async Task OnRequestAsync(IHttpContext context)
{
if (_verb != HttpVerbs.Any && context.Request.HttpVerb != _verb)
return;
await _handler(context).ConfigureAwait(false);
context.SetHandled();
}
}
}