Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/awsl-layer-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
FROM lambci/lambda:20210129-build-ruby2.7 as builder
FROM public.ecr.aws/lambda/ruby:4.0 as builder

ADD Gemfile .
RUN yum install -y libyaml-devel
COPY Gemfile Gemfile.lock ./
RUN bundle config set path "/lambda"
RUN bundle install --without=development --jobs 4 --retry 3

FROM lambci/yumda:2 as yumda
RUN yum install -y libyaml

FROM public.ecr.aws/lambda/ruby:4.0
COPY --from=builder "/lambda/ruby" "/lambda/opt/ruby/gems"

2 changes: 1 addition & 1 deletion .github/awsl-layer-docker/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

rm -Rf ./libs
cp ../../Gemfile Gemfile
cp ../../Gemfile Gemfile && cp ../../Gemfile.lock Gemfile.lock

docker build --no-cache -t lambda .
id=$(docker create lambda)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
ruby-version: 4.0

# - name: Cache Dependencies
# uses: actions/cache@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
uses: ruby/setup-ruby@v1
with:
bundler-cache: false
ruby-version: 3.2
ruby-version: 4.0

- name: build "-api" source
working-directory: git
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
4.0.5
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source "https://rubygems.org"
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# gem "aws-partitions", "~> 1.613.0"
gem "aws-sdk-s3", "~> 1.114.0"
gem "relaton", ENV["RELATION_GEM_VERSION"] || "~> 1.18.0"
gem "aws-sdk-s3", "~> 1.188"
gem "relaton", ENV["RELATION_GEM_VERSION"] || "~> 2.1.0"

group :test do
gem "rspec"
Expand Down
188 changes: 3 additions & 185 deletions lib/app.rb
Original file line number Diff line number Diff line change
@@ -1,188 +1,6 @@
# frozen_string_literal: true

require_relative "finder"
require_relative "relaton/api"

module Relaton
class Api
class << self
#
# AWS Lambda handler
#
# @param [Hash] event
# @option event [String] :path
# @option event [Hash] :queryStringParameters
#
# @param [Hash] context
#
# @return [Hash] AWS Lambda response
#
def handler(event:, context: {}) # rubocop:disable Lint/UnusedMethodArgument
router(event) || resource_not_exist
rescue StandardError => e
puts "Execution error!"
puts e.message
puts e.backtrace
end

private

#
# Route request
#
# @param [Hash] event
# @option event [String] :path
# @option event [Hash] :queryStringParameters
#
# @return [Hash] AWS Lambda response
#
def router(event) # rubocop:disable Metrics/MethodLength
case event["path"]
when /\/api\/v1\/document$/
case event["httpMethod"]
when "GET" then fetch event
end
when /\/api\/v1\/version$/
case event["httpMethod"]
when "GET" then version event["queryStringParameters"]&.fetch("format")
end
end
end

#
# Formatted version response
#
# @param [String, nil] format
#
# @return [Hash] <description>
#
def version(format) # rubocop:disable Metrics/MethodLength
version = ENV.fetch "API_VERSION"
case format
when "xml"
xml = "<version><release>#{version}</release><relaton>#{Relaton::VERSION}</relaton></version>"
response xml, type: "text/xml"
when "json"
json = { release: version, relaton: Relaton::VERSION }.to_json
response json, type: "application/json"
else
response "Release: #{version}, Relaton version: #{Relaton::VERSION}"
end
end

#
# Look up a document
#
# @param [Hash] event
# @option event [Hash] :queryStringParameters
#
# @return [Hash] AWS Lambda response
#
def fetch(event) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
if event["queryStringParameters"].nil?
return bad_request "Parameters are missed or incorrect. "\
"See the documentation https://github.com/relaton"\
"/api.relaton.org#fetch-bibdata-of-a-document"
elsif event["queryStringParameters"]["code"].nil?
return bad_request "Parameter 'code' is required."
end

item = Relaton::Finder.instance.fetch(*params(event))
return not_found "Document not found." unless item

xml = item.to_xml bibdata: true
response xml, type: "text/xml"
rescue Aws::Xml::Parser::ParsingError
bad_request "Parameter 'code' contains invalid symbols. "\
"See this guide https://docs.aws.amazon.com/AmazonS3/"\
"latest/userguide/object-keys.html"
rescue RelatonBib::RequestError => e
service_unavailable e.message
end

#
# Parameters for document fetching
#
# @param [Hash] event
# @option event [Hash] :queryStringParameters
#
# @return [Array<String, Hash>]
#
def params(event)
allowed_params = %w[all_parts keep_year]
opts = event["queryStringParameters"].each_with_object({}) do |(k, v), o|
allowed_params.include?(k) && o[k.to_sym] = v
end

[
event["queryStringParameters"]["code"],
event["queryStringParameters"]["year"],
opts,
]
end

#
# Respond resourse not exist
#
# @return [Hash] AWS Lambda response
#
def resource_not_exist
not_found "Resource doesn't exist."
end

#
# Bad request response
#
# @param [String] msg
#
# @return [Hash] AWS Lambda response
#
def bad_request(msg)
response "Bad request. #{msg}", status: 400
end

#
# Not found response
#
# @param [String] msg
#
# @return [Hash] AWS Lambda response
#
def not_found(msg)
response msg, status: 404
end

#
# Service unavailable respoonse
#
# @param [String] msg message
#
# @return [Hash] AWS Lambda response
#
def service_unavailable(msg)
response msg, status: 503
end

#
# AWS Lambda response
#
# @param [String] body
# @param [String] type
# @param [Integer] status
#
# @return [Hash] AWS Lambda response
#
def response(body, type: "text/plain", status: 200)
{
statusCode: status,
headers: {
"Content-Type" => type,
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Headers" => "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
},
body: body,
}
end
end
end
end
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8
29 changes: 0 additions & 29 deletions lib/finder.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/override/relaton/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize(global_cache, local_cache)
@registry = Relaton::Registry.instance
@db = open_cache_biblio(global_cache, type: :global)
@local_db = open_cache_biblio(local_cache, type: :local)
# @static_db = open_cache_biblio "static_cache"
@queues = {}
@semaphore = Mutex.new
end

private
Expand Down
21 changes: 0 additions & 21 deletions lib/override/relaton_calconnect/hit.rb

This file was deleted.

14 changes: 0 additions & 14 deletions lib/override/relaton_calconnect/hit_collection.rb

This file was deleted.

Loading
Loading