Skip to content

Commit

Permalink
[server] add conference mode setting and get user database name based…
Browse files Browse the repository at this point in the history
… on it
  • Loading branch information
Tomasz Opalach committed Jan 3, 2019
1 parent 9d9fb7c commit 9b0b1a8
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
16 changes: 14 additions & 2 deletions DemoCommon/Utils/Database/DatabaseName.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
using System;
using DemoCommon.Models;

namespace DemoCommon.Utils.Database
{
public static class DatabaseName
public class DatabaseName
{
private readonly DatabaseSettings _databaseSettings;
private readonly bool _conferenceMode;

public DatabaseName(DatabaseSettings databaseSettings, bool conferenceMode)
{
_databaseSettings = databaseSettings;
_conferenceMode = conferenceMode;
}

public const string UserDatabasePrefix = "User-";

public static string For(Guid userId) => $"{UserDatabasePrefix}{userId.ToString()}";
public string For(Guid userId) => _conferenceMode
? _databaseSettings.Name
: $"{UserDatabasePrefix}{userId.ToString()}";
}
}
7 changes: 3 additions & 4 deletions DemoCron/ServiceLocator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using DemoCommon.Utils.Database;
using DemoCron.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -12,10 +13,7 @@ internal static class ServiceLocator
{
private static ServiceProvider Provider { get; set; }

public static T Resolve<T>()
{
return (T)Provider.GetService(typeof(T));
}
public static T Resolve<T>() => (T)Provider.GetService(typeof(T));

public static object Resolve(Type t)
{
Expand Down Expand Up @@ -45,6 +43,7 @@ public static void Configure()

serviceCollection.AddSingleton<Startup>();
serviceCollection.AddSingleton<DocumentStoreHolder>();
serviceCollection.AddSingleton(_ => new DatabaseName(settings.Database, conferenceMode: false));

serviceCollection.AddScoped<DeleteUnusedDatabasesTask>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ namespace DemoServer.Controllers.Demos.Advanced.CreateDatabase
{
public class CreateDatabaseController : DemoCodeController
{
private readonly DatabaseName _databaseName;

public CreateDatabaseController(HeadersAccessor headersAccessor, DocumentStoreCache documentStoreCache,
DatabaseSetup databaseSetup) : base(headersAccessor, documentStoreCache, databaseSetup)
DatabaseSetup databaseSetup, DatabaseName databaseName) : base(headersAccessor, documentStoreCache, databaseSetup)
{
_databaseName = databaseName;
}

protected override Task SetDemoPrerequisites()
Expand All @@ -28,7 +31,7 @@ protected override Task SetDemoPrerequisites()
[HttpPost]
public IActionResult Run()
{
var databaseName = DatabaseName.For(UserId);
var databaseName = _databaseName.For(UserId);

#region Demo

Expand Down
2 changes: 2 additions & 0 deletions DemoServer/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace DemoServer
public class Settings
{
public DatabaseSettings Database { get; set; }

public bool ConferenceMode { get; set; }
}
}
4 changes: 3 additions & 1 deletion DemoServer/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DemoServer.Utils;
using DemoCommon.Utils.Database;
using DemoServer.Utils;
using DemoServer.Utils.Cache;
using DemoServer.Utils.Database;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -49,6 +50,7 @@ public void ConfigureServices(IServiceCollection services)
var demoContainer = DemoContainer.Initialize("Controllers\\Demos", LoggerFactory.CreateLogger<DemoContainer>());
services.AddSingleton(demoContainer);
services.AddSingleton<DocumentStoreHolder>();
services.AddSingleton(_ => new DatabaseName(settings.Database, settings.ConferenceMode));

services.AddScoped<DocumentStoreCache>();
services.AddScoped<HeadersAccessor>();
Expand Down
8 changes: 5 additions & 3 deletions DemoServer/Utils/Cache/DocumentStoreCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using DemoCommon.Utils;
using DemoCommon.Utils.Database;
using DemoServer.Utils.Database;
using Microsoft.Extensions.Caching.Memory;
Expand All @@ -14,11 +13,14 @@ public class DocumentStoreCache

private readonly IMemoryCache _memoryCache;
private readonly DocumentStoreHolder _documentStoreHolder;
private readonly DatabaseName _databaseName;

public DocumentStoreCache(IMemoryCache memoryCache, DocumentStoreHolder documentStoreHolder)
public DocumentStoreCache(IMemoryCache memoryCache, DocumentStoreHolder documentStoreHolder,
DatabaseName databaseName)
{
_memoryCache = memoryCache;
_documentStoreHolder = documentStoreHolder;
_databaseName = databaseName;
}

private string GetKeyName(Guid userId) => $"{KeyPrefix}{userId}";
Expand All @@ -33,7 +35,7 @@ public IDocumentStore GetEntry(Guid userId)
private IDocumentStore SetEntry(ICacheEntry cacheEntry, Guid userId)
{
cacheEntry.SlidingExpiration = FiveMinutes;
var databaseName = DatabaseName.For(userId);
var databaseName = _databaseName.For(userId);

return _documentStoreHolder.CreateStore(databaseName);
}
Expand Down
7 changes: 4 additions & 3 deletions DemoServer/Utils/Database/DatabaseLinks.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using DemoCommon.Models;
using DemoCommon.Utils;
using DemoCommon.Utils.Database;

namespace DemoServer.Utils.Database
{
public class DatabaseLinks
{
private readonly DatabaseSettings _databaseSettings;
private readonly DatabaseName _databaseName;

public DatabaseLinks(Settings settings)
public DatabaseLinks(Settings settings, DatabaseName databaseName)
{
_databaseName = databaseName;
_databaseSettings = settings.Database;
}

Expand All @@ -20,7 +21,7 @@ public DatabaseLinks(Settings settings)

public string ToDocuments(Guid userId)
{
var databaseName = DatabaseName.For(userId);
var databaseName = _databaseName.For(userId);
return $"{StudioUrl}#databases/documents?&database={databaseName}";
}
}
Expand Down
6 changes: 4 additions & 2 deletions DemoServer/Utils/Database/DatabaseSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ public class DatabaseSetup
private readonly DatabaseApi _databaseApi = new DatabaseApi();

private readonly DocumentStoreCache _documentStoreCache;
private readonly DatabaseName _databaseName;

public DatabaseSetup(DocumentStoreCache documentStoreCache)
public DatabaseSetup(DocumentStoreCache documentStoreCache, DatabaseName databaseName)
{
_documentStoreCache = documentStoreCache;
_databaseName = databaseName;
}

private IDocumentStore GetDocumentStore(Guid userId) => _documentStoreCache.GetEntry(userId);

private IAsyncDocumentSession OpenAsyncSession(Guid userId)
{
var databaseName = DatabaseName.For(userId);
var databaseName = _databaseName.For(userId);
var documentStore = _documentStoreCache.GetEntry(userId);
return documentStore.OpenAsyncSession(databaseName);
}
Expand Down
5 changes: 3 additions & 2 deletions DemoServer/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Database": {
"Urls": [ "http://localhost:8080" ],
"Name": "Demo"
}
"Name": "Demo"
},
"ConferenceMode": false
}

0 comments on commit 9b0b1a8

Please sign in to comment.