Exemplo n.º 1
0
        public static ApiRouter AddWithPath(this ApiRouter router, string path, Action <ApiRouter> configure)
        {
            if (path.StartsWith("/"))
            {
                path = path.Remove(0, 1);
            }
            var segments      = path.Split('/');
            var currentRouter = router;

            foreach (var segment in segments)
            {
                if (currentRouter.ChildRouters.ContainsKey(segment))
                {
                    currentRouter = currentRouter.ChildRouters[segment];
                }
                else
                {
                    currentRouter.Add(segment, (r) => {
                        currentRouter = r;
                    });
                }
            }
            configure(currentRouter);
            return(router);
        }
Exemplo n.º 2
0
        public static ApiRouter Search(Type rootType, Uri baseUri)
        {
            var typename = GetSegmentFromControllerType(rootType);
            var router   = new ApiRouter("", baseUri).To(rootType);

            // Find controllers in the namespaces below type
            var controllers = rootType.Assembly.GetTypes()
                              .Where(t => typeof(IHttpController).IsAssignableFrom(t) && t.Namespace.StartsWith(rootType.Namespace) && t != rootType)
                              .Select(t => new ControllerEntry()
            {
                ControllerType = t, Path = t.Namespace.Replace(rootType.Namespace, "").Replace('.', '/') + "/" + GetSegmentFromControllerType(t)
            })
                              .ToList();

            foreach (var controllerEntry in controllers)
            {
                Type localController = controllerEntry.ControllerType;
                router.AddWithPath(controllerEntry.Path,
                                   (r) => {
                    var parameterAttributes = (PathParameterAttribute[])Attribute.GetCustomAttributes(controllerEntry.ControllerType, typeof(PathParameterAttribute));
                    foreach (var pathParameterAttribute in parameterAttributes)
                    {
                        r.Add(pathParameterAttribute.PathTemplate, (cr) => cr.To(localController));
                    }

                    // If no path parameters or any are optional then connect the controller to the router
                    if (parameterAttributes.Count() == 0 ||
                        parameterAttributes.Any(pa => pa.Required == false))
                    {
                        r.To(localController);
                    }
                });
            }
            return(router);
        }
Exemplo n.º 3
0
        public ApiRouter Add(ApiRouter childRouter)
        {
            _childRouters.Add(childRouter.SegmentTemplate, childRouter);

            childRouter.ParentRouter = this;
            return(this);
        }
Exemplo n.º 4
0
        public ApiRouter Add(string childSegmentTemplate, Action <ApiRouter> configure)
        {
            var childrouter = new ApiRouter(childSegmentTemplate);

            configure(childrouter);
            return(Add(childrouter));
        }
Exemplo n.º 5
0
        public Uri GetUrlFromRouter(ApiRouter router)
        {
            string url = router.SegmentTemplate;

            while (router.ParentRouter != null)
            {
                router = router.ParentRouter;
                url    = router.SegmentTemplate + @"/" + url;
            }
            return(new Uri(_baseUrl, url));
        }
Exemplo n.º 6
0
        public static ApiRouter GetAtPath(this ApiRouter router, string path)
        {
            var segments      = path.Split('/');
            var currentRouter = router;

            foreach (var segment in segments)
            {
                if (currentRouter.ChildRouters.ContainsKey(segment))
                {
                    currentRouter = currentRouter.ChildRouters[segment];
                }
                else
                {
                    return(null);
                }
            }
            return(currentRouter);
        }
Exemplo n.º 7
0
 public void AddRouter(ApiRouter router)
 {
     _SegmentRoutes.Add(router);
 }