Skip to content

Commit

Permalink
add: environment variables instead of appsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
nixhantb committed Jan 22, 2025
1 parent 8d42ca3 commit 864bb8e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
3 changes: 0 additions & 3 deletions .env-example

This file was deleted.

8 changes: 8 additions & 0 deletions Server/JobLeet.WebApi/.env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
JWT_KEY=<Value>
DB_HOST=<Value>
DB_PORT=<Value>
DB_NAME=<Value>
DB_USER=<Value>
DB_PASSWORD=<Value>
Issuer=<Value>'http://localhost:5184/'
Audience=<Value>'http://localhost:5184/'
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task<IActionResult> Logout()

private string GenerateJwtToken(IdentityUser user)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT_KEY"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var claims = new[]
Expand All @@ -107,8 +107,8 @@ private string GenerateJwtToken(IdentityUser user)
};

var token = new JwtSecurityToken(
issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],
issuer: _configuration["Issuer"],
audience: _configuration["Audience"],
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds
Expand Down
1 change: 1 addition & 0 deletions Server/JobLeet.WebApi/JobLeet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="8.2.2" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.4" />
Expand Down
33 changes: 21 additions & 12 deletions Server/JobLeet.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder
.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);
DotNetEnv.Env.Load();
builder.Configuration.AddEnvironmentVariables();

#region Register the repository services
// Add the required Configurations
Expand All @@ -58,7 +56,6 @@
builder.Services.AddScoped<IValidator<Skill>, SkillValidator>();

builder.Services.AddScoped<IPersonNameRepository, PersonNameRepository>();
builder.Services.AddScoped<ISkillService, SkillService>();
builder.Services.AddScoped<IValidator<PersonName>, PersonNameValidator>();

builder.Services.AddScoped<IExperienceRepository, ExperienceRepository>();
Expand Down Expand Up @@ -118,7 +115,8 @@

// Register BaseCacheHelper<T> for caching
builder.Services.AddScoped(typeof(BaseCacheHelper<>));
var key = builder.Configuration["Jwt:Key"];
var key = Environment.GetEnvironmentVariable("JWT_KEY");

if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("Jwt:Key cannot be null or empty");
Expand All @@ -138,8 +136,8 @@
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidIssuer = builder.Configuration["Issuer"],
ValidAudience = builder.Configuration["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)),
};
});
Expand All @@ -166,11 +164,22 @@

#region Database configuration Services
builder.Services.AddDbContext<BaseDBContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("jobleetconnect"));
});
options.UseNpgsql(
$"Host={Environment.GetEnvironmentVariable("DB_HOST")};"
+ $"Database={Environment.GetEnvironmentVariable("DB_NAME")};"
+ $"Username={Environment.GetEnvironmentVariable("DB_USER")};"
+ $"Password={Environment.GetEnvironmentVariable("DB_PASSWORD")};"
+ $"Port={Environment.GetEnvironmentVariable("DB_PORT")}"
)
);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("jobleetconnect"))
options.UseNpgsql(
$"Host={Environment.GetEnvironmentVariable("DB_HOST")};"
+ $"Database={Environment.GetEnvironmentVariable("DB_NAME")};"
+ $"Username={Environment.GetEnvironmentVariable("DB_USER")};"
+ $"Password={Environment.GetEnvironmentVariable("DB_PASSWORD")};"
+ $"Port={Environment.GetEnvironmentVariable("DB_PORT")}"
)
);

#endregion
Expand Down
10 changes: 1 addition & 9 deletions Server/JobLeet.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"NLog": {
"LogLevel": {
"Default": "Info"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"jobleetconnect": "Host=<Host>;Database=<Db>;Username=<name>;Password=<pw>;Port=5432"
},

"SecurityHeaders": {
"ContentSecurityPolicy": "default-src 'self'; script-src 'self' 'unsafe-inline'",
"XContentTypeOptions": "nosniff",
Expand Down

0 comments on commit 864bb8e

Please sign in to comment.