Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

78 lines
3.3 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Linq;
using EventStore.ClientAPI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Diagnostics;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.EventSourcing.Grains;
using Squidex.Infrastructure.Json;
using Squidex.Infrastructure.States;
namespace Squidex.Config.Domain
{
public static class EventStoreServices
{
public static void AddMyEventStoreServices(this IServiceCollection services, IConfiguration config)
{
config.ConfigureByOption("eventStore:type", new Options
{
["MongoDb"] = () =>
{
var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
var mongoDatabaseName = config.GetRequiredValue("eventStore:mongoDb:database");
services.AddSingletonAs(c =>
{
var mongoClient = Singletons<IMongoClient>.GetOrAdd(mongoConfiguration, s => new MongoClient(s));
var mongDatabase = mongoClient.GetDatabase(mongoDatabaseName);
return new MongoEventStore(mongDatabase, c.GetRequiredService<IEventNotifier>());
})
.As<IEventStore>();
},
["GetEventStore"] = () =>
{
var eventStoreConfiguration = config.GetRequiredValue("eventStore:getEventStore:configuration");
var eventStoreProjectionHost = config.GetRequiredValue("eventStore:getEventStore:projectionHost");
var eventStorePrefix = config.GetValue<string>("eventStore:getEventStore:prefix");
var connection = EventStoreConnection.Create(eventStoreConfiguration);
services.AddSingletonAs(connection)
.As<IEventStoreConnection>();
services.AddSingletonAs(c => new GetEventStore(connection, c.GetRequiredService<IJsonSerializer>(), eventStorePrefix, eventStoreProjectionHost))
.As<IEventStore>();
services.AddHealthChecks()
.AddCheck<GetEventStoreHealthCheck>("EventStore", tags: new[] { "node" });
}
});
services.AddSingletonAs<OrleansEventNotifier>()
.As<IEventNotifier>();
services.AddSingletonAs<DefaultStreamNameResolver>()
.As<IStreamNameResolver>();
services.AddSingletonAs<DefaultEventDataFormatter>()
.As<IEventDataFormatter>();
services.AddSingletonAs(c =>
{
var allEventConsumers = c.GetServices<IEventConsumer>();
return new EventConsumerFactory(n => allEventConsumers.FirstOrDefault(x => x.Name == n));
});
}
}
}