2015-08-14 41 views
1

我是一個後端開發人員,負責處理包含一個ember前端的項目。我們的項目不尋常,因爲我們沒有生產線;我們編寫代碼並交付給客戶。客戶端只需要使用Microsoft Web堆棧,因此我們構建了這個ember項目,並將結果內容包裝到一個簡單的Visual Studio項目中,然後交給他們進行部署。在部署或運行​​時更改ember baseURL

此客戶端已經不幸讓我們在那顯然是不尋常的餘燼世界上的地位:

  • 客戶不願意安裝或執行任何的灰燼,CLI工具或命令
  • 客戶端使用不同測試和生產站點的目錄結構(即「http://staging.example.com/project」與「http://project.example.com/」)
  • 爲了履行上述要求,我們一直在爲兩種不同的環境提供單獨的構建。 但從長遠來看這也被認爲是不可接受的。

顯然,這是一個沒有人會以完美的眼光選擇的情況。這些限制中的大部分都沒有明確,直到爲時已晚,但實際上客戶不會改變它們。我要尋找一個技術解決方案,看起來像這樣:

  1. 我們開發燼
  2. 我們使用燼-CLI工具(或同等學歷)建立前端
  3. 我們包的成果轉化爲一個視覺工作室項目,其中可能包括web.config中的客戶端的測試環境
  4. 郵編及向客戶
  5. 基於在web.config中的設置,我們燼項目本身配置到:
    • 一套基本URL到適當的數值
    • 連接到位於環境特定的URL

爲了解決這個問題,我們的REST API,我一直在審查下列內容:

不幸的是,我個人並不熟悉餘燼。所以我不明白baseURL是如何真正被使用的,以及爲什麼在部署或運行​​時對ember進行參數化似乎很尷尬。我可以在哪裏學習如何將我們的工作融入可接受的部署流程?

是否可以通過將META標籤放到加載該網站的index.html上來將baseURL或ourBackendApiURL的值發送到ember?

回答

0

下面是最終解決了這個問題。基本上,我們使用由「ember build」創建的index.html,並在運行時找到了從environment.js傳遞給javascript的元標記。通過在index.cshtml中生成元標記,我們可以使用web.config中指定的值。

現在,當我們構建應用程序切換時,我們簡單地忽略它生成的index.html,但將其餘資源添加到預製的Visual Studio Web項目中。該項目可能配置爲測試或生產。

首先,我在Visual Studio中創建了一個空的Web應用程序。在web.config中,我添加了這個:

<appSettings> 
    <add key="hostName" value="https://apps.example.com/" /> 
    <add key="baseURL" value="/projectDir/" /> 
    <add key="serverNamespace" value="api" /> 
    <add key="imgSrc" value="https://images.example.com" /> 
</appSettings>  

,然後index.cshtml被寫入利用這些值:

@{ 
    if (!Request.Url.AbsoluteUri.EndsWith("/")) 
    { 
     Response.Redirect(Request.Url.AbsoluteUri + "/"); 
    }  

    var configStr = @"{{ 
    ""modulePrefix"": ""foo-bar"", 
    ""podModulePrefix"": ""foo-bar/pods"", 
    ""environment"": ""production"", 
    ""baseURL"": ""{3}"", 
    ""locationType"": ""hash"", 
    ""namespace"": ""{0}"", 
    ""host"": ""{1}"", 
    ""foo-auth-host"": ""{1}"", 
    ""APP"": {{ 
     ""name"": ""foo-bar"", 
     ""version"": ""0.0.0"" 
    }}, 
    ""contentSecurityPolicy"": {{ 
     ""default-src"": ""'none'"", 
     ""script-src"": ""'self'"", 
     ""font-src"": ""'self'"", 
     ""img-src"": ""'self' {2}"", 
     ""style-src"": ""'self'"", 
     ""media-src"": ""'self'"", 
     ""connect-src"": ""'self' {1}"" 
    }}, 
    ""simple-auth"": {{ 
     ""authorizer"": ""authorizer:foo"", 
     ""routeAfterAuthentication"": ""/"", 
     ""store"": ""simple-auth-session-store:local-storage"" 
    }}, 
    ""contentSecurityPolicyHeader"": ""Content-Security-Policy-Report-Only"", 
    ""exportApplicationGlobal"": false 
}}"; 
    var serverNamespace = System.Web.Configuration.WebConfigurationManager.AppSettings["serverNamespace"]; 
    var hostName = System.Web.Configuration.WebConfigurationManager.AppSettings["hostName"]; 
    var imgSrc = System.Web.Configuration.WebConfigurationManager.AppSettings["imgSrc"]; 
    var baseUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["baseURL"]; 

    var configStrPopulated = string.Format(configStr, serverNamespace, hostName, imgSrc, baseUrl); 
    var configStrCompacted = System.Text.RegularExpressions.Regex.Replace(configStrPopulated, @"\s+", ""); 
    var configStrEncoded = HttpUtility.UrlEncode(configStrCompacted); 
}<!DOCTYPE html> 
<html> 
<head> 
    ... 
    <meta name="foo-bar/config/environment" content="@configStrEncoded" /> 
    ... 
</head> 
<body> 
    <script src="assets/vendor.js"></script> 
    <script src="assets/foo-bar.js"></script> 
</body> 
</html> 
0

您可以通過在您的environment.js中設置baseURL: null來擺脫baseURL。這將防止ember-cli生成基本標記。另外,如果您使用的是位置哈希(http://example.com/index.html#/path-to-route),我建議在environment.js中設置locationType: 'hash'。如果缺省設置位於不在Web服務器的根目錄中,那麼您將遇到問題。

0

然後,當解釋說未傳達的要求確實是阻礙性的並且需要額外的工時時,他們當然明白你需要記錄下來? ;)

你可以嘗試只添加此:

<base href="project"> 

應自動拾取。否則,在app.js您可以覆蓋baseURLrootURL屬性:

var Router = Ember.Router.extend({ 
    location: 'history', 
    rootURL: function(){ 
    return "/random/"; 
    }.property(), 
    baseURL: function(){ 
    return "/more-random/"; 
    }.property() 
}); 
+0

感謝包!當我搜索基地標籤(我從來沒有見過),我發現這個:https://github.com/ember-cli/ember-cli/issues/2633和http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag,這讓我非常緊張。但這與我想到的解決方案非常接近。我將不得不與正在構建項目餘燼部分的隊友交談。 – phil

+0

@phil哇這樣的戲劇化在那個線程。我非常清楚''標籤是如何工作的,它的字面意思就是這種情況。弄清楚你的特定情況下是否有阻止你使用它的事情,這只是一個工具。 :) –

+0

我不認爲我們會,我們不使用SVG或任何不尋常的東西。所以我認爲在這種情況下,它會按照你的建議工作。再次感謝! – phil

0

我會考慮的東西,如ember-cli-dotenv去,所以你可以指定你在編譯/運行基本URL。他們需要做的只是在項目根目錄中的.env文件中指定BASE_URL=https://whatever-they-want.crazyclient.com(從版本控制中忽略),或者在實際環境中指定BASE_URL。這允許你和他們使用你選擇的服務器。

燼-CLI實現:

// Brocfile.js or ember-cli-build.js: 
var app = new EmberApp({ 
    dotEnv: { 
    clientAllowedKeys: ['ROOT_URL', 'BASE_URL'] 
    } 
}); 

// pre-generated config from ember-cli 
module.exports = app.toTree(); 

然後:

// config/environment.js 
module.exports = function(environment){ 
    return { 
    ROOT_URL: process.env.ROOT_URL, 
    BASE_URL: process.env.BASE_URL, 
    } 
}; 

最後:

var Router = Ember.Router.extend({ 
    location: 'history', 
    rootURL: function(){ 
    return ENV['ROOT_URL'] || 'somedefault'; 
    }.property(), 
    baseURL: function(){ 
    return ENV['BASE_URL'] || 'someotherdefault'; 
    }.property() 
});