Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial Skeleton

  • Loading branch information...
commit 5a3934835f4e84e6314118dc9bfd755aea34f662 0 parents
@Phrogz authored
30 app.rb
@@ -0,0 +1,30 @@
+# encoding: utf-8
+require 'sinatra'
+require 'haml'
+
+require_relative 'minify_resources'
+class MyApp < Sinatra::Application
+ enable :sessions
+
+ configure :production do
+ set :haml, { :ugly=>true }
+ set :clean_trace, true
+ set :css_files, :blob
+ set :js_files, :blob
+ MinifyResources.minify_all
+ end
+
+ configure :development do
+ set :css_files, MinifyResources::CSS_FILES
+ set :js_files, MinifyResources::JS_FILES
+ end
+
+ helpers do
+ include Rack::Utils
+ alias_method :h, :escape_html
+ end
+end
+
+require_relative 'helpers/init'
+require_relative 'models/init'
+require_relative 'routes/init'
2  config.ru
@@ -0,0 +1,2 @@
+require ::File.join( ::File.dirname(__FILE__), 'app' )
+run MyApp.new
1  helpers/init.rb
@@ -0,0 +1 @@
+require_relative 'partials'
30 helpers/nicebytes.rb
@@ -0,0 +1,30 @@
+module NiceBytes
+ K = 2.0**10
+ M = 2.0**20
+ G = 2.0**30
+ T = 2.0**40
+ def nice_bytes( bytes, max_digits=3 )
+ value, suffix, precision = case bytes
+ when 0...K
+ [ bytes, 'B', 0 ]
+ else
+ value, suffix = case bytes
+ when K...M then [ bytes / K, 'kiB' ]
+ when M...G then [ bytes / M, 'MiB' ]
+ when G...T then [ bytes / G, 'GiB' ]
+ else [ bytes / T, 'TiB' ]
+ end
+ used_digits = case value
+ when 0...10 then 1
+ when 10...100 then 2
+ when 100...1000 then 3
+ else 4
+ end
+ leftover_digits = max_digits - used_digits
+ [ value, suffix, leftover_digits > 0 ? leftover_digits : 0 ]
+ end
+ "%.#{precision}f#{suffix}" % value
+ end
+ module_function :nice_bytes
+end
+Sinatra.helpers NiceBytes if defined? Sinatra
19 helpers/partials.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require 'sinatra/base'
+
+module Sinatra
+ module PartialPartials
+ ENV_PATHS = %w[ REQUEST_PATH PATH_INFO REQUEST_URI ]
+ def spoof_request( uri, headers=nil )
+ new_env = env.dup
+ ENV_PATHS.each{ |k| new_env[k] = uri.to_s }
+ new_env.merge!(headers) if headers
+ call( new_env ).last.join
+ end
+
+ def partial( page, variables={} )
+ haml page, {layout:false}, variables
+ end
+ end
+ helpers PartialPartials
+end
49 minify_resources.rb
@@ -0,0 +1,49 @@
+#!/usr/bin/env ruby
+
+module MinifyResources
+ CSS_BLOB = 'public/blob.css'
+ CSS_DIR = 'public/css'
+ CSS_LIST = 'public/css/manifest.txt'
+ CSS_FILES = File.exists?(CSS_LIST) ? IO.read(CSS_LIST).scan(/\S+/) : Dir.chdir(CSS_DIR){ Dir['*.css'] }
+
+ JS_BLOB = 'public/blob.js'
+ JS_DIR = 'public/js'
+ JS_LIST = 'public/js/manifest.txt'
+ JS_FILES = File.exists?(JS_LIST) ? IO.read(JS_LIST).scan(/\S+/) : Dir.chdir(JS_DIR){ Dir['*.js'] }
+
+ def self.minify_all
+ require 'jsmin'
+ require 'cssmin'
+ minify( ::JSMin, JS_DIR, JS_FILES, JS_BLOB )
+ minify( ::CSSMin, CSS_DIR, CSS_FILES, CSS_BLOB )
+ end
+
+ def self.minify( lib, source_dir, files, output )
+ return if files.empty?
+ require_relative 'helpers/nicebytes'
+ raw_size = 0
+ minified = files.map do |file|
+ raw = IO.read(File.join(source_dir,file))
+ raw_size += raw.length
+ lib.minify(raw.dup)
+ end.join
+ min_size = minified.length
+ existing = File.exists?(output) && IO.read(output)
+
+ if minified==existing
+ puts "No change to #{output}"
+ else
+ File.open(output,"w"){ |f| f << minified }
+
+ puts "%i files => %s; %s => %s (%.1f%% reduction)" % [
+ files.length,
+ output,
+ NiceBytes.nice_bytes(raw_size),
+ NiceBytes.nice_bytes(min_size),
+ 100*(raw_size-min_size)/raw_size
+ ]
+ end
+ end
+end
+
+MinifyResources.minify_all if __FILE__==$0
6 models/init.rb
@@ -0,0 +1,6 @@
+# encoding: utf-8
+require 'sequel'
+# DB = Sequel.postgres 'dbname', user:'bduser', password:'dbpass', host:'localhost'
+# DB << "SET CLIENT_ENCODING TO 'UTF8';"
+
+#require_relative 'users'
4 models/user.rb
@@ -0,0 +1,4 @@
+# encoding: utf-8
+class User < Sequel::Model
+ # ...
+end
4 public/css/common.css
@@ -0,0 +1,4 @@
+html, body { background:#eee; margin:0; padding:0; color:#000 }
+h1,h2,h3,h4,h5,h6 { font-family:sans-serif; border-bottom:1px solid #aaa; margin:2em 0 0.5em 0 }
+h1#title { background:#333; margin-top:0; color:#ccc; padding:0.1em 0.5em; font-size:105% }
+#content { margin:1em 2em }
1  routes/init.rb
@@ -0,0 +1 @@
+require_relative 'main'
6 routes/main.rb
@@ -0,0 +1,6 @@
+class MyApp < Sinatra::Application
+ get "/" do
+ @title = "Welcome to MyApp"
+ haml :main
+ end
+end
25 views/layout.haml
@@ -0,0 +1,25 @@
+!!! XML
+!!! 1.1
+%html(xmlns="http://www.w3.org/1999/xhtml")
+ %head
+ %meta(http-equiv="Content-Type" content="text/html; charset=utf-8" )
+ %meta(http-equiv="expires" content="0" )
+ %meta(http-equiv="Content-Script-Type" content="text/javascript" )
+ %meta(http-equiv="Content-Style-Type" content="text/css" )
+ %meta(http-equiv="X-UA-Compatible" content="IE=8")
+ %meta(name="author" content="MyAppAuthor")
+ %link(rel="icon" type="image/png" href="/favicon.png")
+ %title= @title
+ - if options.css_files == :blob
+ %link(type="text/css" rel="stylesheet" href="/blob.css?modified=#{File.mtime('public/blob.css').to_i}" )
+ - else
+ - options.css_files.each do |css|
+ %link(type="text/css" rel="stylesheet" href="/css/#{css}" )
+ - if options.js_files == :blob
+ %script(type="text/javascript" src="/blob.js?modified=#{File.mtime('public/blob.js').to_i}")
+ - else
+ - options.js_files.each do |js|
+ %script(type="text/javascript" src="/js/#{js}")
+ %body{id:@action}
+ %h1#title= @title
+ #content= yield
Please sign in to comment.
Something went wrong with that request. Please try again.