Skip to content

Commit

Permalink
Add compression-level option (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
onionhammer authored Mar 30, 2024
1 parent cd6542b commit fa21e93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/dotnet-serve/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel.DataAnnotations;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using McMaster.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -124,6 +125,9 @@ public virtual bool UseTls
[Option("-b|--brotli", Description = "Enable brotli compression")]
public bool? UseBrotli { get; internal set; }

[Option("--compression-level", Description = "The level of compression to use, when compression is enabled")]
public CompressionLevel? CompressionLevel { get; internal set; }

[Option("-c|--cors", Description = "Enable CORS (It will enable CORS for all origin and all methods)")]
public bool? EnableCors { get; internal set; }

Expand Down
4 changes: 4 additions & 0 deletions src/dotnet-serve/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO.Compression;
using DotNetConfig;
using McMaster.Extensions.CommandLineUtils;

Expand Down Expand Up @@ -93,6 +94,9 @@ private static void ReadConfig(ConfigSection config, CommandLineOptions model)
model.CertificatePassword ??= config.GetString("pfx-pwd");
model.UseGzip ??= config.GetBoolean("gzip");
model.UseBrotli ??= config.GetBoolean("brotli");
model.CompressionLevel ??= config.GetString("compression-level") is string compressionLevel
? Enum.Parse<CompressionLevel>(compressionLevel, ignoreCase: true)
: default;
model.EnableCors ??= config.GetBoolean("cors");
model.PathBase ??= config.GetString("path-base");
model.FallbackFile ??= config.GetString("fallback-file");
Expand Down
14 changes: 12 additions & 2 deletions src/dotnet-serve/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,23 @@ public void ConfigureServices(IServiceCollection services)
if (_options.UseGzip == true)
{
options.Providers.Add<GzipCompressionProvider>();
var gzipCompressionProvider = new GzipCompressionProvider(new GzipCompressionProviderOptions
{
Level = _options.CompressionLevel ?? System.IO.Compression.CompressionLevel.Fastest
});
options.Providers.Add(gzipCompressionProvider);
}
if (_options.UseBrotli == true)
{
options.Providers.Add<BrotliCompressionProvider>();
var brotliCompressionProvider = new BrotliCompressionProvider(new BrotliCompressionProviderOptions
{
Level = _options.CompressionLevel ?? System.IO.Compression.CompressionLevel.Fastest
});
options.Providers.Add(brotliCompressionProvider);
}
});

Expand Down

0 comments on commit fa21e93

Please sign in to comment.