public async Task RenderAsync(Request request, Response response, NextAsync next) { var path = request.Path; SiteDescriptor siteDescriptor = new SiteDescriptor(); IEnumerable <MediaRenderer> renderers = siteDescriptor.FindMediaRenderers(path); if (renderers?.Any() != true) { await next.Invoke(); return; } var entity = new Entity(); renderers = renderers.OrderBy(x => x.Priority); foreach (var renderer in renderers) { } await response.WriteEntityAsync(entity); }
public async Task InvokeAsync(IRequestContext ctx, IObjectFactory objectFactory, NextAsync next) { var req = ctx.Request; var res = ctx.Response; try { ConfigureRequest(req); ConfigureResponse(res); var router = objectFactory.GetInstance <IRouter>(); var routes = router.Find(req.RequestPath); var iterator = routes.GetEnumerator(); NextAsync chain = null; chain = new NextAsync(async() => { if (iterator.MoveNext()) { var route = iterator.Current; var pipeline = route.CreatePipeline(objectFactory); await pipeline.RunAsync(ctx, chain); } else { await next(); } }); await chain.Invoke(); } catch (Exception ex) { ex.Trace(); var status = HttpStatusCode.InternalServerError; res.Status = status; res.Headers[HeaderNames.ContentType] = "text/plain; charset=UTF-8"; var ln = Environment.NewLine; await res.SendAsync( $"{(int)status} - {status.ToString().ChangeCase(TextCase.ProperCase)}{ln}{ex.Message}{ln}Caused by:{ln}{ex.GetStackTrace()}" ); } finally { try { res.Body.Close(); } catch { /* Nada a fazer */ } } }
public async Task RenderAsync(Request req, Response res) { try { var papers = pipelineCatalog?.Find(req.Path); if (papers == null) { var status = HttpStatusCode.ServiceUnavailable; var message = "Paper não configurado corretamente. " + "A instância de IPaperCatalog não foi configurada na instância de IServiceProvider."; await WriteAsync(req, res, status, message, null); return; } if (!papers.Any()) { var status = HttpStatusCode.NotFound; var message = $"Não existe uma rota para: {req.PathBase}{req.Path}"; await WriteAsync(req, res, status, message, null); return; } var enumerator = papers.GetEnumerator(); NextAsync next = null; next = new NextAsync(async() => { if (enumerator.MoveNext()) { var paper = enumerator.Current; await paper.RenderAsync(req, res, next); } else { await WriteAsync(req, res, HttpStatusCode.NotFound, null, null); } }); await next.Invoke(); } catch (Exception ex) { var status = ex is HttpException http ? http.Status : HttpStatusCode.InternalServerError; await WriteAsync(req, res, status, ex.Message, ex); } }
public async Task RenderAsync(Request request, Response response, NextAsync next) { var path = request.Path.Substring(Route.Length); var renderer = SiteDescriptor.GetRenderer(path, request.Method); if (renderer != null) { await renderer.Invoke(request, response, next); } else { await next.Invoke(); } }
public async Task RenderAsync(Request request, Response response, NextAsync next) { await next.Invoke(); //var path = request.Path.Substring(Route.Length); //var route = new UriString(this.Route); //var catalog = siteMapCatalog.FindExact(path).FirstOrDefault(); //if (catalog == null) //{ // await next.Invoke(); // return; //} //var parent = siteMapCatalog.FindExact(path + "/..").FirstOrDefault(); //var children = siteMapCatalog.FindExact(path + "/*").ToArray(); //var entity = new Entity(); //var rowHeaders = new List<string>(); //entity.AddClass(ClassNames.Record); //entity.AddClass(typeof(ISiteMap)); //entity.SetTitle(catalog.Title); //entity.SetSelfLink(route.Append(catalog.Href).ToHref()); //foreach (var property in catalog.Properties.NonNull()) //{ // if (property.Value != null) // { // entity.AddHeader(property.Name, dataType: property.Value.GetType()); // entity.AddProperty(property.Name, property.Value); // } //} //if (parent != null) //{ // entity.AddLink(route.Append(parent.Href).ToHref(), parent.Title, Rel.Up); //} //var subItems = children.Concat(catalog.Items.NonNull()); //foreach (var item in subItems) //{ // var row = new Entity(); // row.AddClass(ClassNames.Data); // row.AddClass(Conventions.MakeName((item is ISiteMap) ? typeof(ISiteMap) : typeof(IRoute))); // row.AddRel(RelNames.Rows); // row.SetTitle(item.Title); // row.SetSelfLink((item is ISiteMap) ? route.Append(item.Href).ToHref() : item.Href); // foreach (var property in item.Properties.NonNull()) // { // if (property.Value == null) // continue; // if (!property.Name.EqualsAnyIgnoreCase(rowHeaders)) // { // rowHeaders.Add(property.Name); // entity.AddRowHeader(property.Name, dataType: Conventions.MakeDataType(property.Value.GetType())); // } // row.AddDataHeader(property.Name, dataType: Conventions.MakeDataType(property.Value.GetType())); // row.AddProperty(property.Name, property.Value); // } // entity.AddRow(row); //} //await response.WriteEntityAsync(entity); }
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); }