Rails serving a static page on Heroku - heroku

In rails 4.0 I use a redirect to serve a static landing page in my rails application:
get '/landing', :to => redirect('/landing.html')
This works locally, but on Heroku, it yields a redirect loop. E.g. in Chrome: This webpage has a redirect loop. What would be a good way to avoid this problem on Heroku or otherwise accomplish this static serving in a proper way?

Related

Sinatra Static Page

In my public folder, I have a file index.html. I want to be able to just use the url localhost:4567 to display the page (without displaying localhost:4567/index.html)
This is my current ruby script:
require 'sinatra'
set :public_folder, 'public'
get '/' do
redirect '/index.html'
end
I have tried removing the redirect statement, but the url still comes up with the index.html.
You could use send_file here:
get "/" do
send_file 'public/index.html'
end
You need to provide the full path of the file from the working directory (i.e. not just the path under public), and this only works at the root url, it won’t serve index.html pages for directories generally. If you want that you would probably need to set up a separate web server in front of Sinatra and configure it appropriately.
The idea behind redirects is exactly to force the browser make a new request to where you are redirecting to.
As you don't want to change the url, you have two possible solutions:
Rewrite instead of redirect. Sinatra itself doesn't provide such functionality, but you can easily use a rack middleware:
require 'rack/rewrite'
use Rack::Rewrite do
rewrite '/', '/index.html'
end
Serve the index.html contents when asked for the root path:
get '/' do
File.read("#{APP_ROOT}/public/index.html")
end

How do you access assets in a Pakyow app on heroku?

Problem: Pakyow App deployed to Heroku and all JS and CSS sourced/linked in template head 404.
I assume this is a problem stemming from Heroku, but rather not one that I am aware of. As far as I know, Heroku allows static assets in the public directory.
Anyway, looking for some helpful pointers from Pakyow users.
The app (currently) is at http://pakyow-go.herokuapp.com and the repo can be found at http://github.com/jphager2/pakyow-go.
This is an unfortunate default config option in the currently released version. To get around it, add this to the production configuration block in app.rb:
app.static = true
The reason it works this way out of the box is in most production cases you don't want the app serving static files. This would instead be the responsibility of the HTTP server. On Heroku though, you want the app server to serve static files unless you host them from a CDN.

Basic HTTP Authentication for a Middleman app

I wonder whether there's an easy way to add Basic HTTP Authentication to a Middleman deployed website? I found some Heroku specific gists, but nothing else.
The Heroku specific gists all seem to need a config.ru file, but do Middleman projects even have such a file? Mine don't.
config.ru is not specific to Middleman or Heroku, but is a convention for serving a rack-based app (see https://devcenter.heroku.com/articles/rack). If you want to have basic auth on a static site that you have built using middleman and then deployed, you will need to set it up in a way that's appropriate to that hosting. Where are you hosting the site?

Ember.js, Rails and Wildcard Subdomains

How can I use ember.js routing with rails wildcard subdomains?
Say, a user signs up on the site, they get a subdomain. Joe and Josh both sign up, they each get a subdomain.
joe.mysite.com
josh.mysite.com
Rails routes all my api calls (routes.rb) while ember routes everything else. What would the best way of doing this? Using rails or ember to route it? Is it even possible to use ember to route subdomains?
The best way would be programmatically checking with location.host or location.hostname.
But ember usually works it's magic by faking URLs and not reloading the page... I don't know of any way to switch subdomains without reloading the page.
I'd redirect the subdomain to a URL in your main app and let Ember handle the routing from there.

How do I create alias for Jekyll static site on Heroku?

I have a Jekyll site running on Heroku.
After site generation, I have a file created public/photos.html. How do I set up Jekyll/Heroku to allow me to access the page as domain.com/photos instead of domain.com/photos.html?
And I'd prefer NOT to have to use rack-rewrite which stands up rack middleware. Seems a bit much for this.
Thanks!
Simplest way is to just name the file photos/index.html instead of photos.html. If that doesn't work for you, you can use the variable permalink in the Front Matter.
You can do route matching in Sinatra and serve whatever pages you like, see: http://www.sinatrarb.com/intro.html

Resources