/// <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(INancyModule module, string path) { var assembly = module.GetType().Assembly; if (assembly.IsDynamic) { return string.Empty; } 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; }
/// <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(INancyModule module, string path) { var assembly = module.GetType().GetTypeInfo().Assembly; if (assembly.IsDynamic) { return(string.Empty); } 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 <SwaggerRouteData> ToSwaggerRouteData(INancyModule module) { Func <IEnumerable <RouteAttribute>, RouteId> getRouteId = (attrs) => { return(attrs.Select(attr => RouteId.Create(module, attr)) .FirstOrDefault(routeId => routeId.IsValid)); }; // Discover route handlers and put them in a Dictionary<RouteId, MethodInfo> var routeHandlers = module.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) .Select(methodInfo => new { RouteId = getRouteId(methodInfo.GetCustomAttributes <RouteAttribute>()), MethodInfo = methodInfo }) .Where(x => x.RouteId.IsValid) .ToDictionary( x => x.RouteId, x => x.MethodInfo ); return(module.Routes .Select(route => CreateSwaggerRouteData(module, route, routeHandlers))); }
public static void MetricForResponseSize(this INancyModule module, string metricName, Predicate <RouteDescription> routePredicate) { var name = string.Format("{0}.{1}", module.GetType().Name, metricName); CheckNancyMetricsIsConfigured(); var histogram = NancyMetrics.CurrentConfig.Registry.Histogram(name, Unit.Custom("bytes"), SamplingType.FavourRecent); module.After.AddItemToEndOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { string lengthHeader; // if available use content length header if (ctx.Response.Headers.TryGetValue("Content-Length", out lengthHeader)) { long length; if (long.TryParse(lengthHeader, out length)) { histogram.Update(length); } } else { // if no content length - get the length of the stream // this might be suboptimal for some types of requests using (var ns = new NullStream()) { ctx.Response.Contents(ns); histogram.Update(ns.Length); } } } }); }
public static void MetricForRequestTime(this INancyModule module, string metricName, Predicate <RouteDescription> routePredicate) { var name = string.Format("{0}.{1}", module.GetType().Name, metricName); CheckNancyMetricsIsConfigured(); var timer = NancyMetrics.CurrentConfig.Registry.Timer(name, Unit.Requests, SamplingType.FavourRecent, TimeUnit.Seconds, TimeUnit.Milliseconds); var key = "Metrics.Nancy.Request.Timer." + metricName; module.Before.AddItemToStartOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { ctx.Items[key] = timer.NewContext(); } return(null); }); module.After.AddItemToEndOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { using (ctx.Items[key] as IDisposable) { } ctx.Items.Remove(key); } }); }
/// <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 INancyModule module) { var typeName = module.GetType().Name; var nameMatch = ModuleNameExpression.Match(typeName); if (nameMatch.Success) { return(nameMatch.Groups["name"].Value); } return(typeName); }
/// <summary> /// Extracts the friendly name of a Nancy module given its type. /// </summary> /// <param name="module">The module instance</param> /// <returns>A string containing the name of the parameter.</returns> public static string GetModuleName(this INancyModule module) { var typeName = module.GetType().Name; var offset = typeName.LastIndexOf("Module", StringComparison.Ordinal); if (offset <= 0) { return(typeName); } return(typeName.Substring(0, offset)); }
public static void MetricForRequestSize(this INancyModule module, string metricName, Predicate <RouteDescription> routePredicate) { var name = string.Format("{0}.{1}", module.GetType().Name, metricName); CheckNancyMetricsIsConfigured(); var histogram = NancyMetrics.CurrentConfig.Registry.Histogram(name, Unit.Custom("bytes"), SamplingType.FavourRecent); module.Before.AddItemToStartOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { histogram.Update(ctx.Request.Headers.ContentLength); } return(null); }); }
private static Type GetModuleMetadataType(INancyModule module) { var metadataName = module.GetType().Name.Replace("Module", "Metadata"); Type type; if (ModuleTypes.TryGetValue(metadataName, out type)) { return(type); } type = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) .FirstOrDefault(x => x.Name == metadataName); ModuleTypes.Add(metadataName, type); return(typeof(ModuleMetadata).IsAssignableFrom(type) ? type : null); }
public object GetMetadata(INancyModule module, RouteDescription routeDescription) { var entryAssembly = Assembly.GetEntryAssembly(); if (module.GetType().Assembly != entryAssembly) { return(null); } DocumentationObject val = null; var eps = BuildDocumentation(Types); string path; var basePath = "/"; if (module.ModulePath.Length > 1) { basePath = module.ModulePath; path = routeDescription.Path.Replace(module.ModulePath, ""); if (string.IsNullOrEmpty(path)) { path = "/"; } } else { path = routeDescription.Path; } val = eps.FirstOrDefault(p => string.Equals(routeDescription.Method, p.HttpMethod.ToString(), StringComparison.CurrentCultureIgnoreCase) && p.BasePath == basePath && path == p.Path); if (val == null) { //TODO:(drose) throw exception to enfore usage? Console.ForegroundColor = ConsoleColor.Red; Console.Out.WriteLine($"No Documentation Found For {module.GetModuleName()}Module : {routeDescription.Method} {path}"); Console.ForegroundColor = ConsoleColor.White; return(null); } val.BasePath = basePath; val.ModuleName = module.GetModuleName().ToLower(); return(val); }
public INancyModule GetModule(Type moduleType, NancyContext context) { return(moduleType == _interactiveModule.GetType() ? _interactiveModule : null); }
private IEnumerable<SwaggerRouteData> ToSwaggerRouteData(INancyModule module) { Func<IEnumerable<RouteAttribute>, RouteId> getRouteId = (attrs) => { return attrs.Select(attr => RouteId.Create(module, attr)) .FirstOrDefault(routeId => routeId.IsValid); }; // Discover route handlers and put them in a Dictionary<RouteId, MethodInfo> var routeHandlers = module.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) .Select(methodInfo => new { RouteId = getRouteId(methodInfo.GetCustomAttributes<RouteAttribute>()), MethodInfo = methodInfo }) .Where(x => x.RouteId.IsValid) .ToDictionary( x => x.RouteId, x => x.MethodInfo ); return module.Routes .Select(route => CreateSwaggerRouteData(module, route, routeHandlers)); }