Fixed: Fix caching spec for Initialize.js

pull/6/head
Qstick 7 years ago
parent 026a2cace5
commit 6cee554760

@ -30,6 +30,7 @@ namespace Lidarr.Http.Frontend
if (context.Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase)) return false;
if (context.Request.Path.EndsWith("index.js")) return false;
if (context.Request.Path.EndsWith("initialize.js")) return false;
if (context.Request.Path.StartsWith("/feed", StringComparison.CurrentCultureIgnoreCase)) return false;
if (context.Request.Path.StartsWith("/log", StringComparison.CurrentCultureIgnoreCase) &&
@ -46,4 +47,4 @@ namespace Lidarr.Http.Frontend
return true;
}
}
}
}

@ -1,4 +1,5 @@
using System.IO;
using System.Text;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common.EnvironmentInfo;
@ -12,6 +13,10 @@ namespace Lidarr.Http.Frontend
private readonly IConfigFileProvider _configFileProvider;
private readonly IAnalyticsService _analyticsService;
private static string _apiKey;
private static string _urlBase;
private string _generatedContent;
public InitializeJsModule(IConfigFileProvider configFileProvider,
IAnalyticsService analyticsService)
@ -19,6 +24,9 @@ namespace Lidarr.Http.Frontend
_configFileProvider = configFileProvider;
_analyticsService = analyticsService;
_apiKey = configFileProvider.ApiKey;
_urlBase = configFileProvider.UrlBase;
Get["/initialize.js"] = x => Index();
}
@ -30,25 +38,41 @@ namespace Lidarr.Http.Frontend
private Stream GetContentStream()
{
var urlBase = _configFileProvider.UrlBase;
var text = GetContent();
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine("window.Lidarr = {");
writer.WriteLine($" apiRoot: '{urlBase}/api/v1',");
writer.WriteLine($" apiKey: '{_configFileProvider.ApiKey}',");
writer.WriteLine($" release: '{BuildInfo.Release}',");
writer.WriteLine($" version: '{BuildInfo.Version.ToString()}',");
writer.WriteLine($" branch: '{_configFileProvider.Branch.ToLower()}',");
writer.WriteLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},");
writer.WriteLine($" urlBase: '{urlBase}',");
writer.WriteLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}");
writer.WriteLine("};");
writer.Write(text);
writer.Flush();
stream.Position = 0;
return stream;
}
private string GetContent()
{
if (RuntimeInfo.IsProduction && _generatedContent != null)
{
return _generatedContent;
}
var builder = new StringBuilder();
builder.AppendLine("window.Lidarr = {");
builder.AppendLine($" apiRoot: '{_urlBase}/api/v1',");
builder.AppendLine($" apiKey: '{_apiKey}',");
builder.AppendLine($" release: '{BuildInfo.Release}',");
builder.AppendLine($" version: '{BuildInfo.Version.ToString()}',");
builder.AppendLine($" branch: '{_configFileProvider.Branch.ToLower()}',");
builder.AppendLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},");
builder.AppendLine($" urlBase: '{_urlBase}',");
builder.AppendLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}");
builder.AppendLine("};");
_generatedContent = builder.ToString();
return _generatedContent;
}
}
}

Loading…
Cancel
Save