Commit 110c9f565f7dc95fe23363fd026d0dafcf040776
Committed by
Alessandro Beltrão
1 parent
5bac7a05
Exists in
federation_webfinger
and in
1 other branch
Creates federation API initial structure
Showing
1 changed file
with
107 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,107 @@ | @@ -0,0 +1,107 @@ | ||
| 1 | +require 'grape' | ||
| 2 | +#require 'rack/contrib' | ||
| 3 | +Dir["#{Rails.root}/lib/noosfero/api/*.rb"].each {|file| require file unless file =~ /api\.rb/} | ||
| 4 | + | ||
| 5 | +module Noosfero | ||
| 6 | + module API | ||
| 7 | + | ||
| 8 | + class Federation < Grape::API | ||
| 9 | + format :json | ||
| 10 | + content_type :txt, "text/plain" | ||
| 11 | + | ||
| 12 | + #REMOVE ME - testing routes only | ||
| 13 | + get "/me" do | ||
| 14 | + present_partial current_person, :with => Entities::Person, :current_person => current_person | ||
| 15 | + end | ||
| 16 | + end | ||
| 17 | + | ||
| 18 | + class BaseAPI < Grape::API | ||
| 19 | + before { set_locale } | ||
| 20 | + before { setup_multitenancy } | ||
| 21 | + before { detect_stuff_by_domain } | ||
| 22 | + before { filter_disabled_plugins_endpoints } | ||
| 23 | + before { init_noosfero_plugins } | ||
| 24 | + after { set_session_cookie } | ||
| 25 | + | ||
| 26 | + version 'v1' | ||
| 27 | + prefix [ENV['RAILS_RELATIVE_URL_ROOT'], "api"].compact.join('/') | ||
| 28 | + format :json | ||
| 29 | + content_type :txt, "text/plain" | ||
| 30 | + | ||
| 31 | + mount V1::Articles | ||
| 32 | + mount V1::Comments | ||
| 33 | + mount V1::Users | ||
| 34 | + mount V1::Communities | ||
| 35 | + mount V1::People | ||
| 36 | + mount V1::Enterprises | ||
| 37 | + mount V1::Categories | ||
| 38 | + mount V1::Tasks | ||
| 39 | + mount V1::Tags | ||
| 40 | + mount V1::Environments | ||
| 41 | + mount V1::Search | ||
| 42 | + mount V1::Contacts | ||
| 43 | + mount V1::Boxes | ||
| 44 | + mount V1::Profiles | ||
| 45 | + mount V1::Activities | ||
| 46 | + mount Session | ||
| 47 | + end | ||
| 48 | + | ||
| 49 | + class API < Grape::API | ||
| 50 | + use Rack::JSONP | ||
| 51 | + helpers APIHelpers | ||
| 52 | + | ||
| 53 | + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | ||
| 54 | + logger.formatter = GrapeLogging::Formatters::Default.new | ||
| 55 | + #use GrapeLogging::Middleware::RequestLogger, { logger: logger } | ||
| 56 | + | ||
| 57 | + rescue_from :all do |e| | ||
| 58 | + logger.error e | ||
| 59 | + error! e.message, 500 | ||
| 60 | + end | ||
| 61 | + | ||
| 62 | + @@NOOSFERO_CONF = nil | ||
| 63 | + def self.NOOSFERO_CONF | ||
| 64 | + if @@NOOSFERO_CONF | ||
| 65 | + @@NOOSFERO_CONF | ||
| 66 | + else | ||
| 67 | + file = Rails.root.join('config', 'noosfero.yml') | ||
| 68 | + @@NOOSFERO_CONF = File.exists?(file) ? YAML.load_file(file)[Rails.env] || {} : {} | ||
| 69 | + end | ||
| 70 | + end | ||
| 71 | + | ||
| 72 | + mount Noosfero::API::BaseAPI | ||
| 73 | + mount Noosfero::API::Federation | ||
| 74 | + | ||
| 75 | + # hook point which allow plugins to add Grape::API extensions to API::API | ||
| 76 | + #finds for plugins which has api mount points classes defined (the class should extends Grape::API) | ||
| 77 | + @plugins = Noosfero::Plugin.all.map { |p| p.constantize } | ||
| 78 | + @plugins.each do |klass| | ||
| 79 | + if klass.public_methods.include? :api_mount_points | ||
| 80 | + klass.api_mount_points.each do |mount_class| | ||
| 81 | + mount mount_class if mount_class && ( mount_class < Grape::API ) | ||
| 82 | + end | ||
| 83 | + end | ||
| 84 | + end | ||
| 85 | + | ||
| 86 | + def self.endpoint_unavailable?(endpoint, environment) | ||
| 87 | + api_class = endpoint.options[:app] || endpoint.options[:for] | ||
| 88 | + if api_class.present? | ||
| 89 | + klass = api_class.name.deconstantize.constantize | ||
| 90 | + return klass < Noosfero::Plugin && !environment.plugin_enabled?(klass) | ||
| 91 | + end | ||
| 92 | + end | ||
| 93 | + | ||
| 94 | + class << self | ||
| 95 | + def endpoints_with_plugins(environment = nil) | ||
| 96 | + if environment.present? | ||
| 97 | + cloned_endpoints = endpoints_without_plugins.dup | ||
| 98 | + cloned_endpoints.delete_if { |endpoint| endpoint_unavailable?(endpoint, environment) } | ||
| 99 | + else | ||
| 100 | + endpoints_without_plugins | ||
| 101 | + end | ||
| 102 | + end | ||
| 103 | + alias_method_chain :endpoints, :plugins | ||
| 104 | + end | ||
| 105 | + end | ||
| 106 | + end | ||
| 107 | +end |