Skip to content

Commit 2fb16ac

Browse files
committed
fix: environment specific settings separated as mvp- and non-mvps
1 parent 534b1b0 commit 2fb16ac

2 files changed

Lines changed: 60 additions & 23 deletions

File tree

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Helpers/Extensions/EnvironmentExtensions.cs

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public static class Environments
66
public static readonly string Development = "dev";
77
public static readonly string Staging = "staging";
88
public static readonly string Production = "production";
9+
public static readonly string MvpDevelopment = "mvp-dev"; // Not in use but should be
910
public static readonly string MvpStaging = "mvp-staging";
1011
public static readonly string MvpProduction = "mvp-production";
1112
}
@@ -14,41 +15,73 @@ public static class EnvironmentExtensions
1415
{
1516
public static bool IsLocal(this IHostEnvironment hostEnvironment)
1617
{
17-
if (hostEnvironment == null)
18-
{
19-
throw new ArgumentNullException(nameof(hostEnvironment));
20-
}
21-
22-
return hostEnvironment.IsEnvironment(Environments.Local);
18+
return IsEnvironment(hostEnvironment, Environments.Local);
2319
}
2420

21+
//
22+
// Non-MVP app environments
23+
//
2524
public static bool IsDevelopment(this IHostEnvironment hostEnvironment)
2625
{
27-
if (hostEnvironment == null)
28-
{
29-
throw new ArgumentNullException(nameof(hostEnvironment));
30-
}
31-
32-
return hostEnvironment.IsEnvironment(Environments.Development);
26+
return IsEnvironment(hostEnvironment, Environments.Development);
3327
}
3428

3529
public static bool IsStaging(this IHostEnvironment hostEnvironment)
3630
{
37-
if (hostEnvironment == null)
38-
{
39-
throw new ArgumentException(null, nameof(hostEnvironment));
40-
}
41-
42-
return hostEnvironment.IsEnvironment(Environments.MvpStaging) || hostEnvironment.IsEnvironment(Environments.Staging);
31+
return IsEnvironment(hostEnvironment, Environments.Staging);
4332
}
4433

4534
public static bool IsProduction(this IHostEnvironment hostEnvironment)
35+
{
36+
return IsEnvironment(hostEnvironment, Environments.Production);
37+
}
38+
39+
// Production-like environments are staging and production
40+
public static bool IsProductionlike(this IHostEnvironment hostEnvironment)
41+
{
42+
return IsProduction(hostEnvironment) || IsStaging(hostEnvironment);
43+
}
44+
45+
//
46+
// MVP app environments
47+
//
48+
public static bool IsMvpDevelopment(this IHostEnvironment hostEnvironment)
49+
{
50+
return IsEnvironment(hostEnvironment, Environments.MvpDevelopment);
51+
}
52+
53+
public static bool IsMvpStaging(this IHostEnvironment hostEnvironment)
54+
{
55+
return IsEnvironment(hostEnvironment, Environments.MvpStaging);
56+
}
57+
58+
public static bool IsMvpProduction(this IHostEnvironment hostEnvironment)
59+
{
60+
return IsEnvironment(hostEnvironment, Environments.MvpProduction);
61+
}
62+
63+
// MVP production-like environments are MVP staging and MVP production
64+
65+
public static bool IsMvpProductionlike(this IHostEnvironment hostEnvironment)
66+
{
67+
return IsMvpProduction(hostEnvironment) || IsMvpStaging(hostEnvironment);
68+
}
69+
70+
//
71+
// Helper methods
72+
//
73+
public static bool IsMvpEnvironment(this IHostEnvironment hostEnvironment)
74+
{
75+
return IsMvpProduction(hostEnvironment) || IsMvpStaging(hostEnvironment) || IsMvpDevelopment(hostEnvironment);
76+
}
77+
78+
public static bool IsEnvironment(IHostEnvironment hostEnvironment, string environmentName)
4679
{
4780
if (hostEnvironment == null)
4881
{
49-
throw new ArgumentException(null, nameof(hostEnvironment));
82+
throw new ArgumentNullException(nameof(hostEnvironment));
5083
}
5184

52-
return hostEnvironment.IsEnvironment(Environments.MvpProduction) || hostEnvironment.IsEnvironment(Environments.Production);
85+
return hostEnvironment.IsEnvironment(environmentName);
5386
}
5487
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,23 @@
167167
//
168168
var app = builder.Build();
169169

170-
// Use swagger only in development
171-
if (EnvironmentExtensions.IsLocal(app.Environment) || EnvironmentExtensions.IsDevelopment(app.Environment))
170+
// Use swagger only in non-productionlike environments: (staging, production)-like
171+
if (!app.Environment.IsMvpProductionlike() && !app.Environment.IsProductionlike())
172172
{
173173
app.UseSwagger();
174174
app.UseSwaggerUI();
175+
}
175176

176-
// Direct cors requests used in dev-stages
177+
// Allow CORS only in non-mvp environments
178+
if (!app.Environment.IsMvpEnvironment())
179+
{
177180
app.UseCors(builder => builder
178181
.AllowAnyOrigin()
179182
.AllowAnyMethod()
180183
.AllowAnyHeader());
181184
}
182185

186+
183187
app.UseSerilogRequestLogging(options =>
184188
{
185189
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>

0 commit comments

Comments
 (0)