Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
  bin
  lib
  Gemfile
  README.md
  uoy-faculty-sinatra-aws.gemspec
Size: Mime:
  README.md

Sinatra AWS Helpers

Installation

Add these lines to your application's Gemfile:

source 'https://gem.fury.io/universityofyork/' do
  gem 'aws-sessionstore-dynamodb', '~> 1'
  gem 'uoy-faculty-sinatra-aws', require: 'sinatra/faculty/aws'
end

aws-sessionstore-dynamodb must be declared separately; we want the local version from gemfury, not the one from rubygems, and declaring it as a dependency in gemspec will pull the rubygems version.

And then execute:

$ bundle

Or install it yourself as:

$ gem install uoy-faculty-sinatra-aws

Usage

Lambda Handler

Call this from the lambda handler function defined in your app. For example, in a lambda.rb file:

# frozen_string_literal: true

require 'rack'
require 'sinatra/lambda_handler'

# Global object that responds to the call method. Stay outside of the handler to take advantage of container reuse
$app ||= Rack::Builder.parse_file("#{__dir__}/config.ru").first # rubocop:disable Style/GlobalVars
ENV['RACK_ENV'] ||= 'production'

def lambda_handler(event:, context:)
  Sinatra::LambdaHandler.handler(event: event, context: context)
end

This allows us to leave the handler in Cloudformation as lambda.lambda_handler but centralise the handler function.

Functions for use within a configure block

  • in_lambda?
    • Helper to check if we're running in a lambda
  • app_secrets
    • Returns the secret defined in APP_SECRET_NAME as a hash
  • aws_config
    • Sets up configuration for a lambda app in an AWS lambda
      • path=''
      • Gets a token for connecting to the DB and connects
      • Sets up DynamoDB for sessions
      • Sets up Cognito for authentication
configure do
  if in_lambda?
    aws_config
  else
    ...
  end
end

Cognito Auth

In your app.rb:

configure do
  if in_lambda?
    register Sinatra::CognitoAuth
  else
    ...
  end
end

Development

Versioning

Your Gem's version is picked up automatically from lib/sinatra/faculty/aws.rb. When any changes are pushed to master, after the normal CI tasks the pipeline will push to gemfury automatically. The usual workflow is:

  • For minor changes, update VERSION and make the change in a single commit

  • For anything else, create a branch and set VERSION to the version you're aiming to release for. Make the changes; when the branch is merged, the gem will be uploaded.

Note that gemfury will never overwrite an existing gem version, even if the old one is yanked!

Running tests

Tests can be run via rake: bundle exec rake spec - this doesn't run performance tests; they can be run separately via bundle exec rake perf.

You can also run rspec normally e.g. bundle exec rspec -fd.

Identifying Performance Tests

If you have performance tests that take a while, tag the context / describe block like this:

context 'when foo is bar baz', :perf do
  ...
end

Contributing

Bug reports and pull requests are welcome at https://github.com/university-of-york/faculty-dev-sinatra-aws-gem