Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Get Configuration Settings
            UseLiveReload = StartupHelpers.GetLogicalSetting("LiveReloadEnabled", Configuration);
            UseRazor      = StartupHelpers.GetLogicalSetting("UseRazor", Configuration);

            WebRoot = Configuration["WebRoot"];
            if (string.IsNullOrEmpty(WebRoot))
            {
                WebRoot = Environment.CurrentDirectory;
            }
            else
            {
                WebRoot = Path.GetFullPath(WebRoot, Environment.CurrentDirectory);
            }

            if (UseLiveReload)
            {
                services.AddLiveReload(opt =>
                {
                    opt.FolderToMonitor   = WebRoot;
                    opt.LiveReloadEnabled = UseLiveReload;

                    var extensions = Configuration["Extensions"];
                    if (!string.IsNullOrEmpty(extensions))
                    {
                        opt.ClientFileExtensions = extensions;
                    }
                });
            }


#if USE_RAZORPAGES
            if (UseRazor)
            {
                services.AddRazorPages(opt => { opt.RootDirectory = "/"; })
                .AddRazorRuntimeCompilation(
                    opt =>
                {
                    opt.FileProviders.Add(new PhysicalFileProvider(WebRoot));
                    LoadPrivateBinAssemblies(opt);
                });
            }
#endif
        }
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureWebHostDefaults(webBuilder =>
            {
                // Custom Config
                var config = new ConfigurationBuilder()
                             .AddJsonFile("LiveReloadServer.json", optional: true)
                             .AddEnvironmentVariables("LIVERELOADSERVER_")
                             .AddCommandLine(args)
                             .Build();


                webBuilder
                .UseConfiguration(config);

                var webRoot = config["WebRoot"];
                if (!string.IsNullOrEmpty(webRoot))
                {
                    webBuilder.UseWebRoot(webRoot);
                }


                string sport = config["Port"];
                int.TryParse(sport, out int port);
                if (port == 0)
                {
                    port = 5000;
                }

                bool useSsl = StartupHelpers.GetLogicalSetting("UseSsl", config);
                webBuilder.UseUrls($"http{(useSsl ? "s" : "")}://0.0.0.0:{port}");

                webBuilder
                .UseStartup <Startup>();
            }));
        }
Ejemplo n.º 3
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)
        {
            bool useSsl      = StartupHelpers.GetLogicalSetting("useSsl", Configuration);
            bool showUrls    = StartupHelpers.GetLogicalSetting("ShowUrls", Configuration);
            bool openBrowser = StartupHelpers.GetLogicalSetting("OpenBrowser", Configuration);

            string defaultFiles = Configuration["DefaultFiles"];

            if (string.IsNullOrEmpty(defaultFiles))
            {
                defaultFiles = "index.html,default.htm,default.html";
            }

            var strPort = Configuration["Port"];

            if (!int.TryParse(strPort, out Port))
            {
                Port = 5000;
            }

            if (UseLiveReload)
            {
                app.UseLiveReload();
            }

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            if (showUrls)
            {
                app.Use(async(context, next) =>
                {
                    var url =
                        $"{context.Request.Method}  {context.Request.Scheme}://{context.Request.Host}  {context.Request.Path}{context.Request.QueryString}";
                    Console.WriteLine(url);
                    await next();
                });
            }

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

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(WebRoot),
                RequestPath  = new PathString("")
            });

#if USE_RAZORPAGES
            if (UseRazor)
            {
                app.UseRouting();
                app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
            }
#endif
            var url        = $"http{(useSsl ? "s" : "")}://localhost:{Port}";
            var extensions = Configuration["Extensions"];

            string headerLine = new string('-', Program.AppHeader.Length);
            Console.WriteLine(headerLine);
            Console.WriteLine(Program.AppHeader);
            Console.WriteLine(headerLine);
            Console.WriteLine($"(c) West Wind Technologies, 2018-{DateTime.Now.Year}\r\n");
            Console.Write($"Site Url     : ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(url);
            Console.ResetColor();
            Console.WriteLine($"Web Root     : {WebRoot}");
            Console.WriteLine(
                $"Extensions   : {(string.IsNullOrEmpty(extensions) ? $"{(UseRazor ? ".cshtml," : "")},.css,.js,.htm,.html,.ts" : extensions)}");
            Console.WriteLine($"Live Reload  : {UseLiveReload}");

#if USE_RAZORPAGES
            Console.WriteLine($"Use Razor    : {UseRazor}");
#endif
            Console.WriteLine($"Show Urls    : {showUrls}");
            Console.WriteLine($"Open Browser : {openBrowser}");
            Console.WriteLine($"Default Pages: {defaultFiles}");

            Console.WriteLine();
            Console.WriteLine("'LiveReloadServer --help' for start options...");
            Console.WriteLine();
            Console.WriteLine("Ctrl-C or Ctrl-Break to exit...");

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

            var oldColor = Console.ForegroundColor;
            foreach (var assmbly in LoadedPrivateAssemblies)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Additional Assembly: " + assmbly);
            }
            foreach (var assmbly in FailedPrivateAssemblies)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed Additional Assembly: " + assmbly);
            }
            Console.ForegroundColor = oldColor;

            if (openBrowser)
            {
                StartupHelpers.OpenUrl(url);
            }
        }