calendar-webapp/source/calendarwebapp/configuration.d

69 lines
2 KiB
D
Raw Normal View History

2017-09-17 17:52:41 +02:00
module calendarwebapp.configuration;
2017-11-09 02:24:15 +01:00
import calendarwebapp.authenticator : Authenticator, MongoDBAuthenticator,
MySQLAuthenticator;
2017-09-17 17:52:41 +02:00
import calendarwebapp.calendarwebapp : CalendarWebapp;
2017-10-30 02:31:48 +01:00
import calendarwebapp.event : EventStore, MongoDBEventStore, MySQLEventStore;
import calendarwebapp.passhash : PasswordHasher, SHA256PasswordHasher;
2017-09-17 17:52:41 +02:00
2017-10-30 02:31:48 +01:00
import mysql : MySQLPool;
2017-09-17 17:52:41 +02:00
import poodinis;
import vibe.db.mongo.client : MongoClient;
import vibe.db.mongo.collection : MongoCollection;
import vibe.db.mongo.mongo : connectMongoDB;
class Context : ApplicationContext
{
public:
override void registerDependencies(shared(DependencyContainer) container)
{
auto mongoClient = connectMongoDB("localhost");
2017-11-20 22:36:16 +01:00
auto pool = new MySQLPool("localhost", "username", "password", "CalendarWebapp");
2017-10-30 02:31:48 +01:00
container.register!MySQLPool.existingInstance(pool);
2017-09-17 17:52:41 +02:00
container.register!MongoClient.existingInstance(mongoClient);
2017-10-30 02:31:48 +01:00
container.register!(EventStore, MySQLEventStore);
container.register!(Authenticator, MySQLAuthenticator);
container.register!(PasswordHasher, SHA256PasswordHasher);
2017-09-17 17:52:41 +02:00
container.register!CalendarWebapp;
container.register!(ValueInjector!string, StringInjector);
container.register!(ValueInjector!MongoCollection, MongoCollectionInjector);
}
}
class StringInjector : ValueInjector!string
{
private:
string[string] config;
public:
this() const @safe pure nothrow
{
// dfmt off
config = ["Database name" : "CalendarWebapp",
"Users collection name": "users",
"Events collection name" : "events"];
// dfmt on
}
override string get(string key) const @safe pure nothrow
{
return config[key];
}
}
class MongoCollectionInjector : ValueInjector!MongoCollection
{
private:
@Autowire MongoClient mongoClient;
@Value("Database name")
string databaseName;
public:
override MongoCollection get(string key) @safe
{
return mongoClient.getCollection(databaseName ~ "." ~ key);
}
}