public static IBaristaModuleLoader Invoke(BrewOrder brewOrder, HttpRequest req) { //Set up the module loader for the request. var moduleLoader = new PrioritizedAggregateModuleLoader(); //Register all the modules within the BaristaLabs.BaristaCore.Extensions assembly. moduleLoader.RegisterModuleLoader(new AssemblyModuleLoader(typeof(TypeScriptTranspiler).Assembly), 1); //Register modules needing context. var currentContextModule = new BaristaContextModule(req); var contextModuleLoader = new InMemoryModuleLoader(); contextModuleLoader.RegisterModule(currentContextModule); moduleLoader.RegisterModuleLoader(contextModuleLoader, 2); //Register the web resource module loader rooted at the target path. if (Uri.TryCreate(new Uri(brewOrder.BaseUrl, UriKind.Absolute), brewOrder.Path, out Uri targetUri)) { var targetPath = targetUri.GetLeftPart(UriPartial.Authority) + String.Join("", targetUri.Segments.Take(targetUri.Segments.Length - 1)); var webResourceModuleLoader = new WebResourceModuleLoader(targetPath); moduleLoader.RegisterModuleLoader(webResourceModuleLoader, 100); } return(moduleLoader); }
public IBaristaRuntimeFactory GetRuntimeFactory() { var webResourceModuleLoader = new WebResourceModuleLoader("https://raw.githubusercontent.com/BaristaLabs/curly-octo-umbrella/master/"); var serviceCollection = new ServiceCollection(); serviceCollection.AddBaristaCore(moduleLoader: webResourceModuleLoader); var provider = serviceCollection.BuildServiceProvider(); return(provider.GetRequiredService <IBaristaRuntimeFactory>()); }
/// <summary> /// Attempts to retrieve the resource using a GET request. /// </summary> /// <param name="baseUrl"></param> /// <param name="resource"></param> /// <param name="content"></param> /// <param name="language"></param> /// <returns></returns> private static async Task <bool> TryGetResource(BrewOrder brewOrder, Uri requestUri) { using (var response = await s_httpClient.GetAsync(requestUri)) { if (response.IsSuccessStatusCode) { brewOrder.Code = await response.Content.ReadAsStringAsync(); brewOrder.Language = WebResourceModuleLoader.GetCanonicalResourceKind(brewOrder.Path, response.Content.Headers.ContentType.ToString()); return(true); } } brewOrder.Code = null; brewOrder.Language = ResourceKind.Binary; return(false); }