Skip to content

Commit

Permalink
Migrate server run endpoint to Azure Function
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed Oct 27, 2020
1 parent 68053eb commit a4cf3a9
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 321 deletions.
29 changes: 29 additions & 0 deletions Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<RootNamespace>blazoract.Api</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
<PackageReference Include="Microsoft.DotNet.Interactive.CSharp" Version="1.0.0-beta.20426.1" />
<PackageReference Include="FSharp.Compiler.Private.Scripting" Version="11.0.0-beta.20428.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\blazoract.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
58 changes: 58 additions & 0 deletions Api/KernelFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Commands;
using blazoract.Shared;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;

namespace blazoract.Api
{
public class KernelFunction
{
private CompositeKernel _kernel;
public KernelFunction(CompositeKernel kernel)
{
this._kernel = kernel;
}

[FunctionName("RunCode")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "code/run")] HttpRequest req,
ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var cell = JsonConvert.DeserializeObject<ExecuteRequest>(requestBody);

Console.WriteLine(requestBody);

var request = await _kernel.SendAsync(new SubmitCode(cell.Input), new CancellationToken());
var result = new ExecuteResult();
request.KernelEvents.Subscribe(x =>
{
Console.WriteLine($"Received event: {x}");
switch (x)
{
case DisplayEvent displayEvent:
result.Output = displayEvent.Value?.ToString();
break;
case CommandFailed commandFailed:
result.CommandFailedMessage = commandFailed.Message;
break;
}
});

return new OkObjectResult(result);
}
}
}
56 changes: 56 additions & 0 deletions Api/KernelFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Commands;
using blazoract.Shared;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;

namespace blazoract.Api
{
public class KernelFunction
{
private CompositeKernel _kernel;
public KernelFunction(CompositeKernel kernel)
{
this._kernel = kernel;
}

[FunctionName("RunCode")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "code/run")] HttpRequest req,
ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var cell = JsonConvert.DeserializeObject<ExecuteRequest>(requestBody);

var request = await _kernel.SendAsync(new SubmitCode(cell.Input), new CancellationToken());
var result = new ExecuteResult();
request.KernelEvents.Subscribe(x =>
{
Console.WriteLine($"Received event: {x}");
switch (x)
{
case DisplayEvent displayEvent:
result.Output = displayEvent.Value?.ToString();
break;
case CommandFailed commandFailed:
result.CommandFailedMessage = commandFailed.Message;
break;
}
});

return new OkObjectResult(result);
}
}
}
8 changes: 8 additions & 0 deletions Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Api": {
"commandName": "Project",
"commandLineArgs": "start --cors *"
}
}
}
8 changes: 8 additions & 0 deletions Api/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"storage1": {
"type": "storage",
"connectionId": "AzureWebJobsStorage"
}
}
}
8 changes: 8 additions & 0 deletions Api/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"storage1": {
"type": "storage.emulator",
"connectionId": "AzureWebJobsStorage"
}
}
}
19 changes: 19 additions & 0 deletions Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive;

[assembly: FunctionsStartup(typeof(blazoract.Api.Startup))]

namespace blazoract.Api
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<CompositeKernel>(new CompositeKernel() {
new CSharpKernel().UseDefaultFormatting().UseDotNetVariableSharing()
});
}
}
}
11 changes: 11 additions & 0 deletions Api/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}
}
12 changes: 12 additions & 0 deletions Api/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*",
"CORSCredentials": false
}
}
9 changes: 7 additions & 2 deletions Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ public static async Task Main(string[] args)
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// Configure HTTP client to send requests to the local function server
Console.WriteLine(builder.Configuration["BaseAddress"]);
Console.WriteLine(builder.Configuration);
var baseAddress = builder.Configuration["BaseAddress"] ?? builder.HostEnvironment.BaseAddress;
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });

// Services for managing local notebook storage
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped<NotebookService>();


await builder.Build().RunAsync();
}
}
Expand Down
3 changes: 3 additions & 0 deletions Client/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",

"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand All @@ -19,6 +21,7 @@
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
2 changes: 1 addition & 1 deletion Client/Shared/CodeCell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
{
isEvaluating = true;
var request = new ExecuteRequest(cell.Content);
var response = await Http.PostAsJsonAsync("run/evaluatecell", request);
var response = await Http.PostAsJsonAsync("api/code/run", request);
result = await response.Content.ReadFromJsonAsync<ExecuteResult>();
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"BaseAddress": "http://localhost:7071/"
}
File renamed without changes.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ Blazoract is an interactive notebook user interface implemented in Blazor WebAss

## Development Setup

Before starting development, be sure that you have the following installed:

- [.NET Core](https://dotnet.microsoft.com/download)
- [Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools)

1. Fork and clone this repository locally using Git.

```
$ git clone https://github.com/{yourusername}/blazoract
```

2. Restore the project's dependencies by running `dotnet restore` in the root.
1. Restore the project's dependencies by running `dotnet restore` in the root.

1. Copy the sample configuration file for running Azure functions locally.

```
$ cp `Api/local.settings.example.json` file into `Api/local.settings.json`
```

1. Open a terminal and run the following to launch a local instance of the Azure Functions for this app. You will need to the Azure Functions Core Tools mentioned above to enable this.

```
$ cd Api
$ func start --build
```

3. Run `dotnet run --project Server` to launch the application server.
1. In another terminal window, run `dotnet run --project Client` to start the client application.

4. Navigate to https://localhost:5001 where the contents of the default notebook should load.
1. Navigate to https://localhost:5001 where the contents of the default notebook should load.

![Screen Shot 2020-10-25 at 10 03 24 PM](https://user-images.githubusercontent.com/1857993/97135602-f194b300-170d-11eb-87e4-af81bda68ad5.png)

Expand Down
72 changes: 0 additions & 72 deletions Server/Controllers/RunController.cs

This file was deleted.

Loading

0 comments on commit a4cf3a9

Please sign in to comment.