Skip to content

Instantly share code, notes, and snippets.

@bbuck
Last active December 16, 2015 04:48
Show Gist options
  • Save bbuck/5379399 to your computer and use it in GitHub Desktop.
Save bbuck/5379399 to your computer and use it in GitHub Desktop.
Generates a static rack application as recommended by Heroku for quick test deployments. Just save this on a *NIX box and make it executable (I use the named rack_static) and then use it via rack_static <name of app>
#!/usr/bin/env ruby
if ARGV.length < 1
puts "rack_static is a simplistic command line tool and is only usable on"
puts "*NIX systems -- 2013 Brandon Buck\n"
puts "Usage: rack_static <project_name>"
exit
end
# Define file contents
@gemfile = "source 'https://rubygems.org'
gem 'rack'"
@configru = "use Rack::Static,
:urls => [\"/images\", \"/js\", \"/css\"],
:root => \"public\"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}"
@index = "<!doctype html>
<html>
<head>
<title>Rack Static Generated Page</title>
<link href='/css/main.css' rel='stylesheet' />
</head>
<body>
<div class='content'>
<h3>Thanks for using the Rack Static Generator</h3>
<p>
Thanks for using this tiny script that I made to generate static rack
apps as recommended by Heroku. This is soley intended to get a base
app up and ready to go!
<br />
- Brandon Buck
</p>
</div>
</body>
</html>"
@css = "
.content {
width: 400px;
margin: 0 auto;
font-family: Helvetica, Calibri, Arial, sans-sarif;
}"
# End file contents
@flags = {}
def log(msg)
if @flags[:verbose]
puts msg
end
end
@name = nil
ARGV.each do |arg|
rx = /^--(.+)$/
if arg =~ rx
match_data = rx.match arg
match = rx[1]
@flags[match.to_sym] = true
elsif arg.length >= 2 and arg[0] == "-"
case arg[-1]
when "v"
@flags[:verbose] = true
end
else
if @name.nil?
@name = arg
end
end
end
require 'fileutils'
log "Creating
#{@name}/
config.ru
public/
index.html
images/
js/
css/
main.css"
pub_root = "#{@name}/public"
FileUtils.mkdir_p ["#{pub_root}/images", "#{pub_root}/js", "#{pub_root}/css"]
FileUtils.touch ["#{@name}/config.ru", "#{pub_root}/index.html", "#{pub_root}/css/main.css"]
FileUtils.cd "#{@name}" do
`bundle init`
end
File.open "#{@name}/Gemfile", "w" do |file|
file.write @gemfile
end
File.open "#{@name}/config.ru", "w" do |file|
file.write @configru
end
log "Adding goodness to files\n"
File.open "#{@name}/public/index.html", "w" do |file|
file.write @index
end
File.open "#{@name}/public/css/main.css", "w" do |file|
file.write @css
end
puts "Static site #{@name} has been created"
puts "cd #{@name} && bundle install\n"
puts "run with: rackup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment