public static IHostBuilder CreateHostBuilder(string[] args) { return(Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { // Custom Config var config = new ConfigurationBuilder() .AddJsonFile("LiveReloadServer.json", optional: true) .AddJsonFile("LiveReloadWebServer.json", optional: true) .AddEnvironmentVariables() .AddEnvironmentVariables("LIVERELOADSERVER_") .AddEnvironmentVariables("LIVERELOADWEBSERVER_") .AddCommandLine(args) .Build(); var serverConfig = new LiveReloadServerConfiguration(); serverConfig.LoadFromConfiguration(config); if (!serverConfig.ShowConsoleOutput) { Console.SetError(TextWriter.Null); Console.SetOut(TextWriter.Null); } var webRoot = serverConfig.WebRoot; if (!Directory.Exists(webRoot)) { throw new ApplicationException($"Launch Error: WebRoot '{webRoot}' is not a valid directory."); } // Custom Logging webBuilder .ConfigureLogging(logging => { logging.ClearProviders(); if (serverConfig.ShowConsoleOutput) { logging.AddConsole(); } logging.AddConfiguration(config); }) .UseConfiguration(config); if (!string.IsNullOrEmpty(webRoot)) { webBuilder.UseWebRoot(webRoot); } webBuilder.UseUrls($"http{(serverConfig.UseSsl ? "s" : "")}://{serverConfig.Host}:{serverConfig.Port}"); webBuilder .UseStartup <Startup>(); })); }
/// <summary> /// Loads and fixes up configuration values from the configuraton. /// </summary> /// <remarks> /// Note we're custom loading this to allow for not overly string command line syntax /// </remarks> /// <param name="Configuration">.NET Core Configuration Provider</param> public bool LoadFromConfiguration(IConfiguration Configuration) { Current = this; WebRoot = Configuration["WebRoot"]; if (string.IsNullOrEmpty(WebRoot)) { WebRoot = Environment.CurrentDirectory; } else { WebRoot = Path.GetFullPath(WebRoot, Environment.CurrentDirectory); } Port = Helpers.GetIntegerSetting("Port", Configuration, Port); UseSsl = Helpers.GetLogicalSetting("UseSsl", Configuration, UseSsl); Host = Helpers.GetStringSetting("Host", Configuration, Host); DefaultFiles = Helpers.GetStringSetting("DefaultFiles", Configuration, DefaultFiles); Extensions = Helpers.GetStringSetting("Extensions", Configuration, Extensions); UseLiveReload = Helpers.GetLogicalSetting("UseLiveReload", Configuration, UseLiveReload); UseRazor = Helpers.GetLogicalSetting("UseRazor", Configuration); ShowUrls = Helpers.GetLogicalSetting("ShowUrls", Configuration, ShowUrls); OpenBrowser = Helpers.GetLogicalSetting("OpenBrowser", Configuration, OpenBrowser); FolderNotFoundFallbackPath = Helpers.GetStringSetting("FolderNotFoundFallbackPath", Configuration, null); // Enables Markdown Middleware and optionally copies Markdown Templates into output folder UseMarkdown = Helpers.GetLogicalSetting("UseMarkdown", Configuration, false); if (UseMarkdown) { // defaults to true but only if Markdown is enabled! CopyMarkdownResources = Helpers.GetLogicalSetting("CopyMarkdownResources", Configuration, CopyMarkdownResources); } MarkdownTemplate = Helpers.GetStringSetting("MarkdownTemplate", Configuration, MarkdownTemplate); MarkdownTheme = Helpers.GetStringSetting("MarkdownTheme", Configuration, MarkdownTheme); MarkdownSyntaxTheme = Helpers.GetStringSetting("MarkdownSyntaxTheme", Configuration, MarkdownSyntaxTheme); return(true); }
public static IHostBuilder CreateHostBuilder(string[] args) { return(Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { // Custom Config var config = new ConfigurationBuilder() .AddJsonFile("LiveReloadServer.json", optional: true) .AddJsonFile("LiveReloadWebServer.json", optional: true) .AddEnvironmentVariables() .AddEnvironmentVariables("LIVERELOADSERVER_") .AddEnvironmentVariables("LIVERELOADWEBSERVER_") .AddCommandLine(args) .Build(); var serverConfig = new LiveReloadServerConfiguration(); serverConfig.LoadFromConfiguration(config); // Custom Logging webBuilder .ConfigureLogging(logging => { logging.ClearProviders(); logging.AddConsole(); logging.AddConfiguration(config); }) .UseConfiguration(config); var webRoot = serverConfig.WebRoot; if (!string.IsNullOrEmpty(webRoot)) { webBuilder.UseWebRoot(webRoot); } webBuilder.UseUrls($"http{(serverConfig.UseSsl ? "s" : "")}://{serverConfig.Host}:{serverConfig.Port}"); webBuilder .UseStartup <Startup>(); })); }
/// <summary> /// Loads and fixes up configuration values from the configuraton. /// </summary> /// <remarks> /// Note we're custom loading this to allow for not overly string command line syntax /// </remarks> /// <param name="Configuration">.NET Core Configuration Provider</param> public bool LoadFromConfiguration(IConfiguration Configuration) { Current = this; WebRoot = Configuration["WebRoot"]; if (string.IsNullOrEmpty(WebRoot)) { // if not set but the first arg does not start with the - it's the folder var args = Environment.GetCommandLineArgs(); if (args.Length > 1 && !args[1].StartsWith("-")) { WebRoot = args[1]; } } if (string.IsNullOrEmpty(WebRoot)) { WebRoot = Environment.CurrentDirectory; } else { var expandedPath = FileUtils.ExpandPathEnvironmentVariables(WebRoot); WebRoot = Path.GetFullPath(expandedPath, Environment.CurrentDirectory); } Port = Helpers.GetIntegerSetting("Port", Configuration, Port); UseSsl = Helpers.GetLogicalSetting("UseSsl", Configuration, UseSsl); Host = Helpers.GetStringSetting("Host", Configuration, Host); DefaultFiles = Helpers.GetStringSetting("DefaultFiles", Configuration, DefaultFiles); Extensions = Helpers.GetStringSetting("Extensions", Configuration, Extensions); UseLiveReload = Helpers.GetLogicalSetting("UseLiveReload", Configuration, UseLiveReload); UseRazor = Helpers.GetLogicalSetting("UseRazor", Configuration); ShowUrls = Helpers.GetLogicalSetting("ShowUrls", Configuration, ShowUrls); OpenBrowser = Helpers.GetLogicalSetting("OpenBrowser", Configuration, OpenBrowser); OpenEditor = Helpers.GetLogicalSetting("OpenEditor", Configuration, OpenEditor); EditorLaunchCommand = Helpers.GetStringSetting("EditorLaunchCommand", Configuration, EditorLaunchCommand); DetailedErrors = Helpers.GetLogicalSetting("DetailedErrors", Configuration, DetailedErrors); FolderNotFoundFallbackPath = Helpers.GetStringSetting("FolderNotFoundFallbackPath", Configuration, null); // Enables Markdown Middleware and optionally copies Markdown Templates into output folder UseMarkdown = Helpers.GetLogicalSetting("UseMarkdown", Configuration, false); if (UseMarkdown) { // defaults to true but only if Markdown is enabled! CopyMarkdownResources = Helpers.GetLogicalSetting("CopyMarkdownResources", Configuration, CopyMarkdownResources); } MarkdownTemplate = Helpers.GetStringSetting("MarkdownTemplate", Configuration, MarkdownTemplate); MarkdownTheme = Helpers.GetStringSetting("MarkdownTheme", Configuration, MarkdownTheme); MarkdownSyntaxTheme = Helpers.GetStringSetting("MarkdownSyntaxTheme", Configuration, MarkdownSyntaxTheme); // Fix ups if (Extensions is null) { Extensions = string.Empty; } if (UseMarkdown) { if (!Extensions.Contains(".md")) { Extensions += ",.md"; } if (!Extensions.Contains(".markdown")) { Extensions += ",.markdown"; } if (!DefaultFiles.Contains(".md")) { DefaultFiles = DefaultFiles.Trim(',') + ",README.md,index.md"; } } return(true); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ServerConfig = new LiveReloadServerConfiguration(); ServerConfig.LoadFromConfiguration(Configuration); if (ServerConfig.UseLiveReload) { services.AddLiveReload(opt => { opt.FolderToMonitor = ServerConfig.WebRoot; opt.LiveReloadEnabled = ServerConfig.UseLiveReload; if (!string.IsNullOrEmpty(ServerConfig.Extensions)) { opt.ClientFileExtensions = ServerConfig.Extensions; } if (ServerConfig.UseMarkdown && !opt.ClientFileExtensions.Contains(".md", StringComparison.OrdinalIgnoreCase)) { opt.ClientFileExtensions += ",.md,.mkdown"; } }); } IMvcBuilder mvcBuilder = null; #if USE_RAZORPAGES if (ServerConfig.UseRazor) { mvcBuilder = services.AddRazorPages(opt => { opt.RootDirectory = "/"; }); } #endif if (ServerConfig.UseMarkdown) { services.AddMarkdown(config => { //var templatePath = Path.Combine(WebRoot, "markdown-themes/__MarkdownPageTemplate.cshtml"); //if (!File.Exists(templatePath)) // templatePath = Path.Combine(Environment.CurrentDirectory,"markdown-themes/__MarkdownPageTemplate.cshtml"); //else var templatePath = ServerConfig.MarkdownTemplate; templatePath = templatePath.Replace("\\", "/"); var folderConfig = config.AddMarkdownProcessingFolder("/", templatePath); // Optional configuration settings folderConfig.ProcessExtensionlessUrls = true; // default folderConfig.ProcessMdFiles = true; // default folderConfig.RenderTheme = ServerConfig.MarkdownTheme; folderConfig.SyntaxTheme = ServerConfig.MarkdownSyntaxTheme; }); // we have to force MVC in order for the controller routing to work mvcBuilder = services .AddMvc(); // copy Markdown Template and resources if it doesn't exist if (ServerConfig.CopyMarkdownResources) { CopyMarkdownTemplateResources(); } } // If Razor or Markdown are enabled we need custom folders if (mvcBuilder != null) { mvcBuilder.AddRazorRuntimeCompilation( opt => { opt.FileProviders.Clear(); opt.FileProviders.Add(new PhysicalFileProvider(ServerConfig.WebRoot)); opt.FileProviders.Add(new PhysicalFileProvider(Path.Combine(Startup.StartupPath, "templates"))); }); LoadPrivateBinAssemblies(mvcBuilder); } }