Made content directory configurable.

This commit is contained in:
Harrison Deng 2021-07-24 02:33:43 -05:00
parent 4476b1b3e1
commit 66aba04156
3 changed files with 17 additions and 4 deletions

View File

@ -0,0 +1,8 @@
namespace Props.Options
{
public class ContentOptions
{
public const string Content = "Content";
public string Dir { get; set; }
}
}

View File

@ -1,26 +1,28 @@
using System.IO; using System.IO;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Props.Options;
namespace Props.Services.Content namespace Props.Services.Content
{ {
public class CachedContentManager<TPage> : IContentManager<TPage> public class CachedContentManager<TPage> : IContentManager<TPage>
{ {
private dynamic data; private dynamic data;
private readonly string directory; private readonly ContentOptions options;
private readonly string fileName; private readonly string fileName;
dynamic IContentManager<TPage>.Json dynamic IContentManager<TPage>.Json
{ {
get get
{ {
if (data == null) data = JValue.Parse(File.ReadAllText(Path.Combine(directory, fileName))); if (data == null) data = JValue.Parse(File.ReadAllText(Path.Combine(options.Dir, fileName)));
return data; return data;
} }
} }
public CachedContentManager(string directory = "content") public CachedContentManager(IConfiguration configuration)
{ {
this.directory = directory; this.options = configuration.GetSection(ContentOptions.Content).Get<ContentOptions>();
this.fileName = typeof(TPage).Name.Replace("Model", "") + ".json"; this.fileName = typeof(TPage).Name.Replace("Model", "") + ".json";
} }
} }

View File

@ -15,5 +15,8 @@
"MaxResults": "100", "MaxResults": "100",
"ShopRegex": "Props\\.Shop\\.." "ShopRegex": "Props\\.Shop\\.."
}, },
"Content": {
"Dir": "./content"
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }