Skip to content

Commit

Permalink
Merge pull request #5 from bastare/feature/refactor
Browse files Browse the repository at this point in the history
Feature/refactor
  • Loading branch information
bastare authored Jul 7, 2024
2 parents f1ce0ee + 2cdabeb commit f26abcf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace TapeCat.Template.Api.Common.Extensions;

using Pipes.SecurityPipes;

public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseSecureHeaders ( this IApplicationBuilder applicationBuilder )
=> applicationBuilder
.UseHttpsRedirection ()
.UseHsts ( hsts =>
{
hsts.MaxAge ( days: 365 ).IncludeSubdomains ();
} )
.UseXContentTypeOptions ()
.UsePermissionsPolicy (
siteUrl => [
$"fullscreen=(self {siteUrl})",
$"geolocation=(self {siteUrl})",
$"payment=(self {siteUrl})",
"camera=()",
"microphone=()",
"usb=()"
]
)
.UseXfo ( xfo =>
{
xfo.SameOrigin ();
} )
.UseReferrerPolicy ( options =>
{
options.NoReferrer ();
} )
.UseXXssProtection ( options =>
{
options.EnabledWithBlockMode ();
} )
.UseCsp ( options =>
{
options
.StyleSources ( configure =>
{
configure
.Self ()
.CustomSources (
"www.google.com" ,
"platform.twitter.com" ,
"cdn.syndication.twimg.com" ,
"fonts.googleapis.com"
)
.UnsafeInline ();
} )
.ScriptSources ( configure =>
{
configure
.Self ()
.CustomSources (
"www.google.com" ,
"cse.google.com" ,
"cdn.syndication.twimg.com" ,
"platform.twitter.com" ,
"https://www.google-analytics.com" ,
"https://connect.facebook.net" ,
"https://www.youtube.com"
)
.UnsafeInline ()
.UnsafeEval ();
} );
} );
}
44 changes: 22 additions & 22 deletions src/server/TapeCat.Template.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@
using Autofac.Extensions.DependencyInjection;
using TapeCat.Template.Api;

var builder = WebApplication.CreateBuilder (
var builder_ = WebApplication.CreateBuilder (
options: new ()
{
Args = args ,
WebRootPath = "webroot"
} );

var startup = new Startup ( builder.Configuration , builder.Environment );
var startup_ = new Startup ( builder_.Configuration , builder_.Environment );

builder.Host
builder_.Host
.UseServiceProviderFactory ( new AutofacServiceProviderFactory () )
.ConfigureContainer<ContainerBuilder> ( startup.ConfigureContainer )
.ConfigureContainer<ContainerBuilder> ( startup_.ConfigureContainer )
.ConfigureAppConfiguration ( ( hostBuilderContext , config ) =>
{
config
.AddJsonFile (
path: "./appsettings.json" ,
optional: false ,
reloadOnChange: true )
.AddJsonFile (
path: $"./appsettings.{hostBuilderContext.HostingEnvironment.EnvironmentName}.json" ,
optional: true ,
reloadOnChange: true )
.AddEnvironmentVariables ();
} );
{
config
.AddJsonFile (
path: "./appsettings.json" ,
optional: false ,
reloadOnChange: true )
.AddJsonFile (
path: $"./appsettings.{hostBuilderContext.HostingEnvironment.EnvironmentName}.json" ,
optional: true ,
reloadOnChange: true )
.AddEnvironmentVariables ();
} );

startup.ConfigureServices ( builder.Services );
startup_.ConfigureServices ( builder_.Services );

var webApplication = builder.Build ();
var webApplication_ = builder_.Build ();

startup.Configure ( webApplication );
startup_.Configure ( webApplication_ );

await webApplication.RunAsync ();
await webApplication_.RunAsync ();
81 changes: 4 additions & 77 deletions src/server/TapeCat.Template.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace TapeCat.Template.Api;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pipes.SecurityPipes;
using Common.Extensions;

public sealed class Startup ( IConfiguration configuration , IWebHostEnvironment webHostEnvironment )
Expand All @@ -36,14 +35,8 @@ public void ConfigureServices ( IServiceCollection serviceCollection )
} );

serviceCollection
.Configure<BrotliCompressionProviderOptions> ( options =>
{
options.Level = CompressionLevel.Fastest;
} )
.Configure<ApiBehaviorOptions> ( options =>
{
options.SuppressModelStateInvalidFilter = true;
} )
.Configure<BrotliCompressionProviderOptions> ( options => options.Level = CompressionLevel.Fastest )
.Configure<ApiBehaviorOptions> ( options => options.SuppressModelStateInvalidFilter = true )
.AddResponseCompression ( options =>
{
options.EnableForHttps = true;
Expand Down Expand Up @@ -88,81 +81,15 @@ public void ConfigureContainer ( ContainerBuilder containerBuilder )
public void Configure ( IApplicationBuilder applicationBuilder )
{
if ( WebHostEnvironmentExtensions.IsProduction ( _webHostEnvironment ) )
{
applicationBuilder
.UseHttpsRedirection ()
.UseHsts ( hsts =>
{
hsts.MaxAge ( days: 365 ).IncludeSubdomains ();
} )
.UseXContentTypeOptions ()
.UsePermissionsPolicy (
siteUrl => [
$"fullscreen=(self {siteUrl})",
$"geolocation=(self {siteUrl})",
$"payment=(self {siteUrl})",
"camera=()",
"microphone=()",
"usb=()"
]
)
.UseXfo ( xfo =>
{
xfo.SameOrigin ();
} )
.UseReferrerPolicy ( options =>
{
options.NoReferrer ();
} )
.UseXXssProtection ( options =>
{
options.EnabledWithBlockMode ();
} )
.UseCsp ( options =>
{
options
.StyleSources ( configure =>
{
configure
.Self ()
.CustomSources (
"www.google.com" ,
"platform.twitter.com" ,
"cdn.syndication.twimg.com" ,
"fonts.googleapis.com"
)
.UnsafeInline ();
} )
.ScriptSources ( configure =>
{
configure
.Self ()
.CustomSources (
"www.google.com" ,
"cse.google.com" ,
"cdn.syndication.twimg.com" ,
"platform.twitter.com" ,
"https://www.google-analytics.com" ,
"https://connect.facebook.net" ,
"https://www.youtube.com"
)
.UnsafeInline ()
.UnsafeEval ();
} );
} );
}
applicationBuilder.UseSecureHeaders ();

if ( WebHostEnvironmentExtensions.IsDevelopment ( _webHostEnvironment ) )
{
applicationBuilder
.UseCors ( builder =>
{
builder
.AllowAnyOrigin ()
.AllowAnyHeader ()
.AllowAnyMethod ();
} );
}
.AllowAnyMethod () );

applicationBuilder
.UseResponseCompression ()
Expand Down

0 comments on commit f26abcf

Please sign in to comment.