private void WireRoutesByVerb(RegistrationContext context, string verb, NancyModule.RouteBuilder nancyRoutes) { var dispatches = context.RouteTable.GetRoutesForVerb(verb).Select( route => new KeyValuePair <Route, Delegate>(route, this.CreateRouteDispatch(context, route))).ToArray(); WireSyncRoutes(nancyRoutes, dispatches); WireAsyncRoutes(nancyRoutes, dispatches); }
public ReturningRouteHandlerBuilder(NancyStackModule module, string route, NancyModule.RouteBuilder actionBuilder) { this.module = module; this.route = route; this.actionBuilder = actionBuilder; }
/// <summary> /// Automatically creates routes based on the defined methods. /// </summary> /// <param name="module">Current NancyModule</param> /// <exception cref="RouteException">If zero or multiple routes are defined for a method</exception> public static void CreateRoutesFromMethods(this NancyModule module) { Type[] validReturnTypes = { typeof(Negotiator), typeof(Response) }; Type type = module.GetType(); // get all public methods MethodInfo[] methods = type.GetMethods(); if (methods.Length == 0) { return; } Type[] interfaces = type.GetInterfaces(); // get all route methods i.e methods returning a view MethodInfo[] routeMethods = methods .Where(m => validReturnTypes.Contains(m.ReturnType) && interfaces.Contains(HasRouteMethodsInterfaceType) && m.DeclaringType != NancyModuleType) .ToArray(); // sanity check routes defined foreach (MethodInfo m in routeMethods) { List <AbstractRouteAttribute> methodAttribs = new List <AbstractRouteAttribute>(); foreach (Type t in RouteTypes) { var attribs = (AbstractRouteAttribute[])m.GetCustomAttributes(t, false); methodAttribs.AddRange(attribs); } if (methodAttribs.Count > 1) { throw new RouteException("Multiple routes defined for: " + m.Name); } } // create routes by inspecting method attributes foreach (MethodInfo m in routeMethods) { NancyModule.RouteBuilder builder = module.Get; AbstractRouteAttribute route = null; // get the route that was defined foreach (Type t in RouteTypes) { var attribs = (AbstractRouteAttribute[])m.GetCustomAttributes(t, false); if (attribs.Length == 0) { continue; } route = attribs[0]; break; } if (route == null) { throw new RouteException("No route defined for: " + m.Name); } switch (route.RequestType) { case EHttpRoute.Post: builder = module.Post; break; case EHttpRoute.Delete: builder = module.Delete; break; } // create Nancy route mapping MethodInfo runMethod = m; builder[route.RoutePath] = p => CreateRoute(p, runMethod, module); } }
private static void WireAsyncRoutes(NancyModule.RouteBuilder nancyRoutes, IEnumerable <KeyValuePair <Route, Delegate> > dispatches) { dispatches.Where(x => x.Value is Func <object, CancellationToken, Task <object> >) .ForEach(dispatch => nancyRoutes[dispatch.Key.Name, dispatch.Key.Path, runAsync: true] = (Func <object, CancellationToken, Task <object> >)dispatch.Value); }
private static void WireSyncRoutes(NancyModule.RouteBuilder nancyRoutes, IEnumerable <KeyValuePair <Route, Delegate> > dispatches) { dispatches.Where(x => x.Value is Func <object, object>) .ForEach(dispatch => nancyRoutes[dispatch.Key.Name, dispatch.Key.Path] = (Func <object, object>)dispatch.Value); }
private static void ApplyNancyHttpServiceRoutes(Func <NancyContext> nancyContext, NancyModule.RouteBuilder nancyRouteBuilder, IEnumerable <NancyHttpServiceRoute> nancyHttpServiceRoutes) { foreach (var route in nancyHttpServiceRoutes) { nancyRouteBuilder[route.Path, true] = (p, t) => route.Action(nancyContext()); } }