tag:support.appharbor.com,2010-11-23:/discussions/problems/381-pushing-same-site-to-separate-environmentsAppHarbor: Discussion 2011-04-08T03:50:28Ztag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T01:50:26Z2011-04-06T01:50:26ZPushing same site to separate environments<div><p>There's an open <a href=
"http://feedback.appharbor.com/forums/95687-general/suggestions/1372539-staging-and-production-options?ref=title">
request</a> for this feature.</p>
<p>In the meantime, using two AppHarbor applications is probably a
good idea. You can discriminate between by development and
production <a href=
"http://stackoverflow.com/questions/1638669/how-to-get-host-header-from-httpcontext-asp-net">
checking the host-header</a>. You could also use AppHarbor
AppSettings <a href=
"http://support.appharbor.com/kb/getting-started/managing-environments">
configuration-variable-replacement</a>. You can configure these in
the application interface.</p>
<p>You could add an attribute to your controllers that determines
whether requests are for the development site and <a href=
"http://stackoverflow.com/questions/1606991/asp-net-mvc-http-authentication-prompt">
require basic http auth</a> if that is the case.</p>
<p>Let us know if this works for you.</p></div>friismtag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T04:19:25Z2011-04-06T04:52:01ZPushing same site to separate environments<div><p>Thanks, this approach seems to work albeit it does require some
extra plumbing:</p>
<ol>
<li>Create second application (YourApp-Dev)<br></li>
<li>Add a new remote to your repository: <code>git remote add
appharbor-d {your dev url}</code><br></li>
<li>Associated it with a sub-domain or leave it as is<br></li>
<li>In Configuration Variables, add
<strong>RuntimeEnvironment</strong> and the value of
<strong>D</strong> or some value indicating this is the staging
environment.<br></li>
<li>In your project, add the appSetting with the same key but a
different value (e.g. <strong>L</strong>) to denote a
localhost.<br></li>
<li>
<p>Create a new derived Authorize attribute and attach it to your
<code>BaseController</code> or a controller you'd like to be
protected in dev:</p>
<pre>
<code>public class AuthorizeLocalAttribute : AuthorizeAttribute {
private const string LOCAL = "L";
private const string DEVELOPMENT = "D";
private const string PRODUCTION = "P";
protected override bool AuthorizeCore(HttpContextBase httpContext) {
// Ignore all Account controller calls (Anonymous)
if (httpContext.Request.RequestContext.RouteData.Values["controller"].ToSafeString() == "Account")
return true;
// Get runtime environment
string runtimeEnvironment = ConfigurationManager.AppSettings["RuntimeEnvironment"];
if (runtimeEnvironment == DEVELOPMENT) {
return base.AuthorizeCore(httpContext);
}
else {
return true;
}
}
}</code>
</pre></li>
</ol>
<p>Change it according to your needs. I might refactor into an
Enum. In my main app, I set the variable to "P" to denote
production... but I don't use it yet.</p>
<p>To push to dev: <code>git push appharbor-d master</code> and to
prod: <code>git push appharbor master</code>.</p>
<p>I also added a line to skip authorization for the Account
controller. This was important for me because otherwise my OpenID
redirection didn't work (it still has an issue with the port
numbers but that's for a different discussion).</p>
<p>Hope this helps other people. I'd love to this baked into
AppHarbor so I don't need to require code but this works and is
fairly lightweight.</p></div>subkamrantag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T04:26:04Z2011-04-06T04:26:04ZPushing same site to separate environments<div><p>PS. <code>ToSafeString()</code> is a simple extension:
<code>return value == null ? String.Empty :
value.ToString()</code>.</p>
<p>Use the attribute like:</p>
<pre>
<code>[AuthorizeLocal(Users = "My Admin ID")]
public abstract class BaseController : Controller { }</code>
</pre></div>subkamrantag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T15:56:14Z2011-04-06T15:56:14ZPushing same site to separate environments<div><p>Cool, thanks for sharing. Is this something you would be
interested in writing up for a blog post, either for your own blog
or as a guest post on the AppHarbor blog?</p></div>friismtag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T16:14:09Z2011-04-06T16:14:09ZPushing same site to separate environments<div><p>I just made one:</p>
<p><a href=
"http://kamranicus.com/Blog/Posts/2/befriending-appharbor-and-making-it-work-for-you">
http://kamranicus.com/Blog/Posts/2/befriending-appharbor-and-making...</a></p>
<p>Hope that helps some people out!</p></div>subkamrantag:support.appharbor.com,2010-11-23:Comment/64613772011-04-06T16:37:59Z2011-04-06T16:37:59ZPushing same site to separate environments<div><p>Perfect!, thanks.</p></div>friism