예제 #1
0
        public async Task RenderAsync(Request req, Response res, NextAsync next)
        {
            var path = req.Path.Substring(Route.Length);

            // Destacando a ação de uma URI, como em /My/Path/-MyAction
            var tokens      = path.Split("/-");
            var paperPath   = tokens.First();
            var paperAction = tokens.Skip(1).FirstOrDefault() ?? "Index";

            var paper = paperCatalog.FindExact(paperPath).FirstOrDefault();

            if (paper == null)
            {
                await next.Invoke();

                return;
            }

            var hasAction = paper.GetMethod(paperAction) != null;

            if (!hasAction)
            {
                await next.Invoke();

                return;
            }

            var args = new Args();

            args.AddMany(req.QueryArgs);
            args.AddMany(Args.ParsePathArgs(path, paper.PathTemplate));

            var context = new PaperContext();

            context.Paper    = paper;
            context.Path     = paperPath;
            context.Action   = paperAction;
            context.Args     = args;
            context.Request  = req;
            context.Response = res;

            var caller   = objectFactory.CreateObject <PaperCaller>();
            var renderer = objectFactory.CreateObject <PaperRenderer>();

            Ret <Result> result = await caller.CallAsync(context);

            if (result.Status.CodeClass != HttpStatusClass.Success)
            {
                var entity = HttpEntity.CreateFromRet(req.RequestUri, result);
                await SendAsync(res, result, entity);

                return;
            }

            Ret <Entity> media = await renderer.RenderAsync(context, result);

            if (!media.Ok)
            {
                var entity = HttpEntity.CreateFromRet(req.RequestUri, result);
                await SendAsync(res, media, entity);

                return;
            }

            await SendAsync(res, media, media.Value);
        }