1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added cdn config options and made bundle extension more generic.

This commit is contained in:
Uncled1023 2016-10-07 00:09:15 -07:00
parent aef087a890
commit 4ec159e753
5 changed files with 53 additions and 41 deletions

View File

@ -1,5 +1,6 @@
using System.Web; using System.Web;
using System.Web.Optimization; using System.Web.Optimization;
using Teknik.Configuration;
using Teknik.Helpers; using Teknik.Helpers;
namespace Teknik namespace Teknik
@ -9,19 +10,23 @@ namespace Teknik
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles) 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 #if !DEBUG
BundleTable.EnableOptimizations = true; BundleTable.EnableOptimizations = true;
#endif #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/bootstrap.css",
"~/Content/font-awesome.css", "~/Content/font-awesome.css",
"~/Content/common.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-{version}.js",
"~/Scripts/jquery.validate*", "~/Scripts/jquery.validate*",
"~/Scripts/bootstrap.js", "~/Scripts/bootstrap.js",
@ -29,16 +34,16 @@ namespace Teknik
"~/Scripts/common.js", "~/Scripts/common.js",
"~/Scripts/respond.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-{version}.js",
"~/Scripts/jquery.validate*")); "~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're // 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. // 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-*")); "~/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.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js")); "~/Scripts/PageDown/Markdown.Sanitizer.js"));
} }

View File

@ -52,11 +52,11 @@ namespace Teknik.Areas.Home
); );
// Register Style Bundles // 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")); "~/Areas/Home/Content/Home.css"));
// Register Script Bundles // 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.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js")); "~/Scripts/PageDown/Markdown.Sanitizer.js"));
} }

View File

@ -14,6 +14,7 @@ namespace Teknik.Configuration
private bool _DevEnvironment; private bool _DevEnvironment;
private bool _Migrate; private bool _Migrate;
private bool _UseCdn;
private string _Title; private string _Title;
private string _Description; private string _Description;
private string _Author; private string _Author;
@ -23,6 +24,7 @@ namespace Teknik.Configuration
private string _BitcoinAddress; private string _BitcoinAddress;
private string _Salt1; private string _Salt1;
private string _Salt2; private string _Salt2;
private string _CdnHost;
private UserConfig _UserConfig; private UserConfig _UserConfig;
private ContactConfig _ContactConfig; private ContactConfig _ContactConfig;
private EmailConfig _EmailConfig; private EmailConfig _EmailConfig;
@ -38,19 +40,21 @@ namespace Teknik.Configuration
private DatabaseConfig _DatabaseConfig; private DatabaseConfig _DatabaseConfig;
private PiwikConfig _PiwikConfig; private PiwikConfig _PiwikConfig;
public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } } public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
public bool Migrate { get { return _Migrate; } set { _Migrate = value; } } public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
public bool UseCdn { get { return _UseCdn; } set { _UseCdn = value; } }
// Site Information // Site Information
public string Title { get { return _Title; } set { _Title = value; } } public string Title { get { return _Title; } set { _Title = value; } }
public string Description { get { return _Description; } set { _Description = value; } } public string Description { get { return _Description; } set { _Description = value; } }
public string Author { get { return _Author; } set { _Author = value; } } public string Author { get { return _Author; } set { _Author = value; } }
public string Host { get { return _Host; } set { _Host = value; } } public string Host { get { return _Host; } set { _Host = value; } }
public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } } public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } } public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } }
public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } } public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } } public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } } public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
public string CdnHost { get { return _CdnHost; } set { _CdnHost = value; } }
// User Configuration // User Configuration
public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } } public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
@ -108,6 +112,7 @@ namespace Teknik.Configuration
{ {
DevEnvironment = false; DevEnvironment = false;
Migrate = false; Migrate = false;
UseCdn = false;
Title = string.Empty; Title = string.Empty;
Description = string.Empty; Description = string.Empty;
Author = string.Empty; Author = string.Empty;
@ -117,6 +122,7 @@ namespace Teknik.Configuration
BitcoinAddress = string.Empty; BitcoinAddress = string.Empty;
Salt1 = string.Empty; Salt1 = string.Empty;
Salt2 = string.Empty; Salt2 = string.Empty;
CdnHost = string.Empty;
UserConfig = new UserConfig(); UserConfig = new UserConfig();
EmailConfig = new EmailConfig(); EmailConfig = new EmailConfig();
ContactConfig = new ContactConfig(); ContactConfig = new ContactConfig();

View File

@ -53,6 +53,7 @@ namespace Teknik
protected void Application_EndRequest(object sender, EventArgs e) protected void Application_EndRequest(object sender, EventArgs e)
{ {
Config config = Config.Load();
HttpContext context = HttpContext.Current; HttpContext context = HttpContext.Current;
// Set the generation time in the header // Set the generation time in the header
@ -64,9 +65,9 @@ namespace Teknik
context.Response.AppendHeader("GenerationTime", elapsedTime); context.Response.AppendHeader("GenerationTime", elapsedTime);
// Allow this domain // Allow this domain, or everything if local
string origin = context.Request.Headers.Get("Origin"); string origin = (Request.IsLocal) ? "*" : context.Request.Headers.Get("Origin");
if (!string.IsNullOrEmpty(origin) && !Request.IsLocal) if (!string.IsNullOrEmpty(origin))
{ {
context.Response.AppendHeader("Access-Control-Allow-Origin", origin); context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
} }

View File

@ -1,6 +1,4 @@
using Microsoft.WindowsAzure.Storage; using System;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
@ -8,47 +6,49 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
using System.Web.Optimization; using System.Web.Optimization;
using Teknik.Configuration;
namespace Teknik.Helpers namespace Teknik.Helpers
{ {
public class AzureScriptBundle : Bundle public class CdnScriptBundle : Bundle
{ {
public AzureScriptBundle(string virtualPath, string cdnHost, string subdomain) public CdnScriptBundle(string virtualPath, string cdnHost)
: base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } }) : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform { CdnHost = cdnHost } })
{ {
ConcatenationToken = ";"; ConcatenationToken = ";";
} }
} }
public class AzureStyleBundle : Bundle public class CdnStyleBundle : Bundle
{ {
public AzureStyleBundle(string virtualPath, string cdnHost, string subdomain) public CdnStyleBundle(string virtualPath, string cdnHost)
: base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } }) : 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 CdnHost { get; set; }
public string Subdomain { get; set; } static CdnBundleTransform()
static AzureBundleTransform()
{ {
} }
public virtual void Process(BundleContext context, BundleResponse response) 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) if (!context.BundleCollection.UseCdn)
{ {
return; 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; return;
} }
@ -56,7 +56,7 @@ namespace Teknik.Helpers
using (var hashAlgorithm = SHA256.CreateHashAlgorithm()) using (var hashAlgorithm = SHA256.CreateHashAlgorithm())
{ {
var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content))); 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);
} }
} }
} }