Skip to content

Commit

Permalink
Added Postgres support with environment var configuration (#29)
Browse files Browse the repository at this point in the history
Added PostgreSQL support
Removed MySQL support
Added development DB connection details to appsetings.Development.json and docker-compose.yml

Fix #28
  • Loading branch information
Hutch79 authored Aug 16, 2024
1 parent d423322 commit a3339d6
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 226 deletions.
1 change: 1 addition & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions API/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using Microsoft.AspNetCore.Mvc;

namespace Your_Lab.Controllers;
Expand Down Expand Up @@ -29,4 +30,9 @@ public IEnumerable<WeatherForecast> Get()
})
.ToArray();
}

public IDictionary Test()
{
return Environment.GetEnvironmentVariables();
}
}
22 changes: 16 additions & 6 deletions API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
builder.Services.AddDbContext<YourLabDbContext>(context =>
{
context.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
var connectionString = builder.Configuration.GetConnectionString("SqlConnectionString");
context.UseSqlServer(connectionString);
});
string connectionString = String.Empty;

var dbHost = builder.Configuration.GetValue<string>("YOUR_LAB:DB:HOST");
var dbPort = builder.Configuration.GetValue<int>("YOUR_LAB:DB:PORT");
var dbDatabase = builder.Configuration.GetValue<string>("YOUR_LAB:DB:DATABASE");
var dbUser = builder.Configuration.GetValue<string>("YOUR_LAB:DB:USER");
var dbPassword = builder.Configuration.GetValue<string>("YOUR_LAB:DB:PASSWORD");

connectionString = $"Host={dbHost}:{dbPort}; Database={dbDatabase}; Username={dbUser}; Password={dbPassword}";
Console.WriteLine(connectionString);

context.UseNpgsql(connectionString);
});

builder.Services.AddRateLimiter(_ => _ // Rate limiting to 100 requests per minute
// ToDo: Rate limit options as env variables
builder.Services.AddRateLimiter(_ => _ // Rate limiting to 100 requests per minute
.AddFixedWindowLimiter(policyName: "fixed", options =>
{
options.PermitLimit = 25;
Expand All @@ -43,12 +53,12 @@
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseRateLimiter();
app.UseHttpsRedirection();

// app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
app.Run();
26 changes: 0 additions & 26 deletions API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:48502",
"sslPort": 44332
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5209",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
Expand All @@ -28,14 +10,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
10 changes: 8 additions & 2 deletions API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"ConnectionStrings": {
"SqlConnectionString": "Server=(localdb)\\mssqllocaldb;Database=yourlab-dev-db;Trusted_Connection=True;"
"YOUR_LAB": {
"DB": {
"HOST": "localhost",
"PORT": "5432",
"DATABASE": "your-labs-db",
"USER": "user",
"PASSWORD": "password"
}
},
"Logging": {
"LogLevel": {
Expand Down
2 changes: 1 addition & 1 deletion Domain/Domains.cs → Domain/DbObjects/Domains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class Domains
public int Id { get; set; }
public required string Domain { get; set; }
public required bool Active { get; set; }
}
}
3 changes: 1 addition & 2 deletions Domain/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ public class User
public int Id { get; set; }
public required string Email { get; set; }
public required string Username { get; set; }

}
}
1 change: 1 addition & 0 deletions Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

76 changes: 0 additions & 76 deletions Infrastructure/Migrations/20240703191648_initial-migration.cs

This file was deleted.

Loading

0 comments on commit a3339d6

Please sign in to comment.