diff --git a/.gitignore b/.gitignore index 54b7261..55fa745 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -config.rb +app/config.rb diff --git a/Gemfile b/Gemfile index a2cdc3d..68a1e04 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,6 @@ gem 'pygments.rb' gem 'haml' gem 'sass' + +gem 'unicorn' +gem 'shotgun' diff --git a/Gemfile.lock b/Gemfile.lock index 041c9ba..d0dc520 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ GEM specs: haml (4.0.3) tilt + kgio (2.8.0) pg (0.15.1) posix-spawn (0.3.6) pygments.rb (0.5.1) @@ -11,13 +12,20 @@ GEM rack (1.5.2) rack-protection (1.5.0) rack + raindrops (0.11.0) sass (3.2.9) sequel (4.0.0) + shotgun (0.9) + rack (>= 1.0) sinatra (1.4.3) rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) tilt (1.4.1) + unicorn (4.6.3) + kgio (~> 2.6) + rack + raindrops (~> 0.7) yajl-ruby (1.1.0) PLATFORMS @@ -29,4 +37,6 @@ DEPENDENCIES pygments.rb sass sequel + shotgun sinatra + unicorn diff --git a/config.example.rb b/app/config.example.rb similarity index 100% rename from config.example.rb rename to app/config.example.rb diff --git a/app/database.rb b/app/database.rb new file mode 100644 index 0000000..85b70f9 --- /dev/null +++ b/app/database.rb @@ -0,0 +1,8 @@ +DB = Sequel.connect(CONFIG[:database_url]) + +DB.create_table(:pastes) do + primary_key :id + String :handle + String :type + Text :contents +end unless DB.table_exists?(:pastes) diff --git a/app/paste.rb b/app/paste.rb new file mode 100644 index 0000000..ca3af79 --- /dev/null +++ b/app/paste.rb @@ -0,0 +1,20 @@ +module Paste + def get(params) + table.where(params).first + end + + def add(params) + id = table.insert(params) + return unless id.is_a?(Integer) + + get(id: id) + end + + private + + def table + DB[:pastes] + end + + extend self +end diff --git a/application.rb b/application.rb deleted file mode 100644 index 18be0fc..0000000 --- a/application.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'securerandom' -require 'bundler/setup' -require 'sinatra' -require 'sequel' -require 'haml' - -require File.expand_path('../config', __FILE__) - -DB = Sequel.connect(CONFIG[:database_url]) -DB.create_table :pastes do - primary_key :id - String :handle - String :type - Text :contents -end unless DB.table_exists? :pastes - -set :public_folder, 'public' - -get '/' do - haml :form, layout: :default -end - -post '/' do - id = DB[:pastes].insert(handle: SecureRandom.hex, contents: params[:contents]) - record = DB[:pastes].where(id: id).first - redirect "/#{id}/#{record[:handle]}" -end - -get '/:id/:handle' do - @record = DB[:pastes].where(id: params[:id], handle: params[:handle]).first - haml :show, layout: :default -end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..696dd7b --- /dev/null +++ b/config.ru @@ -0,0 +1,6 @@ +$:.unshift File.dirname(__FILE__) +require 'pastemaster' + +use Rack::ShowExceptions + +run Pastemaster.new diff --git a/pastemaster.rb b/pastemaster.rb new file mode 100644 index 0000000..924f2d3 --- /dev/null +++ b/pastemaster.rb @@ -0,0 +1,28 @@ +require 'securerandom' + +require 'bundler/setup' +require 'sinatra' +require 'sequel' +require 'haml' + +require 'app/config' +require 'app/database' +require 'app/paste' + +class Pastemaster < Sinatra::Application + set :public_folder, 'public' + + get '/' do + haml :form, layout: :default + end + + post '/' do + record = Paste.add(handle: SecureRandom.hex, contents: params[:contents]) + redirect record ? "/#{id}/#{record[:handle]}" : '' + end + + get '/:id/:handle' do + @record = Paste.get(id: params[:id], handle: params[:handle]) + haml :show, layout: :default + end +end