How to serve static assets with Rack::App? - ruby

I am trying to make Rack::App serve static files from the folder public/ (like possible with Sinatra using set :public_folder, 'public').
Is this supported out of the box? If not, how to do it in the simplest way?

Related

Is it possible to serve static HTML without the file extension?

I have a ruby + sinatra website where most of the files are static HTML, meaning they can be accessed by going to http://www.mydomain.com/features.html, http://www.mydomain.com/pricing.html, etc... (I am using sinatra for a few more dynamic pages)
What I would like to do is serve these static HTML files without their file extensions. So going to http://www.mydomain.com/features would show the same as if I currently visited http://www.mydomain.com/features.html currently.
I have tried simply removing my .html file extensions, however when trying to open these files in the browser, the browser downloads downloads the file instead of rendering it on screen.
Is it possible to continue serving my static .html files just without their file extensions? Thanks!
If you do this:
require 'sinatra'
get '/:file_name' do |fname|
send_file(
File.join(settings.public_folder, "#{fname}.html")
)
end
...then either url:
http://www.mydomain.com/features.html
http://www.mydomain.com/features
will return the same page.
settings.public_folder: By default, this is assumed to be a directory
named “public” within the root directory...http://www.sinatrarb.com/configuration.html
If you define the routes for your dynamic pages above that route, then they will match first.

how to build image service website with sails.js

I am using sails.js to build an image service website. There are lots of images need be shown on the webpage. And the website will allow users to upload images as well. If I put it all images under assets folder, they will be copied to .tmp/public everytime when i restart the sails server . it will be very slow. Does anyone have an idea where should i put .Should I write a route rule to handle this? thanks.
/assets are for your static contents (like css, frontendjs, ...).
If you have a lots of images you should place them in one folder of your server and write a route for showing it.
Or better: Use some Cloud-Hosting like amazonS3 or imageShack.
Example for middleware:
1.) create a file "express.js" in config/
2.) Put this code into the file and change to the right path (routing + your local folder):
module.exports.express = {
customMiddleware: function(app){
app.use('/images', require('../node_modules/sails/node_modules/express').static('/User/yourfolder'));
}
}

How to serve assets conditionally

I'm building an application that has lots of web-templates that get served based on DB binding with the request's domain name. Which is straightforward job with blade.
But, how do I organize the assets of all those web-templates? Obvious route would be to move all the web-template's assets into public directory, but it's an obviously tedious workflow of rewriting paths to the assets inside the web-templates.
I would like you to tell me whether you can think of a way to serve those assets conditionally, so i can let them be siblings to the web-templates blade files inside the views directory.
Thanks
Do you mean that your app has many layouts, like css and js, and each template has the same html, but diffrent css/js ?

Simple "static" Ruby sites?

For early development, I typically build out a static version of the site. Previously I'd use PHP and have something like...
images
javascripts
stylesheets
templates
-- header.php
-- footer.php
index.php
users.php
And the index.php and users.php would have some basic PHP include code for those header and footer files.
I also got the added benefit of being able to use a few PHP functions.
But I haven't used PHP for anything in ages and use Ruby almost exclusively...so I'm wondering, is there a way to pull off something really basic like this in Ruby?
Primarily looking for something that allows me to:
Do basic file includes (so I can create simple templates)
Run Ruby inside the files
Ideally I could also use LiveReload with it.
Additional details: I'm running this locally on OS X and I typically use Pow as a server.
Peter is right to recommend Sinatra. There are typically two types of Sinatra applications. Modular and classical. For your example, I'll create a classical application. It isn't much work to convert it to a modular if you find that style would better fit your needs.
You'll want to install the gem with gem install sinatra. Create a new directory for your project and two new files as follows:
# app.rb
require 'sinatra'
get '/' do
erb :index
end
# config.ru
require './app'
run Sinatra::Application
Create another directory called views and add this file:
# index.erb
Hello World!
Then type ruby app.rb and viola, you now have a working project on localhost:4567/.
To serve static files like css and js, just create a public directory. From there, any file will be able to accessed after the root url. So if you created a css folder, its respective url would be: yourdomain.com/css/styles.css.
So the entire directory would be as folllows:
app/
app.rb
config.ru
public/
css/
js/
images/
views/
index.erb
Between the Sinatra Book and the read me, you should be able to find all the info you need.
To accomplish the templates, you'll need something called Sinatra Partial.
I'm not too familiar LiveReload but it seems like Compass accomplishes the same thing and has great integration with Sinatra. So long as pow is a rack based, you should have no problem using it.
Here is a Sinatra Bootstrap I use for all my projects. It has Compass and Sinatra Partial preconfigured and makes it really easy to deploy with Heroku. It also uses Slim, Coffeescript, Thin (as the server), Twitter Bootstrap and Sass but it shouldn't be too much work to sub that with your respective favorites or remove them all together.
I gifted my girlfriend a website with her domain name, I chose Jekyll for the job which uses RubyGems for the purpose.
It was easy and fun. Moreover you have lots of themes available which you can configure with .YML files just changing there will work for hole site. Although Linux or Mac OS is suggested by official Jekyll website but i did it in windows which was not much of a problem. They have steps defined for working on windows on their website.
http://jekyllrb.com/
The best part was that i could host the website via the git hub pages only. I didn't have to buy anything. Git hub allows you to host one repo. (funFact : Jekyll was developed by github inventor)
That's the one i used and made the website in 20 hours with no prior knowledge of Ruby and Jekyll.
So i will suggest you check it out!
Perhaps Jekyll will fit the bill?
Its description:
Jekyll is a simple, blog aware, static site generator. It takes a
template directory (representing the raw form of a website), runs it
through Textile or Markdown and Liquid converters, and spits out a
complete, static website suitable for serving with Apache or your
favorite web server. This is also the engine behind GitHub Pages,
which you can use to host your project’s page or blog right here from
GitHub
Yes, i use Sinatra for this, see https://github.com/sinatra/sinatra/ for some samples.
Running ruby inside the files isn't very static but in Sinatra you can do both.
Jekyll won't leave you run Ruby code "inside" the files. Jekyll is a parsing engine bundled as a ruby gem. You basically code templates using HTML and Liquid and write content using markdown that gets embebed generating plain HTML files that you can upload to any web server.

In an external assets folder the best place to put libraries for CodeIgniter 2.x?

when I last used CodeIgniter it was version 1.x and I read an external assets folder for css, js, and images was the best way to handle things. I wanted to know if that is the correct way to do things in 2.x. I found this on SO but it didn't address it directly:
Assets in codeigniter
The other SO entries were too old to address 2.x.
Thank you.
As long as they are both publicly accessible then there is no difference between
application
...
system
...
assets
css
style.css
images
image.png
and
application
...
system
...
css
style.css
images
image.png
Depending on your .htaccess file you may need to make a change but CodeIgniter doesn't require you to use any specific scheme for storing assets.

Resources