Beispiel #1
0
        /// <summary>
        /// Responsible for writing the actual request display out to the console.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sw"></param>
        /// <param name="originalPath"></param>
        private void WriteConsoleLogDisplay(HttpContext context, Stopwatch sw, string originalPath)
        {
            var url =
                $"{context.Request.Method}  {context.Request.Scheme}://{context.Request.Host}  {originalPath}{context.Request.QueryString}";

            url = url.PadRight(80, ' ');

            var  ct        = context.Response.ContentType;
            bool isPrimary = ct != null &&
                             (ct.StartsWith("text/html") ||
                              ct.StartsWith("text/plain") ||
                              ct.StartsWith("application/json") ||
                              ct.StartsWith("text/xml"));


            var saveColor = Console.ForegroundColor;

            var status = context.Response.StatusCode;

            if (ct == null) // no response
            {
                ColorConsole.Write(url + " ", ConsoleColor.Red);
                isPrimary = true;
            }
            else if (isPrimary)
            {
                ColorConsole.Write(url + " ", ConsoleColor.Gray);
            }
            else
            {
                ColorConsole.Write(url + " ", ConsoleColor.DarkGray);
            }


            if (status >= 200 && status < 400)
            {
                ColorConsole.Write(status.ToString(),
                                   isPrimary ? ConsoleColor.Green : ConsoleColor.DarkGreen);
            }
            else if (status == 401)
            {
                ColorConsole.Write(status.ToString(),
                                   isPrimary ? ConsoleColor.Yellow : ConsoleColor.DarkYellow);
            }
            else if (status >= 400)
            {
                ColorConsole.Write(status.ToString(), isPrimary ? ConsoleColor.Red : ConsoleColor.DarkRed);
            }

            sw.Stop();
            ColorConsole.WriteLine($" {sw.ElapsedMilliseconds:n0}ms".PadLeft(8), ConsoleColor.DarkGray);


            Console.ForegroundColor = saveColor;
        }
Beispiel #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());
            }
        }