-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add working directory override (#59)
* add working directory override * fix tests * add test * add new sample project, refactor ManagedProcess.cs * refactor * refactor managed process * refactor * forward WorkingDirectory option
- Loading branch information
Showing
15 changed files
with
415 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...es/AspNetCore/AppConfigurationSample/AppConfigurationSample/AppConfigurationSample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<LangVersion>10</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Dapr.Extensions.Configuration" Version="1.11.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/> | ||
<PackageReference Include="Dapr.AspNetCore" Version="1.11.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Man.Dapr.Sidekick.AspNetCore\Man.Dapr.Sidekick.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
</Project> |
25 changes: 25 additions & 0 deletions
25
.../AspNetCore/AppConfigurationSample/AppConfigurationSample/Controllers/SampleController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Dapr.Client; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AppConfigurationSample.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class SampleController : ControllerBase | ||
{ | ||
[HttpGet(Name = "GetSecret")] | ||
public async Task<IActionResult> Get([FromServices] DaprClient daprClient, [FromServices] IConfiguration configuration) | ||
{ | ||
// Can read secrets by using the client or IConfiguration through DI as well | ||
var clientSecrets = await daprClient.GetSecretAsync("localsecretstore", "secret"); | ||
var clientSecret = string.Join(",", clientSecrets.Select(d => d.Value)); | ||
|
||
var configurationSecret = configuration.GetSection("secret").Value; | ||
|
||
return Ok(new | ||
{ | ||
SecretFromClient = clientSecret, | ||
SecretFromConfiguration = configurationSecret | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...AspNetCore/AppConfigurationSample/AppConfigurationSample/Dapr/Components/secretstore.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: localsecretstore | ||
namespace: default | ||
spec: | ||
type: secretstores.local.file | ||
version: v1 | ||
metadata: | ||
- name: secretsFile | ||
value: "Dapr/secrets.json" | ||
|
3 changes: 3 additions & 0 deletions
3
samples/AspNetCore/AppConfigurationSample/AppConfigurationSample/Dapr/secrets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"secret": "YourPasskeyHere" | ||
} |
101 changes: 101 additions & 0 deletions
101
samples/AspNetCore/AppConfigurationSample/AppConfigurationSample/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Dapr.Client; | ||
using Dapr.Extensions.Configuration; | ||
using Man.Dapr.Sidekick; | ||
using Man.Dapr.Sidekick.Threading; | ||
using Serilog; | ||
|
||
// Add Serilog for enhanced console logging. | ||
Log.Logger = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
try | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Host.UseSerilog(); | ||
var env = builder.Environment; | ||
|
||
// Manage local dapr sidecar | ||
if (env.IsDevelopment()) | ||
{ | ||
// Set options in code | ||
var sidecarOptions = new DaprSidecarOptions | ||
{ | ||
AppId = "AppConfigurationSample", | ||
AppPort = 5000, | ||
DaprGrpcPort = 50001, | ||
ResourcesDirectory = Path.Combine(env.ContentRootPath, "Dapr/Components"), | ||
|
||
// Set the working directory to our project to allow relative paths in component yaml files | ||
WorkingDirectory = env.ContentRootPath | ||
}; | ||
|
||
// Build the default Sidekick controller | ||
var sidekick = new DaprSidekickBuilder().Build(); | ||
|
||
// Start the Dapr Sidecar early in the pipeline, this will come up in the background | ||
sidekick.Sidecar.Start(() => new DaprOptions { Sidecar = sidecarOptions }, DaprCancellationToken.None); | ||
|
||
// Add Dapr Sidekick | ||
builder.Services.AddDaprSidekick(builder.Configuration, o => | ||
{ | ||
o.Sidecar = sidecarOptions; | ||
}); | ||
} | ||
|
||
// Add services to the container. | ||
|
||
builder.Services | ||
.AddControllers() | ||
.AddDapr(); | ||
builder.Services.AddDaprClient(); | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
// Wait for the Dapr sidecar to report healthy before attempting to use any Dapr components. | ||
var client = new DaprClientBuilder().Build(); | ||
using (var tokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(1))) | ||
{ | ||
// use the await keyword in your service instead | ||
await client.WaitForSidecarAsync(tokenSource.Token); | ||
} | ||
|
||
// Get secrets from store during startup | ||
var secret = await client.GetSecretAsync("localsecretstore", "secret"); | ||
|
||
// Use secrets to setup your services | ||
Log.Logger.Information("----- Secret from DaprClient: " + string.Join(",", secret.Select(d => d.Value))); | ||
|
||
// or forward them in the ConfigurationManager | ||
|
||
builder.Configuration.AddDaprSecretStore("localsecretstore", client); | ||
|
||
Log.Logger.Information("----- Secret from ConfigurationManager: " + builder.Configuration.GetSection("secret").Value); | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "Application terminated unexpectedly"); | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...s/AspNetCore/AppConfigurationSample/AppConfigurationSample/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...les/AspNetCore/AppConfigurationSample/AppConfigurationSample/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
samples/AspNetCore/AppConfigurationSample/AppConfigurationSample/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ASP.NET Core Controller example | ||
|
||
This sample shows using Dapr with ASP.NET Core controllers. This application is a simple Web API application. | ||
The application uses a Dapr secret store to read secrets. | ||
The sample is starting a Dapr Sidekick process very early in the execution of the `Program.cs` | ||
and then waits for the sidecar components to be loaded so that we can read secrets from the secret store | ||
and use them when we setup our services. | ||
The Sidekick hosted service will then reuse the same daprd process instance and manage it as usual. | ||
|
||
## How Dapr Sidekick was added | ||
|
||
The main change to the ASPCore template is the following block of code at the beginning of the `Program.cs`: | ||
|
||
```csharp | ||
// Manage local dapr sidecar | ||
if (env.IsDevelopment()) | ||
{ | ||
// Set options in code | ||
var sidecarOptions = new DaprSidecarOptions | ||
{ | ||
AppId = "AppConfigurationSample", | ||
AppPort = 5000, | ||
DaprGrpcPort = 50001, | ||
ResourcesDirectory = Path.Combine(env.ContentRootPath, "Dapr/Components"), | ||
|
||
// Set the working directory to our project to allow relative paths in component yaml files | ||
WorkingDirectory = env.ContentRootPath | ||
}; | ||
|
||
// Build the default Sidekick controller | ||
var sidekick = new DaprSidekickBuilder().Build(); | ||
|
||
// Start the Dapr Sidecar early in the pipeline, this will come up in the background | ||
sidekick.Sidecar.Start(() => new DaprOptions { Sidecar = sidecarOptions }, DaprCancellationToken.None); | ||
|
||
// Add Dapr Sidekick | ||
builder.Services.AddDaprSidekick(builder.Configuration, o => | ||
{ | ||
o.Sidecar = sidecarOptions; | ||
}); | ||
} | ||
``` | ||
|
||
## Running the sample | ||
|
||
To run the sample simply set `AppConfigurationSample` as the startup project and run it in Visual Studio, it will launch the Dapr sidecar and connect to it. |
42 changes: 42 additions & 0 deletions
42
src/Man.Dapr.Sidekick/Options/DaprManagedProcessOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Specialized; | ||
using Man.Dapr.Sidekick.Logging; | ||
using Man.Dapr.Sidekick.Threading; | ||
|
||
namespace Man.Dapr.Sidekick.Options | ||
{ | ||
public class DaprManagedProcessOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the full path to the system process. | ||
/// </summary> | ||
public string Filename { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a working directory to be used by the managed process. | ||
/// This is required by some components that use relevant directory paths to load local files from the project folders. | ||
/// Defaults to %USERPROFILE%/.dapr ($HOME/.dapr on Linux) if not specified. | ||
/// </summary> | ||
public string WorkingDirectory { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets optional command-line process arguments. | ||
/// </summary> | ||
public string Arguments { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets an optional <see cref="IDaprLogger"/> instance for receiving stdout log messages from the process. | ||
/// </summary> | ||
public IDaprLogger Logger { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a <see cref="DaprCancellationToken"/> for aborting the process startup operation. | ||
/// </summary> | ||
public DaprCancellationToken CancellationToken { get; set; } = default; | ||
|
||
/// <summary> | ||
/// Gets or sets an optional action for configuring any environment variables for the process. | ||
/// </summary> | ||
public Action<StringDictionary> ConfigureEnvironmentVariables { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.