From 6386287afc177e736d5521d93a228ffe27b8745d Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 31 Oct 2014 17:02:58 -0300 Subject: [PATCH] Add logger to api requests --- lib/api/api.rb | 5 +++++ lib/api/helpers.rb | 13 ++++++++++++- lib/api/v1/articles.rb | 2 -- lib/api/v1/categories.rb | 1 - lib/api/v1/comments.rb | 2 -- lib/api/v1/communities.rb | 1 - lib/api/v1/enterprises.rb | 1 - lib/api/v1/people.rb | 1 - lib/api/v1/users.rb | 2 -- 9 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/api/api.rb b/lib/api/api.rb index eddc248..fe39ea3 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -3,6 +3,11 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} module API class API < Grape::API + before { start_log } + before { setup_multitenancy } + before { detect_stuff_by_domain } + after { end_log } + version 'v1' prefix "api" format :json diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index ce161d5..3d74e3a 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -3,7 +3,7 @@ module API PRIVATE_TOKEN_PARAM = :private_token def logger - API.logger + @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log")) end def current_user @@ -166,6 +166,17 @@ module API end protected + def start_log + logger.info "Started #{request.path}" + end + def end_log + logger.info "Completed #{request.path}" + end + + def setup_multitenancy + Noosfero::MultiTenancy.setup!(request.host) + end + def detect_stuff_by_domain @domain = Domain.find_by_name(request.host) if @domain.nil? diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index c2372c7..c62affc 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -1,7 +1,6 @@ module API module V1 class Articles < Grape::API - before { detect_stuff_by_domain } before { authenticate! } resource :articles do @@ -19,7 +18,6 @@ module API # :params => API::Entities::Article.documentation # } get do - articles = select_filtered_collection_of(environment, 'articles', params) present articles, :with => Entities::Article end diff --git a/lib/api/v1/categories.rb b/lib/api/v1/categories.rb index 33f5514..8250bca 100644 --- a/lib/api/v1/categories.rb +++ b/lib/api/v1/categories.rb @@ -1,7 +1,6 @@ module API module V1 class Categories < Grape::API - before { detect_stuff_by_domain } before { authenticate! } resource :categories do diff --git a/lib/api/v1/comments.rb b/lib/api/v1/comments.rb index 5e29520..e6c52a0 100644 --- a/lib/api/v1/comments.rb +++ b/lib/api/v1/comments.rb @@ -1,8 +1,6 @@ module API module V1 class Comments < Grape::API - - before { detect_stuff_by_domain } before { authenticate! } resource :articles do diff --git a/lib/api/v1/communities.rb b/lib/api/v1/communities.rb index 1332397..f19f689 100644 --- a/lib/api/v1/communities.rb +++ b/lib/api/v1/communities.rb @@ -1,7 +1,6 @@ module API module V1 class Communities < Grape::API - before { detect_stuff_by_domain } before { authenticate! } resource :communities do diff --git a/lib/api/v1/enterprises.rb b/lib/api/v1/enterprises.rb index 6116f8c..ea0d765 100644 --- a/lib/api/v1/enterprises.rb +++ b/lib/api/v1/enterprises.rb @@ -1,7 +1,6 @@ module API module V1 class Enterprises < Grape::API - before { detect_stuff_by_domain } before { authenticate! } resource :enterprises do diff --git a/lib/api/v1/people.rb b/lib/api/v1/people.rb index fd10dca..56e55ab 100644 --- a/lib/api/v1/people.rb +++ b/lib/api/v1/people.rb @@ -1,7 +1,6 @@ module API module V1 class People < Grape::API - before { detect_stuff_by_domain } before { authenticate! } resource :people do diff --git a/lib/api/v1/users.rb b/lib/api/v1/users.rb index 6747638..9408476 100644 --- a/lib/api/v1/users.rb +++ b/lib/api/v1/users.rb @@ -1,8 +1,6 @@ module API module V1 class Users < Grape::API - - before { detect_stuff_by_domain } before { authenticate! } resource :users do -- libgit2 0.21.2