diff --git a/Teknik/App_Start/BundleConfig.cs b/Teknik/App_Start/BundleConfig.cs index f5975f0..cc457d5 100644 --- a/Teknik/App_Start/BundleConfig.cs +++ b/Teknik/App_Start/BundleConfig.cs @@ -1,5 +1,6 @@ using System.Web; using System.Web.Optimization; +using Teknik.Configuration; using Teknik.Helpers; namespace Teknik @@ -9,19 +10,23 @@ namespace Teknik // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { - bundles.UseCdn = true; + // Load the config options + Config config = Config.Load(); - BundleTable.EnableOptimizations = true; + // Set if we are using Cdn + bundles.UseCdn = config.UseCdn; + + BundleTable.EnableOptimizations = false; #if !DEBUG BundleTable.EnableOptimizations = true; #endif - bundles.Add(new AzureStyleBundle("~/Content/Common", "https://cdn.teknik.io", "www").Include( + bundles.Add(new CdnStyleBundle("~/Content/Common", config.CdnHost).Include( "~/Content/bootstrap.css", "~/Content/font-awesome.css", "~/Content/common.css")); - bundles.Add(new AzureScriptBundle("~/bundles/common", "https://cdn.teknik.io", "www").Include( + bundles.Add(new CdnScriptBundle("~/bundles/common", config.CdnHost).Include( "~/Scripts/jquery-{version}.js", "~/Scripts/jquery.validate*", "~/Scripts/bootstrap.js", @@ -29,16 +34,16 @@ namespace Teknik "~/Scripts/common.js", "~/Scripts/respond.js")); - bundles.Add(new AzureScriptBundle("~/bundles/jquery", "https://cdn.teknik.io", "www").Include( + bundles.Add(new CdnScriptBundle("~/bundles/jquery", config.CdnHost).Include( "~/Scripts/jquery-{version}.js", "~/Scripts/jquery.validate*")); // Use the development version of Modernizr to develop with and learn from. Then, when you're // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. - bundles.Add(new AzureScriptBundle("~/bundles/modernizr", "https://cdn.teknik.io", "www").Include( + bundles.Add(new CdnScriptBundle("~/bundles/modernizr", config.CdnHost).Include( "~/Scripts/modernizr-*")); - bundles.Add(new AzureScriptBundle("~/bundles/markdown", "https://cdn.teknik.io", "www").Include( + bundles.Add(new CdnScriptBundle("~/bundles/markdown", config.CdnHost).Include( "~/Scripts/PageDown/Markdown.Converter.js", "~/Scripts/PageDown/Markdown.Sanitizer.js")); } diff --git a/Teknik/Areas/Home/HomeAreaRegistration.cs b/Teknik/Areas/Home/HomeAreaRegistration.cs index d497798..8610fda 100644 --- a/Teknik/Areas/Home/HomeAreaRegistration.cs +++ b/Teknik/Areas/Home/HomeAreaRegistration.cs @@ -52,11 +52,11 @@ namespace Teknik.Areas.Home ); // Register Style Bundles - BundleTable.Bundles.Add(new AzureStyleBundle("~/Content/home", "https://cdn.teknik.io", "www").Include( + BundleTable.Bundles.Add(new CdnStyleBundle("~/Content/home", config.CdnHost).Include( "~/Areas/Home/Content/Home.css")); // Register Script Bundles - BundleTable.Bundles.Add(new AzureScriptBundle("~/bundles/home", "https://cdn.teknik.io", "www").Include( + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/home", config.CdnHost).Include( "~/Scripts/PageDown/Markdown.Converter.js", "~/Scripts/PageDown/Markdown.Sanitizer.js")); } diff --git a/Teknik/Configuration/Config.cs b/Teknik/Configuration/Config.cs index 8537add..df790b3 100644 --- a/Teknik/Configuration/Config.cs +++ b/Teknik/Configuration/Config.cs @@ -14,6 +14,7 @@ namespace Teknik.Configuration private bool _DevEnvironment; private bool _Migrate; + private bool _UseCdn; private string _Title; private string _Description; private string _Author; @@ -23,6 +24,7 @@ namespace Teknik.Configuration private string _BitcoinAddress; private string _Salt1; private string _Salt2; + private string _CdnHost; private UserConfig _UserConfig; private ContactConfig _ContactConfig; private EmailConfig _EmailConfig; @@ -38,19 +40,21 @@ namespace Teknik.Configuration private DatabaseConfig _DatabaseConfig; private PiwikConfig _PiwikConfig; - public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } } - public bool Migrate { get { return _Migrate; } set { _Migrate = value; } } + public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } } + public bool Migrate { get { return _Migrate; } set { _Migrate = value; } } + public bool UseCdn { get { return _UseCdn; } set { _UseCdn = value; } } // Site Information - public string Title { get { return _Title; } set { _Title = value; } } - public string Description { get { return _Description; } set { _Description = value; } } - public string Author { get { return _Author; } set { _Author = value; } } - public string Host { get { return _Host; } set { _Host = value; } } - public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } } - public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } } - public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } } - public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } } - public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } } + public string Title { get { return _Title; } set { _Title = value; } } + public string Description { get { return _Description; } set { _Description = value; } } + public string Author { get { return _Author; } set { _Author = value; } } + public string Host { get { return _Host; } set { _Host = value; } } + public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } } + public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } } + public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } } + public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } } + public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } } + public string CdnHost { get { return _CdnHost; } set { _CdnHost = value; } } // User Configuration public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } } @@ -108,6 +112,7 @@ namespace Teknik.Configuration { DevEnvironment = false; Migrate = false; + UseCdn = false; Title = string.Empty; Description = string.Empty; Author = string.Empty; @@ -117,6 +122,7 @@ namespace Teknik.Configuration BitcoinAddress = string.Empty; Salt1 = string.Empty; Salt2 = string.Empty; + CdnHost = string.Empty; UserConfig = new UserConfig(); EmailConfig = new EmailConfig(); ContactConfig = new ContactConfig(); diff --git a/Teknik/Global.asax.cs b/Teknik/Global.asax.cs index 2504534..c8f8932 100644 --- a/Teknik/Global.asax.cs +++ b/Teknik/Global.asax.cs @@ -53,6 +53,7 @@ namespace Teknik protected void Application_EndRequest(object sender, EventArgs e) { + Config config = Config.Load(); HttpContext context = HttpContext.Current; // Set the generation time in the header @@ -64,9 +65,9 @@ namespace Teknik context.Response.AppendHeader("GenerationTime", elapsedTime); - // Allow this domain - string origin = context.Request.Headers.Get("Origin"); - if (!string.IsNullOrEmpty(origin) && !Request.IsLocal) + // Allow this domain, or everything if local + string origin = (Request.IsLocal) ? "*" : context.Request.Headers.Get("Origin"); + if (!string.IsNullOrEmpty(origin)) { context.Response.AppendHeader("Access-Control-Allow-Origin", origin); } diff --git a/Teknik/Helpers/BundleExtensions.cs b/Teknik/Helpers/BundleExtensions.cs index 8706ad6..e6d957d 100644 --- a/Teknik/Helpers/BundleExtensions.cs +++ b/Teknik/Helpers/BundleExtensions.cs @@ -1,6 +1,4 @@ -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Blob; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; @@ -8,47 +6,49 @@ using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Optimization; +using Teknik.Configuration; namespace Teknik.Helpers { - public class AzureScriptBundle : Bundle + public class CdnScriptBundle : Bundle { - public AzureScriptBundle(string virtualPath, string cdnHost, string subdomain) - : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } }) + public CdnScriptBundle(string virtualPath, string cdnHost) + : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) { ConcatenationToken = ";"; } } - public class AzureStyleBundle : Bundle + public class CdnStyleBundle : Bundle { - public AzureStyleBundle(string virtualPath, string cdnHost, string subdomain) - : base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } }) + public CdnStyleBundle(string virtualPath, string cdnHost) + : base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) { } } - public class AzureBundleTransform : IBundleTransform + public class CdnBundleTransform : IBundleTransform { public string CdnHost { get; set; } - public string Subdomain { get; set; } - - static AzureBundleTransform() + static CdnBundleTransform() { } public virtual void Process(BundleContext context, BundleResponse response) { - var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/'); - var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath); - + + // Don't continue if we aren't using a CDN if (!context.BundleCollection.UseCdn) { return; } - if (string.IsNullOrEmpty(CdnHost) || string.IsNullOrEmpty(Subdomain)) + // Get the directory and filename for the bundle + var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/'); + var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath); + + if (string.IsNullOrEmpty(CdnHost)) { return; } @@ -56,7 +56,7 @@ namespace Teknik.Helpers using (var hashAlgorithm = SHA256.CreateHashAlgorithm()) { var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content))); - context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?sub={3}&v={4}", CdnHost.TrimEnd('/'), dir, file, Subdomain, hash); + context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?v={3}", CdnHost.TrimEnd('/'), dir, file, hash); } } }