-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.rb
122 lines (102 loc) · 2.96 KB
/
application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'bundler'
ENV['RACK_ENV'] = 'development' unless ENV['RACK_ENV']
Bundler.require(:default, ENV['RACK_ENV'])
require 'dotenv'
Dotenv.load
require 'open-uri'
include Twitter::Autolink
class App < Sinatra::Base
configure do
env = ENV['RACK_ENV'] || 'development'
enable :logging
enable :raise_errors
set :root, File.dirname(__FILE__)
set :sass, { :load_paths => [ "#{App.root}/app/css" ] }
set :haml, :format => :html5
register Sinatra::ActiveRecordExtension
register Sinatra::CompassSupport
register Sinatra::AssetPack
register Sinatra::Partial
register Sinatra::Contrib
register WillPaginate::Sinatra
enable :partial_underscores
db = URI.parse(ENV['DATABASE_URL'] || "postgres://localhost/tweetden_#{ENV['RACK_ENV']}")
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
#MongoMapper.database = 'tweetden'
#if ENV['RACK_ENV'] == :production
#MongoMapper.connection = Mongo::Connection.new(ENV['TWEETDEN_MONGO_HOST'], ENV['TWEETDEN_MONGO_PORT'])
#MongoMapper.database.authenticate(ENV['TWEETDEN_MONGO_USER'], ENV['TWEETDEN_MONGO_PASS'])
#else
#MongoMapper.connection = Mongo::Connection.new('localhost')
#end
end
# Load files
(
Dir['./lib/*.rb'].sort +
Dir['./models/*.rb'].sort
).uniq.each { |rb| require rb }
configure :development do |config|
require "sinatra/reloader"
register Sinatra::Reloader
enable :show_exceptions
end
assets {
serve '/images', from: 'app/images'
serve '/js', from: 'app/js'
serve '/css', from: 'app/css'
js :jsforhead, [
'/js/modernizr.js',
'/js/respond.js'
]
js :application, [
'/js/jquery.js',
'/js/underscore.js',
'/js/console.js'
]
css :application, [
'/css/*.css'
]
expires 86400*365, :public
}
before do
@page = (params[:page] || 1).to_i
@per_page = 50
@direction = params[:direction] || "desc"
@user = User.first()
end
get '/' do
cache_control :public, :must_revalidate, :max_age => 3600
if params[:query]
if params[:query] == '' then redirect '/' end
@query = CGI::unescape(params[:query])
@tweets = Tweet.search_for(@query)
params.except!("splat", "captures")
else
@tweets = Tweet
end
@count = @tweets.count
@tweets = @tweets.order("created_at #{@direction}").paginate(:page => @page, :per_page => 50)
haml :index
end
post '/' do
redirect "/?query=#{CGI::escape(params[:query])}"
end
get 'favicon.ico' do
""
end
not_found do
haml :not_found
end
error do
@error = request.env['sinatra.error'].to_s
haml :error
end unless Sinatra::Application.environment == :development
end