/// <summary> /// Render first html GET request. /// </summary> private static async Task <string> WebsiteServerSideRenderingAsync(HttpContext context, AppSelector appSelector, AppJson appJson) { string url; if (UtilServer.IsIssServer) { // Running on IIS Server. url = context.Request.IsHttps ? "https://" : "http://"; url += context.Request.Host.ToUriComponent() + "/Framework/Framework.Angular/server/main.js"; // Url of server side rendering when running on IIS Server } else { // Running in Visual Studio. url = "http://localhost:4000/"; // Url of server side rendering when running in Visual Studio } // Process AppJson string jsonClient = await appSelector.ProcessAsync(context, appJson); // Process (For first server side rendering) // Server side rendering POST. string folderNameServer = appSelector.Website.FolderNameServerGet(appSelector.ConfigServer, "Application.Server/Framework/"); string serverSideRenderView = UtilFramework.FolderNameParse(folderNameServer, "/index.html"); serverSideRenderView = HttpUtility.UrlEncode(serverSideRenderView); url += "?view=" + serverSideRenderView; bool isServerSideRendering = ConfigServer.Load().IsServerSideRendering; string indexHtml; if (isServerSideRendering) { // index.html server side rendering indexHtml = await UtilServer.WebPost(url, jsonClient); // Server side rendering POST. http://localhost:50919/Framework/Framework.Angular/server.js?view=Application.Website%2fDefault%2findex.html } else { // index.html serve directly string fileName = UtilServer.FolderNameContentRoot() + UtilFramework.FolderNameParse(appSelector.Website.FolderNameServerGet(appSelector.ConfigServer, "Application.Server/"), "/index.html"); indexHtml = UtilFramework.FileLoad(fileName); } // Set jsonBrowser in index.html. string scriptFind = "</app-root>"; //" <script>var jsonBrowser={}</script>"; // For example Html5Boilerplate build process renames var jsonBrowser to a. string scriptReplace = "</app-root><script>var jsonBrowser = " + jsonClient + "</script>"; if (isServerSideRendering) { indexHtml = UtilFramework.Replace(indexHtml, scriptFind, scriptReplace); } // Add Angular scripts scriptFind = "</body></html>"; scriptReplace = "<script src=\"runtime.js\" defer></script><script src=\"polyfills.js\" defer></script><script src=\"main.js\" defer></script>" + "</body></html>"; indexHtml = UtilFramework.Replace(indexHtml, scriptFind, scriptReplace); return(indexHtml); }
/// <summary> /// Build all layout Websites. For example: "Application.Website/LayoutDefault" /// </summary> private void BuildWebsite() { var configCli = ConfigCli.Load(); // Ensure FolderNameNpmBuild is defined once only in ConfigCli.json. ConfigCliWebsite configCliWebsite = configCli.WebsiteList.GroupBy(item => item.FolderNameNpmBuild.ToLower()).Where(group => group.Count() > 1).FirstOrDefault()?.FirstOrDefault(); UtilFramework.Assert(configCliWebsite == null, string.Format("ConfigCli.json Website defined more than once. Use DomainNameList instead! (FolderNameNpmBuild={0})", configCliWebsite?.FolderNameNpmBuild)); // Delete folder Application.Server/Framework/Application.Website/ string folderNameApplicationWebsite = UtilFramework.FolderName + "Application.Server/Framework/Application.Website/"; UtilCli.FolderDelete(folderNameApplicationWebsite); foreach (var website in configCli.WebsiteList) { Console.WriteLine(string.Format("### Build Website (Begin) - {0}", website.DomainNameListToString())); // Delete dist folder string folderNameDist = UtilFramework.FolderNameParse(website.FolderNameDist); UtilFramework.Assert(folderNameDist != null); UtilCli.FolderDelete(folderNameDist); // npm run build BuildWebsiteNpm(website); string folderNameServer = UtilFramework.FolderNameParse("Application.Server/Framework/" + website.FolderNameDist); UtilFramework.Assert(folderNameServer != null, "FolderNameServer can not be null!"); UtilFramework.Assert(folderNameServer.StartsWith("Application.Server/Framework/Application.Website/"), "FolderNameServer has to start with 'Application.Server/Framework/Application.Website/'!"); // Copy dist folder string folderNameSource = UtilFramework.FolderName + folderNameDist; string folderNameDest = UtilFramework.FolderName + folderNameServer; if (!UtilCli.FolderNameExist(folderNameSource)) { throw new Exception(string.Format("Folder does not exist! ({0})", folderNameDest)); } // Layout file main.js and Angular file main.js // Prevent for example two main.js. Angular js can not be overridden by layout Website // Application.Website/LayoutDefault/dist/main.js // Application.Server/Framework/Framework.Angular/browser/main.js var fileNameList = new string[] { "runtime.js", "polyfills.js", "main.js" }; foreach (var fileName in fileNameList) { var fileNameFull = folderNameSource + fileName; if (File.Exists(fileNameFull)) { throw new Exception(string.Format("File conflicts with Angular! See also: https://webpack.js.org/configuration/output/ ({0})", fileNameFull)); } } UtilCli.FolderDelete(folderNameDest); UtilFramework.Assert(!UtilCli.FolderNameExist(folderNameDest)); UtilCli.FolderCopy(folderNameSource, folderNameDest, "*.*", true); UtilFramework.Assert(UtilCli.FolderNameExist(folderNameDest)); Console.WriteLine(string.Format("### Build Website (End) - {0}", website.DomainNameListToString())); } }
/// <summary> /// Execute "npm run build" command. /// </summary> private static void BuildWebsiteNpm(ConfigCliWebsite website) { string folderNameNpmBuild = UtilFramework.FolderNameParse(website.FolderNameNpmBuild); if (UtilFramework.StringNull(folderNameNpmBuild) != null) { string folderName = UtilFramework.FolderName + folderNameNpmBuild; UtilCli.Npm(folderName, "install --loglevel error"); // --loglevel error prevent writing to STDERR "npm WARN optional SKIPPING OPTIONAL DEPENDENCY" UtilCli.Npm(folderName, "run build"); } }
/// <summary> /// Browser GET request to download file. /// </summary> private static async Task <bool> FileDownloadAsync(HttpContext context, string path, byte[] data) { bool result = false; if (data != null) { UtilFramework.Assert(data != null); string fileName = UtilFramework.FolderNameParse(null, path); context.Response.ContentType = UtilServer.ContentType(fileName); await context.Response.Body.WriteAsync(data, 0, data.Length); result = true; } return(result); }
/// <summary> /// Returns true, if file found in folder "Application.Server/Framework/Application.Website/" /// </summary> private async Task <bool> WebsiteFileAsync(HttpContext context, string path, AppSelector appSelector) { bool result = false; if (UtilServer.NavigatePathIsFileName(path)) { // Serve fileName string fileName = UtilServer.FolderNameContentRoot() + UtilFramework.FolderNameParse(appSelector.Website.FolderNameServerGet(appSelector.ConfigServer, "Application.Server/"), path); if (File.Exists(fileName)) { context.Response.ContentType = UtilServer.ContentType(fileName); await context.Response.SendFileAsync(fileName); return(true); } } return(result); }
/// <summary> /// Build all layout Websites. For example: "Application.Website/LayoutDefault" /// </summary> private void BuildWebsite() { var configCli = ConfigCli.Load(); // Ensure FolderNameNpmBuild is defined once only in ConfigCli.json. ConfigCliWebsite configCliWebsite = configCli.WebsiteList.GroupBy(item => item.FolderNameNpmBuild.ToLower()).Where(group => group.Count() > 1).FirstOrDefault()?.FirstOrDefault(); UtilFramework.Assert(configCliWebsite == null, string.Format("ConfigCli.json Website defined more than once. Use DomainNameList instead! (FolderNameNpmBuild={0})", configCliWebsite?.FolderNameNpmBuild)); // Delete folder Application.Server/Framework/Application.Website/ string folderNameApplicationWebsite = UtilFramework.FolderName + "Application.Server/Framework/Application.Website/"; UtilCli.FolderDelete(folderNameApplicationWebsite); foreach (var website in configCli.WebsiteList) { Console.WriteLine(string.Format("### Build Website (Begin) - {0}", website.DomainNameListToString())); // Delete dist folder string folderNameDist = UtilFramework.FolderNameParse(website.FolderNameDist); UtilFramework.Assert(folderNameDist != null); UtilCli.FolderDelete(folderNameDist); // npm run build BuildWebsiteNpm(website); string folderNameServer = UtilFramework.FolderNameParse(website.FolderNameServerGet(configCli)); UtilFramework.Assert(folderNameServer != null, "FolderNameServer can not be null!"); UtilFramework.Assert(folderNameServer.StartsWith("Application.Server/Framework/Application.Website/"), "FolderNameServer has to start with 'Application.Server/Framework/Application.Website/'!"); // Copy dist folder string folderNameSource = UtilFramework.FolderName + folderNameDist; string folderNameDest = UtilFramework.FolderName + folderNameServer; if (!UtilCli.FolderNameExist(folderNameSource)) { throw new Exception(string.Format("Folder does not exist! ({0})", folderNameDest)); } UtilCli.FolderDelete(folderNameDest); UtilFramework.Assert(!UtilCli.FolderNameExist(folderNameDest)); UtilCli.FolderCopy(folderNameSource, folderNameDest, "*.*", true); UtilFramework.Assert(UtilCli.FolderNameExist(folderNameDest)); Console.WriteLine(string.Format("### Build Website (End) - {0}", website.DomainNameListToString())); } }
/// <summary> /// Divert request to "Application.Server/Framework/Application.Website/" /// </summary> private static async Task <bool> WebsiteServerSideRenderingAsync(HttpContext context, string navigatePath, AppSelector appSelector, AppJson appJson) { bool result = false; // FolderNameServer string folderNameServer = appSelector.Website.FolderNameServerGet(appSelector.ConfigServer, "Application.Server/"); // FolderName string folderName = UtilServer.FolderNameContentRoot() + folderNameServer; if (!Directory.Exists(folderName)) { throw new Exception(string.Format("Folder does not exis! Make sure cli build command did run. ({0})", folderName)); } // Index.html string pathIndexHtml = navigatePath; if (!UtilServer.NavigatePathIsFileName(navigatePath)) { pathIndexHtml += "index.html"; } // FileName string fileName = UtilFramework.FolderNameParse(folderName, pathIndexHtml); if (File.Exists(fileName)) { if (fileName.EndsWith(".html") && UtilFramework.StringNull(appSelector.AppTypeName) != null) { context.Response.ContentType = UtilServer.ContentType(fileName); string htmlIndex = await WebsiteServerSideRenderingAsync(context, appSelector, appJson); await context.Response.WriteAsync(htmlIndex); result = true; } } return(result); }
private void ArgumentWebsite() { ConfigCli configCli = ConfigCli.Load(); // Input DomainName Console.WriteLine("Enter domain name. For example: 'example.com' or empty for default website:"); Console.Write(">"); string domainName = Console.ReadLine(); // Input AppTypeName Console.WriteLine("Enter AppTypeName. For example: 'Application.AppMain, Application':"); Console.Write(">"); string appTypeName = Console.ReadLine(); if (Type.GetType(appTypeName) == null) { UtilCli.ConsoleWriteLineColor(string.Format("Type not found! ({0})", appTypeName), ConsoleColor.Yellow); } // Input FolderName Console.WriteLine("Enter npm build folder name. Or empty if no build. For example: 'Application.Website/LayoutDefault/'. In this folder ci calls npm install; npm build;"); Console.Write(">"); string folderNameNpmBuild = Console.ReadLine(); folderNameNpmBuild = UtilFramework.FolderNameParse(folderNameNpmBuild); string folderNameNpmBuildCheck = UtilFramework.FolderName + folderNameNpmBuild; if (!Directory.Exists(folderNameNpmBuildCheck)) { UtilCli.ConsoleWriteLineColor(string.Format("Folder does not exist! ({0})", folderNameNpmBuild), ConsoleColor.Yellow); } // Input FolderNameDist Console.WriteLine("Enter dist folder name. For example 'Application.Website/LayoutDefault/dist/'. Content of this folder is copied to FolderNameServer"); Console.Write(">"); string folderNameDist = Console.ReadLine(); folderNameDist = UtilFramework.FolderNameParse(folderNameDist); string folderNameDistCheck = UtilFramework.FolderName + folderNameDist; if (!Directory.Exists(folderNameDistCheck)) { UtilCli.ConsoleWriteLineColor(string.Format("Folder does not exist! ({0})", folderNameDist), ConsoleColor.Yellow); } // Add Website ConfigCliWebsite website = new ConfigCliWebsite(); website.DomainNameList = new List <ConfigCliWebsiteDomain>(); website.DomainNameList.Add(new ConfigCliWebsiteDomain() { EnvironmentName = configCli.EnvironmentNameGet(), DomainName = domainName, AppTypeName = appTypeName }); website.FolderNameNpmBuild = folderNameNpmBuild; website.FolderNameDist = folderNameDist; configCli.WebsiteList.Add(website); ConfigCli.Save(configCli); }