api.rb
2.22 KB
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
require 'grape'
#require 'rack/contrib'
Dir["#{Rails.root}/lib/noosfero/api/*.rb"].each {|file| require_dependency file unless file =~ /api\.rb/}
module Noosfero
module API
class API < Grape::API
use Rack::JSONP
logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log"))
logger.formatter = GrapeLogging::Formatters::Default.new
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
rescue_from :all do |e|
# Many brave warriors have fallen in the battle for fixing the API log
# Please, don't remove these 2 lines until the API log problem has
# been PROPERLY fixed by our savior!!!
# Otherwise we will have no clue of what went wrong in the API
puts "API error during processing: #{$!}"
puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
# Thanks!
logger.error e
end
@@NOOSFERO_CONF = nil
def self.NOOSFERO_CONF
if @@NOOSFERO_CONF
@@NOOSFERO_CONF
else
file = Rails.root.join('config', 'noosfero.yml')
@@NOOSFERO_CONF = File.exists?(file) ? YAML.load_file(file)[Rails.env] || {} : {}
end
end
before { setup_multitenancy }
before { detect_stuff_by_domain }
after { set_session_cookie }
version 'v1'
prefix "api"
format :json
content_type :txt, "text/plain"
helpers APIHelpers
mount V1::Articles
mount V1::Comments
mount V1::Users
mount V1::Communities
mount V1::People
mount V1::Enterprises
mount V1::Categories
mount V1::Tasks
mount V1::Tags
mount V1::Environments
mount Session
# hook point which allow plugins to add Grape::API extensions to API::API
#finds for plugins which has api mount points classes defined (the class should extends Grape::API)
@plugins = Noosfero::Plugin.all.map { |p| p.constantize }
@plugins.each do |klass|
if klass.public_methods.include? :api_mount_points
klass.api_mount_points.each do |mount_class|
mount mount_class if mount_class && ( mount_class < Grape::API )
end
end
end
end
end
end