Skip to content

Commit 643fe95

Browse files
Introduces configuration extension methods
1 parent db85226 commit 643fe95

4 files changed

Lines changed: 120 additions & 23 deletions

File tree

DevProxy.Hosting/DevProxyContainerResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace DevProxy.Hosting;
55
/// <summary>
66
/// Exposes Dev Proxy as a Docker container.
77
/// </summary>
8-
public sealed class DevProxyContainerResource(string name):
8+
public sealed class DevProxyContainerResource(string name) :
99
ContainerResource(name)
1010
{
11-
}
11+
}

DevProxy.Hosting/DevProxyExecutableResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace DevProxy.Hosting;
55
/// <summary>
66
/// Exposes Dev Proxy through its locally installed executable.
77
/// </summary>
8-
public sealed class DevProxyExecutableResource(string name):
8+
public sealed class DevProxyExecutableResource(string name) :
99
ExecutableResource(name, "devproxy", "")
1010
{
11-
}
11+
}

DevProxy.Hosting/DevProxyResourceBuilderExtensions.cs

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,42 @@ public static IResourceBuilder<DevProxyExecutableResource> AddDevProxyExecutable
2222
{
2323
var resource = new DevProxyExecutableResource(name);
2424

25-
return builder.AddResource(resource)
25+
var resourceBuilder = builder.AddResource(resource)
2626
.WithArgs("--as-system-proxy", "false")
2727
.WithHttpEndpoint(targetPort: 8000, name: DevProxyResource.ProxyEndpointName)
2828
.WithHttpEndpoint(targetPort: 8897, name: DevProxyResource.ApiEndpointName);
29+
30+
return resourceBuilder;
31+
}
32+
33+
/// <summary>
34+
/// Configures the DevProxy executable to use a specific configuration file.
35+
/// </summary>
36+
/// <param name="builder">The resource builder for the DevProxy executable resource.</param>
37+
/// <param name="configFile">The path to the configuration file.</param>
38+
/// <returns>The resource builder for method chaining.</returns>
39+
public static IResourceBuilder<DevProxyExecutableResource> WithConfigFile(
40+
this IResourceBuilder<DevProxyExecutableResource> builder,
41+
string configFile)
42+
{
43+
return builder.WithArgs("-c", Path.GetFullPath(configFile, builder.ApplicationBuilder.AppHostDirectory));
44+
}
45+
46+
/// <summary>
47+
/// Configures the DevProxy executable to watch specific URLs.
48+
/// </summary>
49+
/// <param name="builder">The resource builder for the DevProxy executable resource.</param>
50+
/// <param name="urlsToWatch">The collection of URLs to watch.</param>
51+
/// <returns>The resource builder for method chaining.</returns>
52+
public static IResourceBuilder<DevProxyExecutableResource> WithUrlsToWatch(
53+
this IResourceBuilder<DevProxyExecutableResource> builder,
54+
Func<IEnumerable<string>> urlsToWatch)
55+
{
56+
return builder.WithArgs(context =>
57+
{
58+
context.Args.Add("-u");
59+
context.Args.Add(string.Join(" ", urlsToWatch()));
60+
});
2961
}
3062

3163
/// <summary>
@@ -40,20 +72,94 @@ public static IResourceBuilder<DevProxyContainerResource> AddDevProxyContainer(
4072
{
4173
var resource = new DevProxyContainerResource(name);
4274

43-
return builder.AddResource(resource)
75+
var resourceBuilder = builder.AddResource(resource)
4476
.WithImage(DevProxyContainerImageTags.Image)
4577
.WithImageRegistry(DevProxyContainerImageTags.Registry)
4678
.WithImageTag(DevProxyContainerImageTags.Tag)
4779
.WithHttpEndpoint(targetPort: 8000, name: DevProxyResource.ProxyEndpointName)
4880
.WithHttpEndpoint(targetPort: 8897, name: DevProxyResource.ApiEndpointName);
81+
82+
return resourceBuilder;
83+
}
84+
85+
/// <summary>
86+
/// Configures the DevProxy container to mount a certificate folder.
87+
/// </summary>
88+
/// <param name="builder">The resource builder for the DevProxy container resource.</param>
89+
/// <param name="certFolder">The path to the certificate folder on the host.</param>
90+
/// <returns>The resource builder for method chaining.</returns>
91+
public static IResourceBuilder<DevProxyContainerResource> WithCertFolder(
92+
this IResourceBuilder<DevProxyContainerResource> builder,
93+
string certFolder)
94+
{
95+
return builder.WithBindMount(
96+
Path.GetFullPath(certFolder, builder.ApplicationBuilder.AppHostDirectory),
97+
"/home/devproxy/.config/dev-proxy/rootCert");
98+
}
99+
100+
/// <summary>
101+
/// Configures the DevProxy executable to use a specific configuration file.
102+
/// </summary>
103+
/// <param name="builder">The resource builder for the DevProxy executable resource.</param>
104+
/// <param name="configFile">The path to the configuration file.</param>
105+
/// <returns>The resource builder for method chaining.</returns>
106+
public static IResourceBuilder<DevProxyContainerResource> WithConfigFile(
107+
this IResourceBuilder<DevProxyContainerResource> builder,
108+
string configFile)
109+
{
110+
return builder.WithArgs("-c", configFile);
111+
}
112+
113+
/// <summary>
114+
/// Configures the DevProxy container to mount a configuration folder.
115+
/// </summary>
116+
/// <param name="builder">The resource builder for the DevProxy container resource.</param>
117+
/// <param name="configFolder">The path to the configuration folder on the host.</param>
118+
/// <returns>The resource builder for method chaining.</returns>
119+
public static IResourceBuilder<DevProxyContainerResource> WithConfigFolder(
120+
this IResourceBuilder<DevProxyContainerResource> builder,
121+
string configFolder)
122+
{
123+
return builder.WithBindMount(
124+
Path.GetFullPath(configFolder, builder.ApplicationBuilder.AppHostDirectory),
125+
"/config");
126+
}
127+
128+
/// <summary>
129+
/// Configures the DevProxy container to watch specific URLs.
130+
/// </summary>
131+
/// <param name="builder">The resource builder for the DevProxy container resource.</param>
132+
/// <param name="urlsToWatch">The collection of URLs to watch.</param>
133+
/// <returns>The resource builder for method chaining.</returns>
134+
public static IResourceBuilder<DevProxyContainerResource> WithUrlsToWatch(
135+
this IResourceBuilder<DevProxyContainerResource> builder,
136+
Func<IEnumerable<string>> urlsToWatch)
137+
{
138+
return builder.WithArgs(context =>
139+
{
140+
context.Args.Add("-u");
141+
context.Args.Add(string.Join(" ", urlsToWatch()));
142+
});
49143
}
50144
}
51145

146+
/// <summary>
147+
/// Contains container image tags and registry information for DevProxy.
148+
/// </summary>
52149
internal static class DevProxyContainerImageTags
53150
{
151+
/// <summary>
152+
/// The container registry hosting the DevProxy image.
153+
/// </summary>
54154
internal const string Registry = "ghcr.io";
55155

156+
/// <summary>
157+
/// The DevProxy container image name.
158+
/// </summary>
56159
internal const string Image = "dotnet/dev-proxy";
57160

161+
/// <summary>
162+
/// The DevProxy container image tag.
163+
/// </summary>
58164
internal const string Tag = "latest";
59165
}

README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,13 @@ var apiService = builder.AddProject<Projects.AspireStarterApp_ApiService>("apise
3131
// Add Dev Proxy as a container resource
3232
var devProxy = builder.AddDevProxyContainer("devproxy")
3333
// specify the Dev Proxy configuration file
34-
.WithArgs("-c", "./devproxyrc.json")
35-
.WithArgs(context =>
36-
{
37-
context.Args.Add("-u");
38-
// let Dev Proxy intercept requests to the API service
39-
context.Args.Add($"{apiService.GetEndpoint("https").Url}/*");
40-
})
34+
.WithConfigFile("./devproxy.json")
4135
// mount the local folder with PFX certificate for intercepting HTTPS traffic
42-
.WithBindMount(Path.Combine(AppContext.BaseDirectory, ".devproxy", "cert"),
43-
"/home/devproxy/.config/dev-proxy/rootCert")
36+
.WithCertFolder(".devproxy/cert")
4437
// mount the local folder with Dev Proxy configuration
45-
.WithBindMount(Path.Combine(AppContext.BaseDirectory, ".devproxy", "config"),
46-
"/config");
38+
.WithConfigFolder(".devproxy/config")
39+
// let Dev Proxy intercept requests to the API service
40+
.WithUrlsToWatch(() => [$"{apiService.GetEndpoint("https").Url}/*"]);
4741

4842
// Add a web frontend project and configure it to use Dev Proxy
4943
builder.AddProject<Projects.AspireStarterApp_Web>("webfrontend")
@@ -72,11 +66,8 @@ var apiService = builder.AddProject<Projects.AspireStarterApp_ApiService>("apise
7266
.WithHttpsHealthCheck("/health");
7367

7468
var devProxy = builder.AddDevProxyExecutable("devproxy")
75-
.WithArgs("-c", Path.Combine(".devproxy", "config", "devproxy.json"))
76-
.WithArgs(context => {
77-
context.Args.Add("-u");
78-
context.Args.Add($"{apiService.GetEndpoint("https").Url}/*");
79-
});
69+
.WithConfigFile(".devproxy/config/devproxy.json")
70+
.WithUrlsToWatch(() => [$"{apiService.GetEndpoint("https").Url}/*"]);
8071

8172
// Add a web frontend project and configure it to use Dev Proxy
8273
builder.AddProject<Projects.AspireStarterApp_Web>("webfrontend")
@@ -95,7 +86,7 @@ builder.Build().Run();
9586

9687
- **Container Resource**: Use `AddDevProxyContainer` to run Dev Proxy as a containerized service.
9788
- **Executable Resource**: Use `AddDevProxyExecutable` to run Dev Proxy from the locally installed executable.
98-
- **Custom Configuration**: Pass custom arguments and bind mounts to configure Dev Proxy as needed.
89+
- **Custom Configuration**: Use the extension methods to configure Dev Proxy as needed.
9990
- **Service Integration**: Easily integrate Dev Proxy with other services in your application.
10091

10192
## Contributing

0 commit comments

Comments
 (0)