mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-25 03:42:22 +00:00
Implement database connection within the server (move functionality from plugin)
This commit is contained in:
parent
e8b99b81b7
commit
64596e46d8
@ -2,6 +2,7 @@ using Zirconium.Core.Plugins;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
using Zirconium.Core.Logging;
|
||||
using WebSocketSharp.Server;
|
||||
using Zirconium.Core.Database;
|
||||
|
||||
namespace Zirconium.Core
|
||||
{
|
||||
@ -14,6 +15,7 @@ namespace Zirconium.Core
|
||||
public IPluginHostAPI PluginHostAPI { get; }
|
||||
public AuthManager AuthManager { get; }
|
||||
private WebSocketServer _websocketServer;
|
||||
public DatabaseConnector Database { get; private set; }
|
||||
|
||||
public App(Config config)
|
||||
{
|
||||
@ -24,6 +26,7 @@ namespace Zirconium.Core
|
||||
Router = new Router(this);
|
||||
PluginHostAPI = new PluginHostAPI(this, Router);
|
||||
AuthManager = new AuthManager(this);
|
||||
Database = new DatabaseConnector(this);
|
||||
PluginManager = new PluginManager(PluginHostAPI);
|
||||
PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins);
|
||||
Log.Info("Zirconium is initialized successfully");
|
||||
@ -34,7 +37,8 @@ namespace Zirconium.Core
|
||||
_websocketServer.Start();
|
||||
}
|
||||
|
||||
public void Destroy() {
|
||||
public void Destroy()
|
||||
{
|
||||
Log.Info("Shutting down Zirconium...");
|
||||
_websocketServer.Stop();
|
||||
}
|
||||
|
@ -19,6 +19,9 @@ namespace Zirconium.Core
|
||||
// Websocket server settings
|
||||
public Websocket Websocket { get; set; }
|
||||
|
||||
// Database connection credentials
|
||||
public MongoDatabaseConfig MongoDatabase { get; set; }
|
||||
|
||||
// Configurations of plugins
|
||||
public Dictionary<string, dynamic> Plugins { get; set; }
|
||||
|
||||
@ -31,4 +34,13 @@ namespace Zirconium.Core
|
||||
public int Port { 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; }
|
||||
}
|
||||
}
|
35
src/Zirconium/Core/Database/DatabaseConnector.cs
Normal file
35
src/Zirconium/Core/Database/DatabaseConnector.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Models;
|
||||
|
||||
namespace Zirconium.Core.Plugins.Interfaces
|
||||
@ -21,5 +22,6 @@ namespace Zirconium.Core.Plugins.Interfaces
|
||||
void RegisterIPCService(IPluginAPI plugin, dynamic service);
|
||||
Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject);
|
||||
Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject);
|
||||
IMongoDatabase GetRawDatabase();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using Newtonsoft.Json;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
@ -96,5 +97,10 @@ namespace Zirconium.Core.Plugins
|
||||
public Task MakeIPCNotification(string pluginName, string methodName, dynamic paramsObject) {
|
||||
return _ipcRouter.MakeNotif(pluginName, methodName, paramsObject);
|
||||
}
|
||||
|
||||
public IMongoDatabase GetRawDatabase()
|
||||
{
|
||||
return _app.Database.MongoDatabase;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Runtime.Loader;
|
||||
using System.Threading;
|
||||
using McMaster.NETCore.Plugins;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Logging;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
@ -75,7 +76,8 @@ namespace Zirconium.Core.Plugins
|
||||
typeof(ICoreEventHandler),
|
||||
typeof(BaseMessage),
|
||||
typeof(CoreEvent),
|
||||
typeof(ExportedIPCMethodAttribute)
|
||||
typeof(ExportedIPCMethodAttribute),
|
||||
typeof(IMongoDatabase)
|
||||
},
|
||||
config => config.PreferSharedTypes = true
|
||||
);
|
||||
|
@ -11,5 +11,9 @@
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.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>
|
||||
</Project>
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
namespace TestMongoDB
|
||||
@ -11,24 +16,34 @@ namespace TestMongoDB
|
||||
return "TestMongoDB";
|
||||
}
|
||||
|
||||
public async void Initialize(IPluginHostAPI pluginHostAPI)
|
||||
public void Initialize(IPluginHostAPI pluginHostAPI)
|
||||
{
|
||||
var tm = new TestModel();
|
||||
tm.ABC = "qweqowie";
|
||||
dynamic paramsObject = new ExpandoObject();
|
||||
paramsObject.ColName = "test_model";
|
||||
paramsObject.Model = tm;
|
||||
await pluginHostAPI.MakeIPCRequest("MongoDB", "Insert", paramsObject);
|
||||
var db = pluginHostAPI.GetRawDatabase();
|
||||
|
||||
db.GetCollection<TestModel>("test_model").InsertOne(tm);
|
||||
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)
|
||||
{
|
||||
pluginManager.Depends(this, "MongoDB");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class TestModel
|
||||
{
|
||||
[BsonId]
|
||||
public ObjectId Id { get; set; }
|
||||
|
||||
public string ABC { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,9 @@
|
||||
<ProjectReference Include="../../Zirconium/Zirconium.csproj">
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</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>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user