-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
68 lines (61 loc) · 1.76 KB
/
app.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
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader' if development?
require 'sinatra/activerecord'
require 'sinatra/jsonp'
require 'sinatra/cross_origin'
require './config/environments' #database configuration
require 'json'
require './helpers/authentication'
require './helpers/adminAuthentication'
require './helpers/keyProtection'
require './helpers/constants'
require './helpers/haltJsonp'
require './controllers/users_controller'
require './controllers/posts_controller'
require './controllers/friends_controller'
require './controllers/images_controller'
require './controllers/ad_controller'
require './controllers/admin_controller'
set :protection, :except => [:http_origin]
set :public_folder, File.dirname(__FILE__) + '/static'
if Rack::Utils.respond_to?("key_space_limit=")
Rack::Utils.key_space_limit = 400000
end
helpers do
include Sinatra::Authentication
include Sinatra::HaltJsonp
include Sinatra::AdminAuthentication
include Sinatra::KeyProtection
end
configure do
enable :cross_origin
mime_type :json, "application/json"
mime_type :png, "image/png"
mime_type :html, "text/html"
ActiveRecord::Base.default_timezone = :utc
end
before do
if ENV['RACK_ENV'] != 'test'
query_string = env["QUERY_STRING"]
method = query_string.scan(/&?method=(\w+)&?/).flatten
if method.length != 0
puts "JSONP in method.length != 0"
env["QUERY_STRING"] = query_string
env["REQUEST_METHOD"] = method[0]
end
end
if (request.content_type == "application/json" &&
(body = request.body.read) != "")
begin
json = JSON.parse(body)
@json = json;
rescue JSON::ParserError => e
haltJsonp 400, "Invalid JSON: \n" + e.message
end
end
end
not_found do
jsonp({:status => 404, :message => "Non-existing resource"})
end