Implement database connection within the server (move functionality from plugin)

This commit is contained in:
ChronosX88 2020-10-07 18:50:54 +04:00
parent e8b99b81b7
commit 64596e46d8
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
10 changed files with 93 additions and 75 deletions

View File

@ -2,6 +2,7 @@ using Zirconium.Core.Plugins;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Core.Logging; using Zirconium.Core.Logging;
using WebSocketSharp.Server; using WebSocketSharp.Server;
using Zirconium.Core.Database;
namespace Zirconium.Core namespace Zirconium.Core
{ {
@ -14,6 +15,7 @@ namespace Zirconium.Core
public IPluginHostAPI PluginHostAPI { get; } public IPluginHostAPI PluginHostAPI { get; }
public AuthManager AuthManager { get; } public AuthManager AuthManager { get; }
private WebSocketServer _websocketServer; private WebSocketServer _websocketServer;
public DatabaseConnector Database { get; private set; }
public App(Config config) public App(Config config)
{ {
@ -24,6 +26,7 @@ namespace Zirconium.Core
Router = new Router(this); Router = new Router(this);
PluginHostAPI = new PluginHostAPI(this, Router); PluginHostAPI = new PluginHostAPI(this, Router);
AuthManager = new AuthManager(this); AuthManager = new AuthManager(this);
Database = new DatabaseConnector(this);
PluginManager = new PluginManager(PluginHostAPI); PluginManager = new PluginManager(PluginHostAPI);
PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins); PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins);
Log.Info("Zirconium is initialized successfully"); Log.Info("Zirconium is initialized successfully");
@ -34,7 +37,8 @@ namespace Zirconium.Core
_websocketServer.Start(); _websocketServer.Start();
} }
public void Destroy() { public void Destroy()
{
Log.Info("Shutting down Zirconium..."); Log.Info("Shutting down Zirconium...");
_websocketServer.Stop(); _websocketServer.Stop();
} }

View File

@ -19,6 +19,9 @@ namespace Zirconium.Core
// Websocket server settings // Websocket server settings
public Websocket Websocket { get; set; } public Websocket Websocket { get; set; }
// Database connection credentials
public MongoDatabaseConfig MongoDatabase { get; set; }
// Configurations of plugins // Configurations of plugins
public Dictionary<string, dynamic> Plugins { get; set; } public Dictionary<string, dynamic> Plugins { get; set; }
@ -31,4 +34,13 @@ namespace Zirconium.Core
public int Port { get; set; } public int Port { get; set; }
public string Endpoint { get; set; } public string Endpoint { get; set; }
} }
public class MongoDatabaseConfig
{
public string Host { get; set; }
public int Port { get; set; }
public string User { get; set; }
public string Password { get; set; }
public string Database { get; set; }
}
} }

View File

@ -0,0 +1,35 @@
using MongoDB.Driver;
using Zirconium.Core.Logging;
namespace Zirconium.Core.Database
{
public class DatabaseConnector
{
private App _app;
public IMongoDatabase MongoDatabase { get; private set; }
public DatabaseConnector(App app)
{
this._app = app;
MongoClient client;
string host = _app.Config.MongoDatabase.Host;
int port = _app.Config.MongoDatabase.Port;
string user = _app.Config.MongoDatabase.User;
string password = _app.Config.MongoDatabase.Password;
string database = _app.Config.MongoDatabase.Database;
if ((user == null || user == "") && (password == null || password == ""))
{
client = new MongoClient($"mongodb://{host}:{port}");
}
else
{
client = new MongoClient($"mongodb://{user}:{password}@{host}:{port}");
}
var db = client.GetDatabase(database);
this.MongoDatabase = db;
Log.Debug("Database initialized");
}
}
}

View File

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Driver;
using Zirconium.Core.Models; using Zirconium.Core.Models;
namespace Zirconium.Core.Plugins.Interfaces namespace Zirconium.Core.Plugins.Interfaces
@ -21,5 +22,6 @@ namespace Zirconium.Core.Plugins.Interfaces
void RegisterIPCService(IPluginAPI plugin, dynamic service); void RegisterIPCService(IPluginAPI plugin, dynamic service);
Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject); Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject);
Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject); Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject);
IMongoDatabase GetRawDatabase();
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Driver;
using Newtonsoft.Json; using Newtonsoft.Json;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
@ -96,5 +97,10 @@ namespace Zirconium.Core.Plugins
public Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject) { public Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject) {
return _ipcRouter.MakeNotif(pluginName, methodName, paramsObject); return _ipcRouter.MakeNotif(pluginName, methodName, paramsObject);
} }
public IMongoDatabase GetRawDatabase()
{
return _app.Database.MongoDatabase;
}
} }
} }

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Threading; using System.Threading;
using McMaster.NETCore.Plugins; using McMaster.NETCore.Plugins;
using MongoDB.Driver;
using Zirconium.Core.Logging; using Zirconium.Core.Logging;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
@ -75,7 +76,8 @@ namespace Zirconium.Core.Plugins
typeof(ICoreEventHandler), typeof(ICoreEventHandler),
typeof(BaseMessage), typeof(BaseMessage),
typeof(CoreEvent), typeof(CoreEvent),
typeof(ExportedIPCMethodAttribute) typeof(ExportedIPCMethodAttribute),
typeof(IMongoDatabase)
}, },
config => config.PreferSharedTypes = true config => config.PreferSharedTypes = true
); );

View File

@ -11,5 +11,9 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" /> <PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" />
<PackageReference Include="Nett" Version="0.15.0" /> <PackageReference Include="Nett" Version="0.15.0" />
<PackageReference Include="MongoDB.Bson" Version="2.10.4" />
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.10.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,65 +0,0 @@
using System;
using System.Collections.Immutable;
using System.Collections.Generic;
using Zirconium.Core.Plugins.Interfaces;
using MongoDB.Driver;
using Zirconium.Core.Logging;
using Zirconium.Core.Plugins.IPC;
namespace MongoDBPlugin
{
class Plugin : IPluginAPI
{
private IMongoDatabase _database;
public string GetPluginUniqueName()
{
return "MongoDB";
}
public void Initialize(IPluginHostAPI pluginHostAPI)
{
var settingsDynamic = pluginHostAPI.GetSettings(this);
var settings = (IImmutableDictionary<string, dynamic>)((IDictionary<string, object>)settingsDynamic).ToImmutableDictionary();
var host = (string)settings.GetValueOrDefault("Host");
var port = (int)settings.GetValueOrDefault("Port");
var user = (string)settings.GetValueOrDefault("User");
var password = (string)settings.GetValueOrDefault("Password");
var database = (string)settings.GetValueOrDefault("Database");
MongoClient client;
if (user == null && password == null)
{
client = new MongoClient($"mongodb://{host}:{port}");
}
else
{
client = new MongoClient($"mongodb://{user}:{password}@{host}:{port}");
}
var db = client.GetDatabase(database);
Log.Info("MongoDB is connected");
_database = db;
var ipcService = new IPCService(db);
pluginHostAPI.RegisterIPCService(this, ipcService);
}
public void PreInitialize(IPluginManager pluginManager) { }
class IPCService
{
private IMongoDatabase _db;
public IPCService(IMongoDatabase db) { _db = db; }
[ExportedIPCMethod("Insert")]
public void Insert(dynamic paramsObject) {
var colName = paramsObject.ColName;
var model = paramsObject.Model;
_db.GetCollection<dynamic>(colName).InsertOne(model);
Log.Debug("successfully inserted the document");
}
}
}
}

View File

@ -1,4 +1,9 @@
using System.Dynamic; using System.ComponentModel.DataAnnotations.Schema;
using System.Dynamic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Zirconium.Core.Logging;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
namespace TestMongoDB namespace TestMongoDB
@ -11,24 +16,34 @@ namespace TestMongoDB
return "TestMongoDB"; return "TestMongoDB";
} }
public async void Initialize(IPluginHostAPI pluginHostAPI) public void Initialize(IPluginHostAPI pluginHostAPI)
{ {
var tm = new TestModel(); var tm = new TestModel();
tm.ABC = "qweqowie"; tm.ABC = "qweqowie";
dynamic paramsObject = new ExpandoObject(); var db = pluginHostAPI.GetRawDatabase();
paramsObject.ColName = "test_model";
paramsObject.Model = tm; db.GetCollection<TestModel>("test_model").InsertOne(tm);
await pluginHostAPI.MakeIPCRequest("MongoDB", "Insert", paramsObject); Log.Debug("successfully inserted the model");
var filter = Builders<TestModel>.Filter.Eq("ABC", "qweqowie");
var found = db.GetCollection<TestModel>("test_model").Find(filter).FirstOrDefault();
if (found != null) {
Log.Debug($"Found document with ABC property: {found.ABC}");
} else {
Log.Debug("Nothing found!");
}
} }
public void PreInitialize(IPluginManager pluginManager) public void PreInitialize(IPluginManager pluginManager)
{ {
pluginManager.Depends(this, "MongoDB");
} }
} }
class TestModel class TestModel
{ {
[BsonId]
public ObjectId Id { get; set; }
public string ABC { get; set; } public string ABC { get; set; }
} }
} }

View File

@ -8,6 +8,9 @@
<ProjectReference Include="../../Zirconium/Zirconium.csproj"> <ProjectReference Include="../../Zirconium/Zirconium.csproj">
<ExcludeAssets>runtime</ExcludeAssets> <ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference> </ProjectReference>
<PackageReference Include="MongoDB.Bson" Version="2.10.4" />
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.10.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>