Commit 0060851a by Rocky Duan

add shared key authentication

parent 3b10ee3f
...@@ -35,6 +35,12 @@ APIPREFIX = CommentService::API_PREFIX ...@@ -35,6 +35,12 @@ APIPREFIX = CommentService::API_PREFIX
DEFAULT_PAGE = 1 DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 20 DEFAULT_PER_PAGE = 20
if RACK_ENV.to_s != "test" # disable api_key auth in test environment
before do
error 401 unless params[:api_key] == CommentService.config[:api_key]
end
end
# these files must be required in order # these files must be required in order
require './api/search' require './api/search'
......
level_limit: 3 level_limit: 3
cache_enabled: false cache_enabled: false
api_key: "PUT_YOUR_API_KEY_HERE"
cache_timeout: cache_timeout:
threads_search: 10 threads_search: 10
threads_query: 10 threads_query: 10
...@@ -74,7 +74,6 @@ helpers do ...@@ -74,7 +74,6 @@ helpers do
memcached_key = "threads_query_#{query_params.hash}" memcached_key = "threads_query_#{query_params.hash}"
cached_results = Sinatra::Application.cache.get(memcached_key) cached_results = Sinatra::Application.cache.get(memcached_key)
if cached_results if cached_results
puts "cache hit"
return { return {
collection: cached_results[:collection_ids].map{|id| CommentThread.find(id).to_hash(recursive: bool_recursive)}, collection: cached_results[:collection_ids].map{|id| CommentThread.find(id).to_hash(recursive: bool_recursive)},
num_pages: cached_results[:num_pages], num_pages: cached_results[:num_pages],
...@@ -83,8 +82,6 @@ helpers do ...@@ -83,8 +82,6 @@ helpers do
end end
end end
puts "cache miss"
sort_key_mapper = { sort_key_mapper = {
"date" => :created_at, "date" => :created_at,
"activity" => :last_activity_at, "activity" => :last_activity_at,
......
...@@ -94,10 +94,8 @@ class CommentThread < Content ...@@ -94,10 +94,8 @@ class CommentThread < Content
memcached_key = "threads_search_#{params.merge(options).hash}" memcached_key = "threads_search_#{params.merge(options).hash}"
results = Sinatra::Application.cache.get(memcached_key) results = Sinatra::Application.cache.get(memcached_key)
if results if results
puts "cache hit"
return results return results
end end
puts "cache miss"
end end
search = Tire::Search::Search.new 'comment_threads' search = Tire::Search::Search.new 'comment_threads'
search.query {|query| query.text :_all, params["text"]} if params["text"] search.query {|query| query.text :_all, params["text"]} if params["text"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment