Added new route exception event handler. Renamed events. Added better example code. Bumped nuget version to 1.1.2.
This commit is contained in:
parent
77f13ac7ae
commit
b4414f4da2
@ -34,29 +34,44 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
new Route(HttpRequestMethod.Get, "/json", Json)
|
new Route(HttpRequestMethod.Get, "/json", Json)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
listener.RequestPreProcessEvent += (ListenerContext context) => {
|
||||||
|
Console.WriteLine("Request start: " + context.Request.RawUrl);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
listener.RequestPostProcessEvent += (ListenerContext context) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine("Request end: " + context.Request.RawUrl);
|
||||||
|
};
|
||||||
|
|
||||||
listener.Start();
|
listener.Start();
|
||||||
|
|
||||||
|
Console.WriteLine("Available routes:");
|
||||||
|
|
||||||
|
foreach (var route in listener.Routes)
|
||||||
|
Console.WriteLine($"- [{route.Method}] {route.Syntax}");
|
||||||
|
|
||||||
Console.WriteLine($"Rest api server running at http://localhost:{listener.Port}");
|
Console.WriteLine($"Rest api server running at http://localhost:{listener.Port}");
|
||||||
|
|
||||||
listener.Block();
|
listener.Block();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpListenerResponse Status(RouteContext context)
|
public static HttpListenerResponse Status(ListenerContext context)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpListenerResponse Add(RouteContext context, double a, double b)
|
public static HttpListenerResponse Add(ListenerContext context, double a, double b)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpListenerResponse Signup(RouteContext context, User user)
|
public static HttpListenerResponse Signup(ListenerContext context, User user)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
|
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpListenerResponse Json(RouteContext context)
|
public static HttpListenerResponse Json(ListenerContext context)
|
||||||
{
|
{
|
||||||
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,11 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MontoyaTech.Rest.Net
|
namespace MontoyaTech.Rest.Net.Events
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A route post process delegate that can be used to process the response from routes.
|
/// A delegate to post process requests.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
public delegate void RoutePostprocess(RouteContext context);
|
public delegate void RequestPostProcessEventHandler(ListenerContext context);
|
||||||
}
|
}
|
15
Rest.Net/Events/RequestPreProcessEventHandler.cs
Normal file
15
Rest.Net/Events/RequestPreProcessEventHandler.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A delegate that can be used to pre process requests and stop them from being handled by routes if needed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public delegate bool RequestPreProcessEventHandler(ListenerContext context);
|
||||||
|
}
|
16
Rest.Net/Events/RouteExceptionEventHandler.cs
Normal file
16
Rest.Net/Events/RouteExceptionEventHandler.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A delegate that can be used to handle route exceptions.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="route"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="ex"></param>
|
||||||
|
public delegate void RouteExceptionEventHandler(Route route, ListenerContext context, Exception ex);
|
||||||
|
}
|
@ -8,10 +8,10 @@ using System.Net;
|
|||||||
namespace MontoyaTech.Rest.Net
|
namespace MontoyaTech.Rest.Net
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An outline of a Route Context which includes
|
/// An outline of a Listener Context which includes
|
||||||
/// the request and response for a route.
|
/// the given request and a resposne.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class RouteContext
|
public class ListenerContext
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Http Request that requested this route.
|
/// The Http Request that requested this route.
|
||||||
@ -24,11 +24,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
public HttpListenerResponse Response = null;
|
public HttpListenerResponse Response = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new RouteContext with a given request and response.
|
/// Creates a new ListenerContext with a given request and response.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="response"></param>
|
/// <param name="response"></param>
|
||||||
public RouteContext(HttpListenerRequest request, HttpListenerResponse response)
|
public ListenerContext(HttpListenerRequest request, HttpListenerResponse response)
|
||||||
{
|
{
|
||||||
this.Request = request;
|
this.Request = request;
|
||||||
this.Response = response;
|
this.Response = response;
|
@ -8,7 +8,7 @@
|
|||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||||
<Authors>MontoyaTech</Authors>
|
<Authors>MontoyaTech</Authors>
|
||||||
<Company>MontoyaTech</Company>
|
<Company>MontoyaTech</Company>
|
||||||
<Copyright>MontoyaTech 2022</Copyright>
|
<Copyright>MontoyaTech 2023</Copyright>
|
||||||
<PackageProjectUrl>https://code.montoyatech.com/MontoyaTech/Rest.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://code.montoyatech.com/MontoyaTech/Rest.Net</PackageProjectUrl>
|
||||||
<Description>A simple C# library for creating a rest api.</Description>
|
<Description>A simple C# library for creating a rest api.</Description>
|
||||||
<PackageTags>MontoyaTech;Rest.Net</PackageTags>
|
<PackageTags>MontoyaTech;Rest.Net</PackageTags>
|
||||||
@ -17,7 +17,7 @@
|
|||||||
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
|
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
|
||||||
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
|
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
|
||||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||||
<Version>1.1.1</Version>
|
<Version>1.1.2</Version>
|
||||||
<PackageReleaseNotes>HttpListener now returns NotFound if no route was found. Added WithRedirect and WithHtml extensions.</PackageReleaseNotes>
|
<PackageReleaseNotes>HttpListener now returns NotFound if no route was found. Added WithRedirect and WithHtml extensions.</PackageReleaseNotes>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The target function to invoke if this route is invoked.
|
/// The target function to invoke if this route is invoked.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Func<RouteContext, HttpListenerResponse> Target;
|
private Func<ListenerContext, HttpListenerResponse> Target;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether or not to close the response after the route is invoked.
|
/// Whether or not to close the response after the route is invoked.
|
||||||
@ -42,7 +42,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="syntax"></param>
|
/// <param name="syntax"></param>
|
||||||
/// <param name="target"></param>
|
/// <param name="target"></param>
|
||||||
/// <param name="closeResponse"></param>
|
/// <param name="closeResponse"></param>
|
||||||
public Route(string method, string syntax, Func<RouteContext, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -58,7 +58,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="syntax"></param>
|
/// <param name="syntax"></param>
|
||||||
/// <param name="target"></param>
|
/// <param name="target"></param>
|
||||||
/// <param name="closeResponse"></param>
|
/// <param name="closeResponse"></param>
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -110,7 +110,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
/// <param name="arguments"></param>
|
/// <param name="arguments"></param>
|
||||||
public virtual void Invoke(RouteContext context, params string[] arguments)
|
public virtual void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.Invoke(context);
|
this.Target.Invoke(context);
|
||||||
}
|
}
|
||||||
@ -118,9 +118,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1> : Route
|
public class Route<T1> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -128,10 +128,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert<T1>(arguments[0]));
|
this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert<T1>(arguments[0]));
|
||||||
}
|
}
|
||||||
@ -139,9 +139,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2> : Route
|
public class Route<T1, T2> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -149,10 +149,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -164,9 +164,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3> : Route
|
public class Route<T1, T2, T3> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -174,10 +174,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -190,9 +190,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3, T4> : Route
|
public class Route<T1, T2, T3, T4> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, T4, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -200,10 +200,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, T4, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -217,9 +217,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5> : Route
|
public class Route<T1, T2, T3, T4, T5> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, T4, T5, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -227,10 +227,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -245,9 +245,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6> : Route
|
public class Route<T1, T2, T3, T4, T5, T6> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -255,10 +255,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -274,9 +274,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6, T7> : Route
|
public class Route<T1, T2, T3, T4, T5, T6, T7> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -284,10 +284,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
@ -304,9 +304,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
|
|
||||||
public class Route<T1, T2, T3, T4, T5, T6, T7, T8> : Route
|
public class Route<T1, T2, T3, T4, T5, T6, T7, T8> : Route
|
||||||
{
|
{
|
||||||
private Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> Target;
|
private Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> Target;
|
||||||
|
|
||||||
public Route(string method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(string method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
{
|
{
|
||||||
this.Method = method;
|
this.Method = method;
|
||||||
this.Syntax = syntax;
|
this.Syntax = syntax;
|
||||||
@ -314,10 +314,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
this.CloseResponse = closeResponse;
|
this.CloseResponse = closeResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route(HttpRequestMethod method, string syntax, Func<RouteContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
public Route(HttpRequestMethod method, string syntax, Func<ListenerContext, T1, T2, T3, T4, T5, T6, T7, T8, HttpListenerResponse> target, bool closeResponse = true)
|
||||||
: this(method.ToString(), syntax, target, closeResponse) { }
|
: this(method.ToString(), syntax, target, closeResponse) { }
|
||||||
|
|
||||||
public override void Invoke(RouteContext context, params string[] arguments)
|
public override void Invoke(ListenerContext context, params string[] arguments)
|
||||||
{
|
{
|
||||||
this.Target.DynamicInvoke(
|
this.Target.DynamicInvoke(
|
||||||
context,
|
context,
|
||||||
|
@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using MontoyaTech.Rest.Net.Events;
|
||||||
|
|
||||||
namespace MontoyaTech.Rest.Net
|
namespace MontoyaTech.Rest.Net
|
||||||
{
|
{
|
||||||
@ -32,12 +33,17 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// An event to preprocess routes before the route is executed.
|
/// An event to preprocess routes before the route is executed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event RoutePreprocess PreprocessEvent;
|
public event RequestPreProcessEventHandler RequestPreProcessEvent;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An event to postprocess routes before the route response is returned.
|
/// An event to postprocess routes before the route response is returned.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event RoutePostprocess PostprocessEvent;
|
public event RequestPostProcessEventHandler RequestPostProcessEvent;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An event that is invoked when a route has an unhandled exception.
|
||||||
|
/// </summary>
|
||||||
|
public event RouteExceptionEventHandler RouteExceptionEvent;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new RouteListener with the default port number.
|
/// Creates a new RouteListener with the default port number.
|
||||||
@ -116,12 +122,12 @@ namespace MontoyaTech.Rest.Net
|
|||||||
bool close = true;
|
bool close = true;
|
||||||
string[] arguments = null;
|
string[] arguments = null;
|
||||||
|
|
||||||
var context = new RouteContext(ctx.Request, ctx.Response);
|
var context = new ListenerContext(ctx.Request, ctx.Response);
|
||||||
|
|
||||||
//Preprocess the route context, if it returns false, then we have to not invoke the route.
|
//Preprocess the route context, if it returns false, then we have to not invoke the route.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (this.PreprocessEvent != null && !this.PreprocessEvent.Invoke(context))
|
if (this.RequestPreProcessEvent != null && !this.RequestPreProcessEvent.Invoke(context))
|
||||||
handled = true;
|
handled = true;
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
@ -138,8 +144,11 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
this.Routes[i].Invoke(context, arguments);
|
this.Routes[i].Invoke(context, arguments);
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
if (this.RouteExceptionEvent != null)
|
||||||
|
this.RouteExceptionEvent.Invoke(this.Routes[i], context, ex);
|
||||||
|
|
||||||
ctx.Response.WithStatus(HttpStatusCode.InternalServerError);
|
ctx.Response.WithStatus(HttpStatusCode.InternalServerError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,8 +159,8 @@ namespace MontoyaTech.Rest.Net
|
|||||||
//Post process the route context.
|
//Post process the route context.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (this.PostprocessEvent != null)
|
if (this.RequestPostProcessEvent != null)
|
||||||
this.PostprocessEvent.Invoke(context);
|
this.RequestPostProcessEvent.Invoke(context);
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MontoyaTech.Rest.Net
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A route preprocess delegate that can be used to process requests before they reach a route
|
|
||||||
/// and reject them if needed.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public delegate bool RoutePreprocess(RouteContext context);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user