using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Blog3000.Server { public class BlogEnv { private readonly IWebHostEnvironment env; private readonly ILogger logger; public BlogEnv(ILogger logger, IWebHostEnvironment env) { this.env = env; this.logger = logger; // Determin path, set in AppDomain-var (for legacy) AppDataPath = BuildAppDataPath(); System.AppDomain.CurrentDomain.SetData("AppDataPath", AppDataPath); logger.LogInformation($"Set AppDataPath={AppDataPath}"); } public string AppDataPath { get; private set; } private BlogConfigJson _configJson; public BlogConfigJson BlogConfig { get { if (_configJson == null) { try { //var path = env.WebRootFileProvider.GetFileInfo("config.json")?.PhysicalPath; _configJson = LoadConfig(); } catch (Exception ex) { //logger.LogWarning($"Error reading wwwroot/config.json {ex}"); logger.LogWarning($"Error reading blogconfig.json {ex}"); } } return _configJson; } } public static BlogConfigJson LoadConfig() { Console.WriteLine($"Loading blogconfig.json {GetCfgFilePath()}"); var txt = System.IO.File.ReadAllText(GetCfgFilePath()); return JsonConvert.DeserializeObject(txt); } public static void SaveConfig(BlogConfigJson c) { var txt = JsonConvert.SerializeObject(c, Formatting.Indented); System.IO.File.WriteAllText(GetCfgFilePath(), txt); } private static string BuildAppDataPath() { // Determin path, set in AppDomain-var (for legacy) if (Program.blogDataPath == null) { //logger.LogInformation($"CurrentDir->{Environment.CurrentDirectory}"); //AppDataPath = System.IO.Path.Combine(env.ContentRootPath, "app_data"); return System.IO.Path.Combine(Environment.CurrentDirectory, "app_data"); } else { return Program.blogDataPath; } } private static string GetCfgFilePath() { return System.IO.Path.Combine(BuildAppDataPath(), "blogconfig.json"); } public class BlogConfigJson: ClientConfigJson { public ClientConfigJson ToClientConfig() { return new ClientConfigJson { Title = Title, DefaultEMailAddress = DefaultEMailAddress, AuthorFullnames = AuthorFullnames, TopTopics = TopTopics, SocialLinks = SocialLinks }; } // public bool AllowSearchBots { get; set; } public bool AllowSearchDevBot { get; set; } public string SyslogAppName { get; set; } public string PublicDlUri { get; set; } public int? ListenPort { get; set; } public string VisibleUrlPrefix { get; set; } // public ProtectedConfig Protected { get; set; } public bool TwitterEnabled { get; set; } public bool TwitterTestMode { get; set; } public override string ToString() { return JsonConvert.SerializeObject(this, Formatting.Indented); } } public class ProtectedConfig { public string TwitterAPIKey { get; set; } public string TwitterAPISecret { get; set; } public string TwitterAccessToken { get; set; } public string TwitterAccessSecret { get; set; } } public class ClientConfigJson { public string Title { get; set; } public string DefaultEMailAddress { get; set; } public Dictionary AuthorFullnames { get; set; } public Dictionary SocialLinks { get; set; } public string[] TopTopics { get; set; } } } }