Example #1
0
        public static void OpenUrl(string url)
        {
            Process p = null;

            try
            {
                var psi = new ProcessStartInfo(url);
                p = Process.Start(psi);
            }
            catch
            {
                // hack because of this: https://github.com/dotnet/corefx/issues/10361
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
                    RuntimeInformation.OSDescription.Contains("microsoft-standard"))  // wsl
                {
                    url = url.Replace("&", "^&");
                    try
                    {
                        Process.Start(new ProcessStartInfo("cmd.exe", $"/c start {url}")
                        {
                            CreateNoWindow = true
                        });
                    }
                    catch
                    {
                        ColorConsole.WriteEmbeddedColorLine($"Open your browser at: [darkcyan]{url}[/darkcyan]");
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    p = Process.Start("xdg-open", url);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    p = Process.Start("open", url);
                }
                else
                {
                    ColorConsole.WriteEmbeddedColorLine($"Open your browser at: [darkcyan]{url}[/darkcyan]");
                }
            }

            p?.Dispose();
        }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IWebHostEnvironment env)
        {
            if (ServerConfig.UseLiveReload)
            {
                app.UseLiveReload();
            }

            ////if (env.IsDevelopment())
            ////    app.UseDeveloperExceptionPage();
            ////else

            app.UseExceptionHandler("/Error");

            if (ServerConfig.ShowUrls)
            {
                app.Use(DisplayRequestInfoMiddlewareHandler);
            }

            if (ServerConfig.UseMarkdown)
            {
                app.UseMarkdown();
            }

            app.UseDefaultFiles(new DefaultFilesOptions
            {
                FileProvider     = new PhysicalFileProvider(ServerConfig.WebRoot),
                DefaultFileNames = new List <string>(ServerConfig.DefaultFiles.Split(',', ';'))
            });

            // add static files to WebRoot and our templates folder which provides markdown templates
            // and potentially other library resources in the future

            var wrProvider = new PhysicalFileProvider(ServerConfig.WebRoot);
            var tpProvider = new PhysicalFileProvider(Path.Combine(Startup.StartupPath, "templates"));

            var extensionProvider = new FileExtensionContentTypeProvider();

            extensionProvider.Mappings.Add(".dll", "application/octet-stream");
            if (ServerConfig.AdditionalMimeMappings != null)
            {
                foreach (var map in ServerConfig.AdditionalMimeMappings)
                {
                    extensionProvider.Mappings[map.Key] = map.Value;
                }
            }

            var compositeProvider = new CompositeFileProvider(wrProvider, tpProvider);
            var staticFileOptions = new StaticFileOptions
            {
                //FileProvider = compositeProvider, //new PhysicalFileProvider(WebRoot),

                FileProvider        = new PhysicalFileProvider(ServerConfig.WebRoot),
                RequestPath         = new PathString(""),
                ContentTypeProvider = extensionProvider,
                DefaultContentType  = "application/octet-stream"
            };

            app.UseStaticFiles(staticFileOptions);


            if (ServerConfig.UseRazor || ServerConfig.UseMarkdown || !string.IsNullOrEmpty(ServerConfig.FolderNotFoundFallbackPath))
            {
                app.UseRouting();
            }

#if USE_RAZORPAGES
            if (ServerConfig.UseRazor)
            {
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapRazorPages();
                });
            }
#endif
            if (ServerConfig.UseMarkdown)
            {
                app.UseEndpoints(endpoints =>
                {
                    // We need MVC Routing for Markdown to work
                    endpoints.MapDefaultControllerRoute();
                });
            }

            //if (!string.IsNullOrEmpty(ServerConfig.FolderNotFoundFallbackPath))
            //{
            //    app.UseEndpoints(endpoints =>
            //    {
            //        endpoints.MapFallbackToFile("/index.html");
            //    });
            //}
            //app.Use(FallbackMiddlewareHandler);


            string headerLine = new string('-', Helpers.AppHeader.Length);
            Console.WriteLine(headerLine);
            ColorConsole.WriteLine(Helpers.AppHeader, ConsoleColor.Yellow);
            Console.WriteLine(headerLine);
            Console.WriteLine($"(c) West Wind Technologies, 2019-{DateTime.Now.Year}\r\n");

            string displayUrl = ServerConfig.GetHttpUrl();
            string hostUrl    = ServerConfig.GetHttpUrl(true);
            if (hostUrl == displayUrl || hostUrl.Contains("127.0.0.1"))
            {
                hostUrl = null;
            }
            else
            {
                hostUrl = $"  [darkgray](binding: {hostUrl})[/darkgray]";
            }

            ColorConsole.WriteEmbeddedColorLine($"Site Url     : [darkcyan]{ServerConfig.GetHttpUrl()}[/darkcyan] {hostUrl}");
            //ConsoleHelper.WriteLine(, ConsoleColor.DarkCyan);
            Console.WriteLine($"Web Root     : {ServerConfig.WebRoot}");
            Console.WriteLine($"Executable   : {Assembly.GetExecutingAssembly().Location}");
            Console.WriteLine($"Live Reload  : {ServerConfig.UseLiveReload}");
            if (ServerConfig.UseLiveReload)
            {
                Console.WriteLine(
                    $"  Extensions : {(string.IsNullOrEmpty(ServerConfig.Extensions) ? $"{(ServerConfig.UseRazor ? ".cshtml," : "")}.css,.js,.htm,.html,.ts" : ServerConfig.Extensions)}");
            }


#if USE_RAZORPAGES
            Console.WriteLine($"Use Razor    : {ServerConfig.UseRazor}");
#endif

            Console.WriteLine($"Use Markdown : {ServerConfig.UseMarkdown}");
            if (ServerConfig.UseMarkdown)
            {
                Console.WriteLine($"  Resources  : {ServerConfig.CopyMarkdownResources}");
                Console.WriteLine($"  Template   : {ServerConfig.MarkdownTemplate}");
                Console.WriteLine($"  Theme      : {ServerConfig.MarkdownTheme}");
                Console.WriteLine($"  SyntaxTheme: {ServerConfig.MarkdownSyntaxTheme}");
            }

            Console.WriteLine($"Show Urls    : {ServerConfig.ShowUrls}");
            Console.WriteLine($"Open Browser : {ServerConfig.OpenBrowser}");
            Console.WriteLine($"Default Pages: {ServerConfig.DefaultFiles}");
            Console.WriteLine($"Environment  : {env.EnvironmentName}");

            Console.WriteLine();
            ColorConsole.Write(Helpers.ExeName + " --help", ConsoleColor.DarkCyan);
            Console.WriteLine(" for start options...");
            Console.WriteLine();
            ColorConsole.WriteLine("Ctrl-C or Ctrl-Break to exit...", ConsoleColor.Yellow);

            Console.WriteLine("----------------------------------------------");

            var oldColor = Console.ForegroundColor;
            foreach (var assmbly in LoadedPrivateAssemblies)
            {
                var fname = Path.GetFileName(assmbly);
                ColorConsole.WriteEmbeddedColorLine("Additional Assembly: [green]" + fname + "[/green]");
            }

            foreach (var assmbly in FailedPrivateAssemblies)
            {
                var fname = Path.GetFileName(assmbly);
                ColorConsole.WriteLine("Failed Additional Assembly: " + fname, ConsoleColor.DarkGreen);
            }

            Console.ForegroundColor = oldColor;

            if (ServerConfig.OpenBrowser)
            {
                Helpers.OpenUrl(ServerConfig.GetHttpUrl());
            }
        }