Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

.env
launchSettings.json

*.user
*.pubxml
Expand Down
75 changes: 75 additions & 0 deletions source/AppHost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Management.Automation;
using Nivot.Aspire.Hosting.PowerShell;

var builder = DistributedApplication.CreateBuilder(args);

const string webappPort = "44200";
const string ContainerHostAuthorizationToken = "d344827a-ca42-4159-95f6-5fb9551d62aa";

var storage = builder.AddAzureStorage("storage")
.RunAsEmulator(options => options.WithLifetime(ContainerLifetime.Persistent).WithBlobPort(10000));
var storageBlob = storage.AddBlobs("cache");
var blobContainer = storageBlob.AddBlobContainer("edge");

var ps = builder.AddPowerShell("ps", PSLanguageMode.FullLanguage);

var containerManagerBinPath = Path.Join(Directory.GetParent(new Projects.Container_Manager().ProjectPath)!.FullName, "bin", "Debug", "net9.0");
var setupContainerManager = ps.AddScript("Preparing-container-host",
$"""
$containerCapabilityId = New-Object Security.Principal.SecurityIdentifier @(
'S-1-15-3-1024-4233803318-1181731508-1220533431-3050556506-2713139869-1168708946-594703785-1824610955'
)
$aclRule = New-Object Security.AccessControl.FileSystemAccessRule @(
$containerCapabilityId,
[Security.AccessControl.FileSystemRights]::ReadAndExecute,
([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [Security.AccessControl.InheritanceFlags]::ObjectInherit),
[Security.AccessControl.PropagationFlags]::None,
[Security.AccessControl.AccessControlType]::Allow
)

$binPath = '{containerManagerBinPath}'
$acl = Get-Acl $binPath
$acl.AddAccessRule($aclRule);
Set-Acl $binPath -AclObject $acl
""").WithArgs(containerManagerBinPath);

var containerManager = builder.AddProject<Projects.Container_Manager>("container-manager")
.WithEnvironment("SHARPLAB_CONTAINER_HOST_AUTHORIZATION_TOKEN", ContainerHostAuthorizationToken)
.WaitForCompletion(setupContainerManager);

var mirrorSharp = builder.AddExecutable("mirrorsharp-ci", "npm", "../#external/mirrorsharp/WebAssets", "ci");
var mirrorSharpBuild = builder.AddNpmApp("mirrorsharp-build", "../#external/mirrorsharp/WebAssets", "build")
.WithParentRelationship(mirrorSharp)
.WaitForCompletion(mirrorSharp);

var mirrorsharpPreview = builder.AddExecutable("mirrorsharp-preview", "npm", "../#external/mirrorsharp-codemirror-6-preview/WebAssets", "ci");
var mirrorsharpPreviewBuild = builder.AddNpmApp("mirrorsharp-preview-build", "../#external/mirrorsharp-codemirror-6-preview/WebAssets", "build")
.WithParentRelationship(mirrorsharpPreview)
.WaitForCompletion(mirrorsharpPreview);

var installWebApp = builder.AddExecutable("install-webapp", "npm", "../WebApp", "install")
.WaitForCompletion(mirrorSharpBuild)
.WaitForCompletion(mirrorsharpPreviewBuild);
var buildWebApp = builder.AddNpmApp("build-webapp", "../WebApp", "build")
.WithParentRelationship(installWebApp)
.WaitForCompletion(installWebApp);
var webapp = builder.AddNpmApp("webapp", "../WebApp", scriptName: "start:fast")
.WaitForCompletion(buildWebApp)
.WithHttpEndpoint(env: "PORT")
.WithEnvironment("PORT", webappPort)
.WithEnvironment("test", blobContainer).WaitFor(blobContainer);
installWebApp.WithParentRelationship(webapp);

var webappServer = builder.AddProject<Projects.WebApp_Server>("webapp-server")
.WaitFor(webapp)
.WaitFor(containerManager)
.WithEnvironment("SHARPLAB_LOCAL_SECRETS_PublicStorageConnectionString", storageBlob).WaitFor(storageBlob)
.WithEnvironment("SHARPLAB_CONTAINER_HOST_URL", containerManager.GetEndpoint("http"))
.WithEnvironment("SHARPLAB_ASSETS_BASE_URL", $"http://localhost:{webappPort}/")
.WithEnvironment("SHARPLAB_ASSETS_LATEST_URL_V2", $"http://localhost:{webappPort}/latest")
.WithEnvironment("SHARPLAB_LOCAL_SECRETS_ContainerHostAuthorizationToken", ContainerHostAuthorizationToken)
.WithEnvironment("SHARPLAB_WEBAPP_NAME", "local")
.WithEnvironment("SHARPLAB_CACHE_PATH_PREFIX", "edge")
.WithEnvironment("SHARPLAB_ASSETS_RELOAD_TOKEN", "12345");

builder.Build().Run();
25 changes: 25 additions & 0 deletions source/AppHost/AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.3.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>9cc6fba6-3727-4501-b54b-d61cf8eae623</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.3.0" />
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="9.3.0" />
<PackageReference Include="Aspire.Hosting.NodeJs" Version="9.3.0" />
<PackageReference Include="Nivot.Aspire.Hosting.PowerShell" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WebApp.Server\WebApp.Server.csproj" />
<ProjectReference Include="..\Container.Manager\Container.Manager.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions source/AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17000;http://localhost:15000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21000",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22000"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19000",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20000"
}
}
}
}
13 changes: 13 additions & 0 deletions source/AppHost/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Aspire Setup

run:

```bash
git submodule update --init --recursive
```

# Aspire AppHost

```bash
dotnet run --project source/AppHost
```
8 changes: 8 additions & 0 deletions source/AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions source/AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
1 change: 1 addition & 0 deletions source/Container.Manager/Container.Manager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ProjectReference Include="..\#external\Fragile\Fragile\Fragile.csproj" />
<ProjectReference Include="..\Container.Warmup\Container.Warmup.csproj" />
<ProjectReference Include="..\Container\Container.csproj" />
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions source/Container.Manager/Internal/ExecutionManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace SharpLab.Container.Manager.Internal {
public class ExecutionManager {
Expand Down Expand Up @@ -30,6 +32,7 @@ CancellationToken cancellationToken
) {
// Note that _containers are never accessed through multiple threads for the same session id,
// so atomicity is not required within same session id
using var activity = SharpLabActivities.Source.StartActivity("Container Execution", ActivityKind.Internal);
using var allocationCancellation = CancellationFactory.ContainerAllocation(cancellationToken);
if (_containerPool.GetSessionContainer(sessionId) is not {} container) {
if (_crashSuspensionManager.GetSuspension(sessionId) is {} suspension)
Expand All @@ -51,6 +54,10 @@ CancellationToken cancellationToken
cancellationToken
);

if (!result.IsSuccess) {
activity?.SetStatus(ActivityStatusCode.Error, result.FailureMessage.ToString());
}

if (container.HasExited())
return RemoveContainerAndSetSuspension(sessionId, result);

Expand Down
18 changes: 12 additions & 6 deletions source/Container.Manager/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Runtime.Versioning;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using SharpLab.Container.Manager.Internal;

Expand All @@ -8,11 +8,17 @@ namespace SharpLab.Container.Manager {
public class Program {
public static void Main(string[] args) {
DotEnv.Load();
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

var startup = new Startup();
startup.ConfigureServices(builder.Services);

var app = builder.Build();
app.MapDefaultEndpoints();
startup.Configure(app);
app.Run();
}
}
}
23 changes: 23 additions & 0 deletions source/Container.Manager/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:5014;http://localhost:5015",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5015",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Autofac.Core;
using Autofac.Core.Registration;
using JetBrains.Annotations;

namespace Autofac.Extras.FileSystemRegistration.Internal {
public class DirectoryModuleRegistrar : IDirectoryModuleRegistrar {
private readonly ContainerBuilder _builder;
private readonly string[] _directoryPaths;

private string _filePattern = "*.*";
private Func<FileInfo, bool> _fileFilter = f => f.Extension.Equals(".dll", StringComparison.InvariantCultureIgnoreCase)
|| f.Extension.Equals(".exe", StringComparison.InvariantCultureIgnoreCase);
private Func<Assembly, bool> _assemblyFilter = a => true;

public DirectoryModuleRegistrar([NotNull] ContainerBuilder builder, params string[] directoryPaths)
{
ArgumentNullException.ThrowIfNull(builder);
_builder = builder;
_directoryPaths = directoryPaths;

var callback = _builder.RegisterCallback(DiscoverModules);
RegistrarData = new ModuleRegistrarData(callback);
}

public ModuleRegistrarData RegistrarData { get; }

public IDirectoryModuleRegistrar WhereFileMatches(string filePattern)
{
ArgumentNullException.ThrowIfNull(filePattern);

_filePattern = filePattern;
return this;
}

public IDirectoryModuleRegistrar WhereFile(Func<FileInfo, bool> filter) {
ArgumentNullException.ThrowIfNull(filter);

_fileFilter = filter;
return this;
}

public IDirectoryModuleRegistrar WhereAssembly(Func<Assembly, bool> filter) {
ArgumentNullException.ThrowIfNull(filter);

_assemblyFilter = filter;
return this;
}

private void DiscoverModules(IComponentRegistryBuilder registry) {
var files = _directoryPaths.Select(p => new DirectoryInfo(p))
.SelectMany(d => d.GetFiles(_filePattern))
.Where(_fileFilter);

var registrar = new ImmediateModuleRegistrar(registry, this.RegistrarData);
foreach (var file in files) {
var assembly = LoadAssemblySafe(file);
if (assembly == null || !_assemblyFilter(assembly))
continue;

registrar.RegisterAssemblyModules(assembly);
}
}

[CanBeNull]
private static Assembly? LoadAssemblySafe(FileInfo file) {
try {
return Assembly.LoadFrom(file.FullName);
}
catch (BadImageFormatException) {
return null;
}
}

[NotNull]
public IModuleRegistrar RegisterModule([NotNull] IModule module) {
ArgumentNullException.ThrowIfNull(module);
_builder.RegisterCallback(module.Configure);
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.IO;
using System.Reflection;
using Autofac.Core.Registration;
using JetBrains.Annotations;

namespace Autofac.Extras.FileSystemRegistration.Internal {
public interface IDirectoryModuleRegistrar : IModuleRegistrar {
[NotNull] IDirectoryModuleRegistrar WhereFileMatches([NotNull] string filePattern);
[NotNull] IDirectoryModuleRegistrar WhereFile([NotNull] Func<FileInfo, bool> filter);
[NotNull] IDirectoryModuleRegistrar WhereAssembly([NotNull] Func<Assembly, bool> filter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Autofac.Core;
using Autofac.Core.Registration;
using JetBrains.Annotations;

namespace Autofac.Extras.FileSystemRegistration.Internal
{
public class ImmediateModuleRegistrar : IModuleRegistrar
{
private readonly IComponentRegistryBuilder _registry;

public ImmediateModuleRegistrar([NotNull] IComponentRegistryBuilder registry, ModuleRegistrarData registrarData)
{
ArgumentNullException.ThrowIfNull(registry);
ArgumentNullException.ThrowIfNull(registrarData);

_registry = registry;
RegistrarData = registrarData;
}

public ModuleRegistrarData RegistrarData { get; }

public IModuleRegistrar RegisterModule(IModule module)
{
module.Configure(_registry);
return this;
}
}
}
Loading