private ConfigurableBootstrapper SetupBoot(NancyModule module)
 {
     return(new ConfigurableBootstrapper(with =>
     {
         with.DisableAutoRegistration();
         with.Module(module, module.GetType().FullName);
     }));
 }
Beispiel #2
0
        /// <summary>
        /// Get the description for a route.
        /// </summary>
        /// <param name="module">The module that the route is defined in.</param>
        /// <param name="path">The path of the route that the description should be retrieved for.</param>
        /// <returns>A <see cref="string"/> containing the description of the route if it could be found, otherwise <see cref="string.Empty"/>.</returns>
        public string GetDescription(NancyModule module, string path)
        {
            var assembly =
                module.GetType().Assembly;

            var moduleName =
                string.Concat(module.GetType().FullName, ".resources");

            var resourceName = assembly
                               .GetManifestResourceNames()
                               .FirstOrDefault(x => x.Equals(moduleName, StringComparison.OrdinalIgnoreCase));

            if (resourceName != null)
            {
                var manager =
                    new ResourceManager(resourceName.Replace(".resources", string.Empty), assembly);

                return(manager.GetString(path));
            }

            return(string.Empty);
        }
Beispiel #3
0
        /// <summary>
        /// Extracts the friendly name of a Nancy module given its type.
        /// </summary>
        /// <param name="name">The type name taken from GetType().Name.</param>
        /// <returns>A string containing the name of the parameter.</returns>
        /// <exception cref="FormatException"></exception>
        public static string GetModuleName(this NancyModule module)
        {
            var typeName  = module.GetType().Name;
            var nameMatch =
                ModuleNameExpression.Match(typeName);

            if (nameMatch.Success)
            {
                return(nameMatch.Groups["name"].Value);
            }

            return(typeName);
        }
        /// <summary>
        /// Get the description for a route.
        /// </summary>
        /// <param name="module">The module that the route is defined in.</param>
        /// <param name="path">The path of the route that the description should be retrieved for.</param>
        /// <returns>A <see cref="string"/> containing the description of the route if it could be found, otherwise <see cref="string.Empty"/>.</returns>
        public string GetDescription(NancyModule module, string path)
        {
            var assembly =
                module.GetType().Assembly;

            var moduleName =
                string.Concat(module.GetType().FullName, ".resources");

            var resourceName = assembly
                .GetManifestResourceNames()
                .FirstOrDefault(x => x.Equals(moduleName, StringComparison.OrdinalIgnoreCase));

            if (resourceName != null)
            {
                var manager =
                    new ResourceManager(resourceName.Replace(".resources", string.Empty), assembly);

                return manager.GetString(path);
            }

            return string.Empty;
        }
        private IEnumerable <DocumentedMethod> CreateDocumentedMethods(NancyModule module)
        {
            foreach (var method in module.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var swagger = method.GetAttribute <OperationAttribute>();

                if (swagger != null)
                {
                    yield return(new DocumentedMethod
                    {
                        OperationAttribute = swagger,
                        Method = method
                    });
                }
            }
        }
 private static void AddModuleMetaToApplication(INancyApplication application, NancyModule mod, string action)
 {
     IList<ModuleMeta> postList = GetModuleMetaList(application, action);
     postList.Add(new ModuleMeta(mod.GetType(), mod.GetRouteDescription(action)));
 }
Beispiel #7
0
        /// <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);
            }
        }