BRAKING refactor project structure.
Refactor: the project is now divided into a more clear structure, with **Infrastructure** and **Application** layers added. Refactor: configurations are split into sections for different layers. Fix: now EF Core related operations, such as migration, should be invoked in `OptixServe.Infrastructure`, with config file and data dir passed into `dotnet ef` command. See `OptixServe.Infrastructure/Utilites/DesignTimeDbContextFactory.cs` for details. Fix: EF migrations are ignored in gitignore on purpose in early development.
This commit is contained in:
@ -1,16 +1,15 @@
|
||||
namespace OptixServe.Api.Configuration;
|
||||
|
||||
public record OptixServeSettings
|
||||
public record ApiConfiguration
|
||||
{
|
||||
public ApiSettings? Api { get; set; } = new();
|
||||
public DatabaseSettings? Database { get; set; } = new();
|
||||
public JwtSettings? Jwt { get; set; } = new();
|
||||
}
|
||||
|
||||
public record ApiSettings
|
||||
{
|
||||
public string? Listen { get; set; } = "127.0.0.1";
|
||||
public int? Port { get; set; } = 10086;
|
||||
public string? Listen { get; set; }
|
||||
public int? Port { get; set; }
|
||||
}
|
||||
|
||||
public record JwtSettings
|
||||
@ -20,15 +19,3 @@ public record JwtSettings
|
||||
public string Audience { get; set; } = "OptixServeUsers";
|
||||
public int TokenExpirationMinutes { get; set; } = 60;
|
||||
}
|
||||
|
||||
public enum DatabaseType
|
||||
{
|
||||
Sqlite,
|
||||
MySQL
|
||||
}
|
||||
|
||||
public record DatabaseSettings
|
||||
{
|
||||
public DatabaseType Type { get; set; } = DatabaseType.Sqlite;
|
||||
public string? Host { get; set; }
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace OptixServe.Api.Configuration;
|
||||
|
||||
public static class ConfigurationHelper
|
||||
{
|
||||
public static IConfigurationBuilder CreateDefaultBuilder()
|
||||
{
|
||||
var aspEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
var netEnv = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
|
||||
// Console.WriteLine($"ASPNETCORE_ENVIRONMENT: {aspEnv}, DOTNET_ENVIRONMENT: {netEnv}");
|
||||
var env = aspEnv ?? netEnv ?? null;
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.AddJsonFile("config.json", optional: true);
|
||||
|
||||
if (env != null)
|
||||
{
|
||||
builder.AddJsonFile($"appsettings.{env}.json", optional: true)
|
||||
.AddJsonFile($"config.{env}.json", optional: true);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using OptixServe.Core.Services;
|
||||
using OptixServe.Application.Services;
|
||||
using OptixServe.Api.Dtos;
|
||||
using OptixServe.Api.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace OptixServe.Api.Endpoints;
|
||||
|
||||
@ -10,7 +9,7 @@ namespace OptixServe.Api.Endpoints;
|
||||
[JsonSerializable(typeof(UserDto))]
|
||||
[JsonSerializable(typeof(IEnumerable<UserDto>))]
|
||||
[JsonSerializable(typeof(LoginRequestDto))]
|
||||
[JsonSerializable(typeof(LoginResponseDto))] // For returning the token string
|
||||
[JsonSerializable(typeof(LoginResponseDto))]
|
||||
public partial class UserJsonContext : JsonSerializerContext { }
|
||||
|
||||
public static class UserEndpoint
|
||||
|
@ -1,6 +1,6 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Infrastructure.Configuration;
|
||||
using OptixServe.Api.Dtos;
|
||||
|
||||
namespace OptixServe.Api.Endpoints;
|
||||
@ -22,14 +22,14 @@ public static class VersionEndpoint
|
||||
var group = parentGroup.MapGroup("/version");
|
||||
|
||||
group.MapGet("/", () => "v1");
|
||||
group.MapGet("/test/dbconfig", (IOptions<OptixServeSettings> appSettings) =>
|
||||
group.MapGet("/test/dbconfig", (IOptions<InfrastructureConfiguration> infrastructureConfig) =>
|
||||
{
|
||||
var dbType = appSettings.Value.Database?.Type;
|
||||
var dbHost = appSettings.Value.Database?.Host;
|
||||
var dbType = infrastructureConfig.Value.Database?.Type;
|
||||
var dbHost = infrastructureConfig.Value.Database?.Host;
|
||||
return Results.Ok(new CommonErrorDto
|
||||
{
|
||||
Message = $"Set up {dbType} database on {dbHost}"
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OptixServe.Core\OptixServe.Core.csproj" />
|
||||
<ProjectReference Include="..\OptixServe.Application\OptixServe.Application.csproj" />
|
||||
<ProjectReference Include="..\OptixServe.Infrastructure\OptixServe.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.6" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25306.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
@ -5,9 +5,10 @@ using Microsoft.IdentityModel.Tokens;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Api.Endpoints;
|
||||
using OptixServe.Api.Services;
|
||||
using OptixServe.Core.Data;
|
||||
using OptixServe.Core.Services;
|
||||
using OptixServe.Api.Utilites;
|
||||
using OptixServe.Application.Services;
|
||||
using OptixServe.Infrastructure.Configuration;
|
||||
using OptixServe.Infrastructure.Data;
|
||||
using OptixServe.Infrastructure.Utilites;
|
||||
|
||||
class Program
|
||||
{
|
||||
@ -89,12 +90,7 @@ static class StartupHelper
|
||||
public static void AddConfigurationWithCommand(this WebApplicationBuilder builder, FileInfo? file)
|
||||
{
|
||||
// Configure configuration sources in specified order
|
||||
var configurationBuilder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("config.json", optional: true)
|
||||
.AddJsonFile($"config.{builder.Environment.EnvironmentName}.json", optional: true);
|
||||
var configurationBuilder = ConfigurationHelper.CreateDefaultBuilder();
|
||||
|
||||
// Add command-line specified config file if provided
|
||||
if (file != null)
|
||||
@ -123,13 +119,17 @@ static class StartupHelper
|
||||
/// <param name="builder">WebApplicationBuilder instance</param>
|
||||
public static void RegisterServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
// Add configuration class
|
||||
var optixSettigns = builder.Configuration.GetSection("OptixServe");
|
||||
var onConfigSettings = optixSettigns.Get<OptixServeSettings>();
|
||||
builder.Services.Configure<OptixServeSettings>(optixSettigns);
|
||||
// Add configuration classes
|
||||
var apiConfigSection = builder.Configuration.GetSection("OptixServe:Api");
|
||||
builder.Services.Configure<ApiConfiguration>(apiConfigSection);
|
||||
var apiConfig = apiConfigSection.Get<ApiConfiguration>();
|
||||
|
||||
var infraConfigSection = builder.Configuration.GetSection("OptixServe:Infrastructure");
|
||||
builder.Services.Configure<InfrastructureConfiguration>(infraConfigSection);
|
||||
var infraConfig = infraConfigSection.Get<InfrastructureConfiguration>();
|
||||
|
||||
// Add DBContext class
|
||||
builder.Services.AddAppDatabase(onConfigSettings?.Database!);
|
||||
builder.Services.AddAppDatabase(infraConfig?.Database!);
|
||||
builder.Services.AddScoped<DbInitializer>();
|
||||
|
||||
// Application services
|
||||
@ -140,7 +140,7 @@ static class StartupHelper
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
var jwtSettings = onConfigSettings?.Jwt ?? throw new ArgumentNullException(nameof(builder), "JWT settings are not configured.");
|
||||
var jwtSettings = apiConfig?.Jwt ?? throw new ArgumentNullException(nameof(builder), "JWT settings are not configured.");
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
@ -178,4 +178,4 @@ static class StartupHelper
|
||||
VersionEndpoint.Register(rootGroup);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,9 +13,9 @@ public interface ITokenService
|
||||
public string GenerateToken(User user);
|
||||
}
|
||||
|
||||
public class TokenService(IOptions<OptixServeSettings> optixServeSettings) : ITokenService
|
||||
public class TokenService(IOptions<ApiConfiguration> apiConfiguration) : ITokenService
|
||||
{
|
||||
private readonly JwtSettings _jwtSettings = optixServeSettings.Value.Jwt ?? throw new ArgumentNullException(nameof(optixServeSettings), "JWT settings are not configured.");
|
||||
private readonly JwtSettings _jwtSettings = apiConfiguration.Value.Jwt ?? throw new ArgumentNullException(nameof(apiConfiguration), "JWT settings are not configured.");
|
||||
|
||||
public string GenerateToken(User user)
|
||||
{
|
||||
@ -41,4 +41,4 @@ public class TokenService(IOptions<OptixServeSettings> optixServeSettings) : ITo
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Core.Data;
|
||||
|
||||
namespace OptixServe.Api.Utilites;
|
||||
|
||||
public static class DatabaseHelper
|
||||
{
|
||||
public static string BuildConnectionString(DatabaseSettings dbSettings)
|
||||
{
|
||||
return dbSettings.Type switch
|
||||
{
|
||||
DatabaseType.Sqlite => $"Data Source={dbSettings.Host ?? "optixserve.db"}",
|
||||
DatabaseType.MySQL => throw new NotSupportedException("MySQL connection is not yet implemented"),
|
||||
_ => throw new NotSupportedException($"Database type {dbSettings.Type} is not supported")
|
||||
};
|
||||
}
|
||||
|
||||
public static void ConfigureDbContext(DbContextOptionsBuilder options, DatabaseSettings dbSettings)
|
||||
{
|
||||
if (dbSettings?.Type == DatabaseType.Sqlite)
|
||||
{
|
||||
var dbPath = dbSettings.Host ?? "optixserve.db";
|
||||
var connectionString = $"Data Source={dbPath}";
|
||||
|
||||
options.UseSqlite(connectionString, b => b.MigrationsAssembly("OptixServe.Api"));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("Only SQLite database is currently supported");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Core.Data;
|
||||
|
||||
namespace OptixServe.Api.Utilites;
|
||||
|
||||
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
|
||||
{
|
||||
public AppDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var configuration = ConfigurationHelper.CreateDefaultBuilder().Build();
|
||||
|
||||
var dbSettings = configuration.GetSection("OptixServe:Database").Get<DatabaseSettings>()!;
|
||||
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
|
||||
DatabaseHelper.ConfigureDbContext(optionsBuilder, dbSettings);
|
||||
|
||||
return new AppDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
@ -9,17 +9,19 @@
|
||||
"OptixServe": {
|
||||
"Api": {
|
||||
"Listen": "0.0.0.0",
|
||||
"Port": "54321"
|
||||
"Port": "54321",
|
||||
"Jwt": {
|
||||
"Secret": "YOUR_SECRET_KEY_HERE_DO_NOT_SHARE_THIS_AND_MAKE_IT_LONG_ENOUGH",
|
||||
"Issuer": "OptixServe",
|
||||
"Audience": "OptixServeUsers",
|
||||
"TokenExpirationMinutes": 60
|
||||
}
|
||||
},
|
||||
"Database": {
|
||||
"Type": "Sqlite",
|
||||
"Host": "optixserve.db"
|
||||
},
|
||||
"Jwt": {
|
||||
"Secret": "YOUR_SECRET_KEY_HERE_DO_NOT_SHARE_THIS_AND_MAKE_IT_LONG_ENOUGH",
|
||||
"Issuer": "OptixServe",
|
||||
"Audience": "OptixServeUsers",
|
||||
"TokenExpirationMinutes": 60
|
||||
"Infrastructure": {
|
||||
"Database": {
|
||||
"Type": "Sqlite",
|
||||
"Host": "optixserve.db"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user