Skip to content

Commit

Permalink
improved project structure, added automatic env building for database…
Browse files Browse the repository at this point in the history
… connections etc.
  • Loading branch information
stijuh committed Jun 6, 2023
1 parent 84f036a commit 9117965
Show file tree
Hide file tree
Showing 36 changed files with 154 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN dotnet publish GainsTracker.CoreAPI/GainsTracker.CoreAPI.csproj -c Release -
FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime

ENV ASPNETCORE_URLS=http://*:4040
ENV ASPNETCORE_ENVIRONMENT=Development
ENV ASPNETCORE_ENVIRONMENT=Docker

WORKDIR /app
COPY --from=publish /app/publish .
Expand Down
18 changes: 9 additions & 9 deletions GainsTracker.ClientNative/GainsTracker.ClientNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@
<MauiIcon Include="Resources\AppIcon\appicon.svg" Color="#512BD4"/>

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128"/>

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
<MauiImage Include="Resources\Images\*"/>
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208"/>

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<MauiFont Include="Resources\Fonts\*"/>

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<!-- <None Remove="Resources\AppIcon\appiconfg.svg" />-->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)"/>
<!-- <None Remove="Resources\AppIcon\appiconfg.svg" />-->
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GainsTracker.UI\GainsTracker.UI.csproj" />
<ProjectReference Include="..\GainsTracker.UI\GainsTracker.UI.csproj"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GainsTracker.Common\GainsTracker.Common.csproj" />
<ProjectReference Include="..\GainsTracker.Common\GainsTracker.Common.csproj"/>
</ItemGroup>
</Project>
20 changes: 19 additions & 1 deletion GainsTracker.ClientNative/Resources/AppIcon/appicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion GainsTracker.ClientWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GainsTracker.UI.Services;
using Microsoft.Extensions.FileProviders;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

Expand Down
4 changes: 2 additions & 2 deletions GainsTracker.Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public static class StringExtensions
{
public static bool ToBool(this string str)
{
var success = bool.TryParse(str, out bool result);
bool success = bool.TryParse(str, out bool result);

if (!success)
throw new ArgumentException($"Couldn't parse '{str}' to a boolean value.");

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Friends.Services;
using GainsTracker.CoreAPI.Components.Friends.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
using GainsTracker.CoreAPI.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GainsTracker.Common.Exceptions;
using GainsTracker.CoreAPI.Components.Friends.Models;
using GainsTracker.CoreAPI.Components.Workouts.Models;
using GainsTracker.CoreAPI.Configurations.Database;
using GainsTracker.CoreAPI.Database;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Components.Friends.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Services;
using GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
using GainsTracker.CoreAPI.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GainsTracker.CoreAPI.Components.HealthMetrics.Models;
using GainsTracker.CoreAPI.Components.Workouts.Models;
using GainsTracker.CoreAPI.Configurations.Database;
using GainsTracker.CoreAPI.Database;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Components.HealthMetrics.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public WeightMetric()
Date = DateTime.UtcNow;
}

public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime Date { get; init; }

public Goal<WeightMetric> CreateAsGoal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;
public class CreateMetricDto
{
public MetricType Type { get; set; }
public JsonDocument Data { get; set; }
public JsonDocument? Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace GainsTracker.CoreAPI.Components.HealthMetrics.Services.Dto;

public class MetricDto
{
public string Id { get; set; }
public string Id { get; set; } = string.Empty;
public MetricType Type { get; set; }
public DateTime LoggingDate { get; set; }
public JsonDocument Data { get; set; }
public JsonDocument? Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public HealthMetricService(BigBrainHealthMetric bigBrain)

public void AddMetricToGainsAccount(string userHandle, CreateMetricDto createMetricDto)
{
Metric metric = MetricFactory.DeserializeMetricFromJson(createMetricDto.Type, createMetricDto.Data);
Metric metric = MetricFactory.DeserializeMetricFromJson(createMetricDto.Type, createMetricDto.Data!);
GainsAccount gains = _bigBrain.GetGainsAccountByUsername(userHandle);
gains.AddMetric(metric);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GainsTracker.CoreAPI.Components.Workouts.Services;
using GainsTracker.CoreAPI.Configurations.Controllers;
using GainsTracker.CoreAPI.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GainsTracker.CoreAPI.Components.Workouts.Models;
using GainsTracker.CoreAPI.Components.Workouts.Services;
using GainsTracker.CoreAPI.Components.Workouts.Services.Dto;
using GainsTracker.CoreAPI.Configurations.Controllers;
using GainsTracker.CoreAPI.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GainsTracker.Common.Exceptions;
using GainsTracker.CoreAPI.Components.Workouts.Models;
using GainsTracker.CoreAPI.Components.Workouts.Models.Workouts;
using GainsTracker.CoreAPI.Configurations.Database;
using GainsTracker.CoreAPI.Database;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Components.Workouts.Data;
Expand Down
34 changes: 0 additions & 34 deletions GainsTracker.CoreAPI/Configurations/ProgramAppExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Configurations.Database;
namespace GainsTracker.CoreAPI.Database;

public sealed class AppDbContext : IdentityDbContext<User>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GainsTracker.Common.Exceptions;
using GainsTracker.CoreAPI.Components.Workouts.Models;

namespace GainsTracker.CoreAPI.Configurations.Database;
namespace GainsTracker.CoreAPI.Database;

/// <summary>
/// Oh yeah this is big brain time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Configurations.Database;
namespace GainsTracker.CoreAPI.Database;

public class DbInitializer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using GainsTracker.CoreAPI.Components.Workouts.Models.Workouts;
using Microsoft.EntityFrameworkCore;

namespace GainsTracker.CoreAPI.Configurations;
namespace GainsTracker.CoreAPI.Database;

public static class ModelBuilderExtensions
{
Expand Down
6 changes: 3 additions & 3 deletions GainsTracker.CoreAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.Json.Serialization;
using GainsTracker.Common.Extensions;
using GainsTracker.CoreAPI.Configurations;

namespace GainsTracker.CoreAPI;

Expand All @@ -20,15 +19,16 @@ public static void Main(string[] args)
builder.Services.AddEndpointsApiExplorer();

// Configuration.
builder.RegisterEpicDependencies();
builder.ConfigureContextAndIdentity();
builder.ConfigureAuthentication();
builder.AddSwaggerDocumentation();
builder.ConfigureCors();
builder.RegisterEpicDependencies();
builder.EnableDataProtection();

WebApplication app = builder.Build();

app.ResetAndUpdateDatabase(args[0].ToBool());
app.ResetAndUpdateDatabase(args.Length > 0 && args[0].ToBool());

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// God that's a boat load of usings
// Woah that's a boat load of usings

using System.Security.Claims;
using System.Text;
Expand All @@ -10,19 +10,23 @@
using GainsTracker.CoreAPI.Components.Security.Services;
using GainsTracker.CoreAPI.Components.Workouts.Data;
using GainsTracker.CoreAPI.Components.Workouts.Services;
using GainsTracker.CoreAPI.Configurations.Database;
using GainsTracker.CoreAPI.Database;
using GainsTracker.CoreAPI.Shared;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;

namespace GainsTracker.CoreAPI.Configurations;
namespace GainsTracker.CoreAPI;

public static class ProgramBuilderExtensions
public static class ProgramExtensions
{
/// <summary>
/// Register all dependencies, scoped as well as transient.
/// Register all dependencies, scoped as well as transient. Epic.
/// </summary>
public static void RegisterEpicDependencies(this WebApplicationBuilder builder)
{
Expand All @@ -42,8 +46,15 @@ public static void RegisterEpicDependencies(this WebApplicationBuilder builder)
/// </summary>
public static void ConfigureContextAndIdentity(this WebApplicationBuilder builder)
{
// Set DbContext.
builder.Services.AddDbContext<AppDbContext>(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("databaseConnection")); });
// Load the appsettings.json file
builder.Configuration.AddJsonFile("appsettings.json", true, true);

string? connection = builder.Configuration
.GetSection($"ConnectionStrings:{builder.Environment.EnvironmentName}")
.Value;
builder.Configuration.GetSection("ConnectionStrings:connection").Value = connection;

builder.Services.AddDbContext<AppDbContext>(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("connection")); });

// Map Identity to User and the database.
builder.Services.AddIdentity<User, IdentityRole>()
Expand Down Expand Up @@ -120,6 +131,16 @@ public static void ConfigureAuthentication(this WebApplicationBuilder builder)
});
}

public static void EnableDataProtection(this WebApplicationBuilder builder)
{
builder.Services.AddDataProtection().UseCryptographicAlgorithms(
new AuthenticatedEncryptorConfiguration
{
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
});
}

/// <summary>
/// Swagger documentation with authentication.
/// </summary>
Expand Down Expand Up @@ -158,4 +179,30 @@ public static void AddSwaggerDocumentation(this WebApplicationBuilder builder)
});
});
}

/// <summary>
/// Useful for debugging.
/// </summary>
/// <param name="execute">Flag to execute this function or not.</param>
public static void ResetAndUpdateDatabase(this WebApplication app, bool execute = true)
{
if (!execute)
return;

using IServiceScope scope = app.Services.CreateScope();
AppDbContext db = scope.ServiceProvider.GetRequiredService<AppDbContext>();

Console.WriteLine("Resetting database..");
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Database.Migrate();
}

/// <summary>
/// Configure global exception handling.
/// </summary>
public static void AddGlobalErrorHandler(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.UseMiddleware<GlobalErrorHandlingMiddleware>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using GainsTracker.Common;
using Microsoft.AspNetCore.Mvc;

namespace GainsTracker.CoreAPI.Configurations.Controllers;
namespace GainsTracker.CoreAPI.Shared;

/// <summary>
/// Extended controller class to add commonly used controller functionality.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json;
using GainsTracker.Common.Exceptions;

namespace GainsTracker.CoreAPI.Configurations.Exceptions;
namespace GainsTracker.CoreAPI.Shared;

public class GlobalErrorHandlingMiddleware
{
Expand Down
4 changes: 2 additions & 2 deletions GainsTracker.CoreAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
}
},
"ConnectionStrings": {
"databaseConnection": "Host=localhost:15432; Database=gainstracker_db; Username=stoy; Password=postman",
"databaseConnectionDocker": "Host=db:5432; Database=gainstracker_db; Username=stoy; Password=postman"
"Development": "Host=localhost:15432; Database=gainstracker_db; Username=stoy; Password=postman",
"Docker": "Host=db:5432; Database=gainstracker_db; Username=stoy; Password=postman"
},
"JWT": {
"ValidAudience": "http://localhost:4200",
Expand Down
9 changes: 0 additions & 9 deletions GainsTracker.Testing.ClientNative/BuildTest.cs

This file was deleted.

Loading

0 comments on commit 9117965

Please sign in to comment.