public static T UseHttpApi <T>(this T kernel, HttpPort httpPort, HttpProbingSettings httpProbingSettings) where T : Kernel { var initApiCommand = new Command("#!enable-http") { IsHidden = true, Handler = CommandHandler.Create((InvocationContext cmdLineContext) => { var context = cmdLineContext.GetService <KernelInvocationContext>(); if (context.Command is SubmitCode) { var probingUrls = httpProbingSettings is not null ? httpProbingSettings.AddressList : new[] { new Uri($"http://localhost:{httpPort}") }; var html = HttpApiBootstrapper.GetHtmlInjection(probingUrls, httpPort?.ToString() ?? Guid.NewGuid().ToString("N")); context.Display(html, "text/html"); } return(Task.CompletedTask); }) }; kernel.AddDirective(initApiCommand); return(kernel); }
public static IWebHostBuilder UseDotNetInteractiveHttpApi(this IWebHostBuilder builder, bool enableHttpApi, HttpPort httpPort, HttpProbingSettings httpProbingSettings, IServiceCollection services) { var httpStartupOptions = new HttpOptions(enableHttpApi, httpPort); builder.ConfigureServices(c => { c.AddSingleton(httpStartupOptions); }) .ConfigureServices(c => { if (enableHttpApi && httpProbingSettings != null) { c.AddSingleton(httpProbingSettings); } c.AddSingleton(httpStartupOptions); if (services != null) { foreach (var serviceDescriptor in services) { c.Add(serviceDescriptor); } } }); return(builder); }
public static IApplicationBuilder UseDotNetInteractiveHttpApi <T>(this IApplicationBuilder app, T kernel, Assembly staticResourceRoot, HttpProbingSettings httpProbingSettings, HttpPort httpPort) where T : Kernel { app.UseStaticFiles(new StaticFileOptions { FileProvider = new FileProvider(kernel, staticResourceRoot) }); app.UseWebSockets(); app.UseCors("default"); app.UseRouting(); app.UseRouter(r => { r.Routes.Add(new VariableRouter(kernel)); r.Routes.Add(new KernelsRouter(kernel)); var htmlNotebookFrontedEnvironment = kernel.FrontendEnvironment as HtmlNotebookFrontedEnvironment; if (htmlNotebookFrontedEnvironment is { }) { r.Routes.Add(new DiscoveryRouter(htmlNotebookFrontedEnvironment)); r.Routes.Add(new HttpApiTunnelingRouter(htmlNotebookFrontedEnvironment)); } if (htmlNotebookFrontedEnvironment is null || htmlNotebookFrontedEnvironment.RequiresAutomaticBootstrapping) { var enableHttp = new SubmitCode("#!enable-http", kernel.Name); enableHttp.PublishInternalEvents(); kernel.DeferCommand(enableHttp); } kernel = kernel.UseHttpApi(httpPort, httpProbingSettings); });