This commit is contained in:
CodeLiturgy 2022-08-19 17:47:35 +01:00
parent 77a5c56e84
commit 3a66d08ddc
26 changed files with 315 additions and 40 deletions

View File

@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BlueWest\BlueWest.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["BlueWest.Api.Gateway/BlueWest.Api.Gateway.csproj", "BlueWest.Api.Gateway/"]
RUN dotnet restore "BlueWest.Api.Gateway/BlueWest.Api.Gateway.csproj"
COPY . .
WORKDIR "/src/BlueWest.Api.Gateway"
RUN dotnet build "BlueWest.Api.Gateway.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BlueWest.Api.Gateway.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlueWest.Api.Gateway.dll"]

View File

@ -0,0 +1,11 @@
// See https://aka.ms/new-console-template for more information
using BlueWest.Core;
using BlueWest.Tools;
EventManager EventManager = new EventManager(new Dictionary<Type, List<EventListenerBase>>(10000));
ThreadServer _threadServer;
System.Threading.Thread.Sleep(2500);
_threadServer = new ThreadServer(EventManager);
_threadServer.Init();

View File

@ -12,6 +12,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
@ -23,4 +25,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Startup" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,62 @@
using System;
using System.Threading.Tasks;
using BlueWest.Tools;
using BlueWest.WebApi.MySQL;
namespace BlueWest.WebApi.Interfaces
{
public struct ExchangeEvent { }
public class ExchangeInterface : EventListener<ExchangeEvent>, IDisposable, IAsyncDisposable
{
private readonly EventManager _eventManager;
private readonly CountryDbContext _countryDbContext;
private readonly FinanceDbContext _financeDbContext;
private readonly UserDbContext _userDbContext;
public ExchangeInterface(
CountryDbContext countryDbContext,
FinanceDbContext financeDbContext,
UserDbContext userDbContext,
EventManager eventManager)
{
_countryDbContext = countryDbContext;
_financeDbContext = financeDbContext;
_userDbContext = userDbContext;
_eventManager = eventManager;
Init();
}
public ExchangeInterface() { }
private void Init()
{
_eventManager.EventStartListening<ExchangeEvent>(this);
_eventManager.TriggerEvent<ExchangeEvent>(new ExchangeEvent());
Console.WriteLine($"{nameof(ExchangeInterface)} Just started!");
}
public void OnEvent(ExchangeEvent eventType)
{
Console.WriteLine($"Service received exchange {nameof(ExchangeEvent)}");
}
/// <summary>
/// Stop Listening for events
/// </summary>
public void Dispose()
{
_eventManager.EventStopListening<ExchangeEvent>(this);
}
public ValueTask DisposeAsync()
{
_eventManager.EventStopListening<ExchangeEvent>(this);
return ValueTask.CompletedTask;
}
}
}

View File

@ -35,19 +35,18 @@ namespace BlueWest.WebApi.MySQL
} }
private static void UserModel(ModelBuilder modelBuilder) private static ModelBuilder UserModel(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<Country>(builder =>
builder
.HasMany<User>()
.WithOne(user => user.Country));
modelBuilder modelBuilder
.Entity<User>(builder => builder .Entity<User>(builder => builder
.HasOne<Country>() .HasOne<Country>()
.WithMany(co => co.Users)); .WithMany(co => co.Users));
modelBuilder.Entity<Country>(builder => builder
.HasMany<User>()
.WithOne(co => co.Country)
.HasForeignKey(x => x.CountryId));
modelBuilder.Entity<FinanceOp>(builder => modelBuilder.Entity<FinanceOp>(builder =>
@ -56,6 +55,8 @@ namespace BlueWest.WebApi.MySQL
.WithMany(x => x.FinanceTransactions) .WithMany(x => x.FinanceTransactions)
.HasForeignKey(x => x.UserId); .HasForeignKey(x => x.UserId);
}); });
return modelBuilder;
} }
} }
} }

View File

@ -70,7 +70,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost] [HttpPost]
public ActionResult AddUser(UserCreate userCreate) public ActionResult AddUser(UserCreate userCreate)
{ {
var user = new User(userCreate, 0, new List<FinanceOp>(), null); var user = new User(userCreate, 0, new List<FinanceOp>(), null, -1);
_dbContext.Users.Add(user); _dbContext.Users.Add(user);
_dbContext.SaveChanges(); _dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user); return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);

View File

@ -0,0 +1,6 @@
namespace BlueWest.Data;
public class AuthManager
{
}

View File

@ -0,0 +1,9 @@
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Identity;
namespace BlueWest.WebApi;
public class EntityDbContext : IdentityUser
{
}

View File

@ -0,0 +1,10 @@
using System.Security.Principal;
namespace BlueWest.Data;
public class IdentityContext : IIdentity
{
public string AuthenticationType { get; }
public bool IsAuthenticated { get; }
public string Name { get; }
}

View File

@ -9,13 +9,13 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BlueWest.Core; using BlueWest.Core;
using BlueWest.Tools; using BlueWest.Tools;
using BlueWest.WebApi.Interfaces;
namespace BlueWest.WebApi namespace BlueWest.WebApi
{ {
public class Program public class Program
{ {
public static readonly EventManager EventManager = public static EventManager EventManager { get; set; }
new EventManager(new Dictionary<Type, List<EventListenerBase>>(10000));
private static ThreadServer _threadServer; private static ThreadServer _threadServer;
@ -39,11 +39,41 @@ namespace BlueWest.WebApi
Host1 = CreateHostBuilder(args) Host1 = CreateHostBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory()) .UseContentRoot(Directory.GetCurrentDirectory())
.Build(); .Build();
Host1.Run(); Host1.RunAsync();
// Use RunASync
/*System.Threading.Thread.Sleep(2500);
_threadServer = new ThreadServer(EventManager); var tryGetEventManager = Host1.Services.GetService(typeof(EventManager));
_threadServer.Init();*/
try
{
_ = Host1.Services.GetService(typeof(ExchangeInterface));
}
catch (Exception e)
{
if (e is InvalidOperationException exception)
{
if (e.Source == "Pomelo.EntityFrameworkCore.MySql")
{
throw new InvalidOperationException("MySQL Error");
}
}
throw;
}
if(tryGetEventManager == null) Console.WriteLine($"Failed to get {nameof(EventManager)} Service.");
if (tryGetEventManager is EventManager eventManager)
{
// Use RunASync
System.Threading.Thread.Sleep(2500);
_threadServer = new ThreadServer(eventManager);
_threadServer.Init();
}
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>

View File

@ -13,6 +13,8 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using BlueWest.Tools;
using BlueWest.WebApi.Interfaces;
using BlueWest.WebApi.MySQL; using BlueWest.WebApi.MySQL;
using BlueWest.WebApi.Tools; using BlueWest.WebApi.Tools;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -22,19 +24,28 @@ namespace BlueWest.WebApi
{ {
public class Startup public class Startup
{ {
public IConfiguration Configuration { get; }
private readonly IWebHostEnvironment _environment;
private readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
/// <summary> /// <summary>
/// Startup configuration of the API /// Startup configuration of the API
/// </summary> /// </summary>
/// <param name="configuration">Accessible configuration information</param> public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
public Startup(IConfiguration configuration)
{ {
Configuration = configuration; Configuration = configuration;
_environment = hostEnvironment;
} }
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. /// <summary>
/// Configure Services
/// </summary>
/// <param name="services">Dependency injection</param>
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddCors(options => services.AddCors(options =>
@ -88,10 +99,9 @@ namespace BlueWest.WebApi
services services
.AddDbContextPool<CountryDbContext>(options => options.GetSqlSettings(Configuration)) .AddSingleton<EventManager>()
.AddDbContextPool<UserDbContext>(options => options.GetSqlSettings(Configuration)) .PrepareDatabasePool(Configuration, _environment)
.AddDbContextPool<FinanceDbContext>(options => options.GetSqlSettings(Configuration)) .AddSingleton<ExchangeInterface>();
;
// services.AddGrpc(); // services.AddGrpc();
} }
@ -120,7 +130,6 @@ namespace BlueWest.WebApi
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();

View File

@ -1,20 +1,55 @@
using System; using System;
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace BlueWest.WebApi; namespace BlueWest.WebApi
public static class StartupExtensions
{ {
public static void GetSqlSettings(this DbContextOptionsBuilder optionsBuilder, IConfiguration configuration) /// <summary>
/// Startup Extensions
/// </summary>
public static class StartupExtensions
{ {
optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"), /// <summary>
new MySqlServerVersion(new Version(8, 0, 11))) /// Get MYSQL Connection String
/// </summary>
/// <param name="optionsBuilder"></param>
/// <param name="configuration"></param>
public static void GetMySqlSettings(
this DbContextOptionsBuilder optionsBuilder,
IConfiguration configuration,
IWebHostEnvironment environment)
{
var optionsBuilderRef = optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11)));
// The following three options help with debugging, but should // The following three options help with debugging, but should
// be changed or removed for production. // be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information) if (environment.IsDevelopment())
.EnableSensitiveDataLogging() {
.EnableDetailedErrors(); optionsBuilderRef
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
}
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection PrepareDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool<CountryDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<UserDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<FinanceDbContext>(options => options.GetMySqlSettings(configuration, environment));
}
} }
} }

View File

@ -7,8 +7,7 @@ namespace BlueWest.Data
[MapFrom(typeof(FinanceOpCreate))] [MapFrom(typeof(FinanceOpCreate))]
public partial class FinanceOp public partial class FinanceOp
{ {
public int UserId { get; set; }
public int UserId { get; set; }
[Key] public TimeSpan CreationDate { get; set; } [Key] public TimeSpan CreationDate { get; set; }

View File

@ -0,0 +1,8 @@
namespace BlueWest.Data
{
public class TransactionAmount
{
public Currency Currency { get; set; }
}
}

View File

@ -12,12 +12,16 @@ namespace BlueWest.Data
*/ */
public partial class User public partial class User
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public List<FinanceOp> FinanceTransactions { get; set; } public List<FinanceOp> FinanceTransactions { get; set; }
public Country Country { get; set; } public Country Country { get; set; }
[ForeignKey("Country")]
public int CountryId { get; set; }
public User(int id, string name) public User(int id, string name)
{ {
Id = id; Id = id;

View File

@ -0,0 +1,6 @@
namespace BlueWest.Data;
public class UserUnique
{
}

View File

@ -30,6 +30,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "README", "README", "{E9E3CE
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{552B9217-4BB2-432A-B1B5-45E9B1AB13BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlueWest.Api.Gateway", "BlueWest.Api.Gateway\BlueWest.Api.Gateway.csproj", "{A78343AF-77C6-48FD-A9C4-F8B7CBA56212}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -64,6 +68,10 @@ Global
{5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Debug|Any CPU.Build.0 = Debug|Any CPU {5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Release|Any CPU.ActiveCfg = Release|Any CPU {5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Release|Any CPU.Build.0 = Release|Any CPU {5BE0A68C-B3ED-4FA1-B74B-3E857504899B}.Release|Any CPU.Build.0 = Release|Any CPU
{A78343AF-77C6-48FD-A9C4-F8B7CBA56212}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A78343AF-77C6-48FD-A9C4-F8B7CBA56212}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A78343AF-77C6-48FD-A9C4-F8B7CBA56212}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A78343AF-77C6-48FD-A9C4-F8B7CBA56212}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -76,5 +84,7 @@ Global
{72B37540-A12F-466E-A58F-7BA2B247CB74} = {A1606EEC-6AC5-4779-B140-F57089F5A05F} {72B37540-A12F-466E-A58F-7BA2B247CB74} = {A1606EEC-6AC5-4779-B140-F57089F5A05F}
{08F4484E-5FD8-4590-A8D7-12FBE47120C8} = {A1606EEC-6AC5-4779-B140-F57089F5A05F} {08F4484E-5FD8-4590-A8D7-12FBE47120C8} = {A1606EEC-6AC5-4779-B140-F57089F5A05F}
{5BE0A68C-B3ED-4FA1-B74B-3E857504899B} = {A1606EEC-6AC5-4779-B140-F57089F5A05F} {5BE0A68C-B3ED-4FA1-B74B-3E857504899B} = {A1606EEC-6AC5-4779-B140-F57089F5A05F}
{293E7852-8AFD-4EFB-8C2D-F1BBE68AEE78} = {552B9217-4BB2-432A-B1B5-45E9B1AB13BC}
{A78343AF-77C6-48FD-A9C4-F8B7CBA56212} = {552B9217-4BB2-432A-B1B5-45E9B1AB13BC}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -1,10 +1,11 @@
using System; using System;
using BlueWest.Core; using BlueWest.Core;
using BlueWest.Core.ComponentSystem; using BlueWest.Core.ComponentSystem;
using PerformanceSolution.Core.System;
namespace BlueWest.Artefacts namespace BlueWest.Artefacts
{ {
/*public class WorldArtefact: Artefact public class WorldArtefact: Artefact
{ {
public WorldArtefact() public WorldArtefact()
@ -16,6 +17,7 @@ namespace BlueWest.Artefacts
protected override void Start() protected override void Start()
{ {
AddComponent<TimeManagement>(); AddComponent<TimeManagement>();
Console.WriteLine("World Artefact enabled");
//BlueConsole.Log("World Artefact started"); //BlueConsole.Log("World Artefact started");
} }
@ -33,5 +35,5 @@ namespace BlueWest.Artefacts
{ {
base.OnDisable(); base.OnDisable();
} }
}*/ }
} }

View File

@ -16,6 +16,12 @@ namespace BlueWest.Tools
_instance = this; _instance = this;
} }
public EventManager()
{
_subscribersList = new Dictionary<Type, List<EventListenerBase>>();
_instance = this;
}
/// <suary> /// <suary>
/// Adds a new subscriber to a certain event. /// Adds a new subscriber to a certain event.
/// </suary> /// </suary>

View File

@ -12,6 +12,8 @@ namespace BlueWest.Core
{ {
public bool IsActive { get; private set; } public bool IsActive { get; private set; }
public EventManager EventManager => _eventManager;
private readonly EventManager _eventManager; private readonly EventManager _eventManager;
public ThreadServer(EventManager eventManager) public ThreadServer(EventManager eventManager)
{ {
@ -70,6 +72,7 @@ namespace BlueWest.Core
{ {
Priority = threadPriority Priority = threadPriority
}; };
thread.Start(); thread.Start();
} }
} }