/manage-courses-backend

API to serve post graduate teacher training courses, subjects and training providers

Primary LanguageRubyMIT LicenseMIT

Build Status Build Status

Manage Courses Backend

Prerequisites

Native

  • Ruby 2.6.1
  • postgresql-9.6 postgresql-contrib-9.6

Docker

  • docker
  • docker-compose

Setting up the app in development

Native

  1. Run bundle install to install the gem dependencies
  2. Run bundle exec rails db:setup to create a development and testing database
  3. Run bundle exec rails server to launch the app on http://localhost:3001.

Docker

Run this in a shell and leave it running:

docker-compose up --build --detach

You can then follow the log output with

docker-compose logs --follow

The first time you run the app, you need to set up the databases. With the above command running separately, do:

docker-compose exec web /bin/sh -c "bundle exec rails db:setup"

Then open http://localhost:3001 to see the app.

Running specs, linter(without auto correct) and annotate models and serializers

bundle exec rake

Running specs

bundle exec rspec

Linting

It's best to lint just your app directories and not those belonging to the framework:

bundle exec govuk-lint-ruby app config db lib spec --format clang

or

docker-compose exec web /bin/sh -c "bundle exec govuk-lint-ruby app config db lib spec Gemfile --format clang"

Accessing API

V1

See API Docs

Quick check that it's working in local development with the token "bats" configured in config/environments/development.rb:

curl http://localhost:3001/api/v1/2019/subjects.json -H "Authorization: Bearer bats"

V2

Development

In development mode, authenticating with V2 of the API relies on an email address of an existing use in the database being supplied as the bearer token. An example HTTP request would look like:

GET /api/v2/users.json
Authorization: Bearer user@digital.education.gov.uk

or with curl:

curl http://localhost:3001/api/v2/users.json -H "Authorization: Bearer user@digital.education.gov.uk"

Production

In production mode the bearer token is an encrypted JWT with the JSON payload:

{
  "email": "user@digital.education.gov.uk"
}

Encoding the payload can be done with the Ruby jwt gem:

JWT.encode payload, SECRET, 'HS256'

Settings vs config vs Environment variables

Refer to the the config gem to understand the file based settings loading order.

To override file based via Machine based env variables settings

cat config/settings.yml
file
  based
    settings
      env1: 'some file based value'
export SETTINGS__FILE__BASED__SETTINGS__ENV1="machine wins"
puts Settings.file.based.setting.env1

machine wins

Any Machine based env variables settings that is not prefixed with SETTINGS.* are not considered for general consumption.

## CI variables

You'll need to define the AZURE_CR_PASSWORD in Travis in order to successfully build and publish. This can be done using this command:

travis encrypt AZURE_CR_PASSWORD="xxx" --add

Sentry

To track exceptions through Sentry, configure the SENTRY_DSN environment variable:

SENTRY_DSN=https://aaa:bbb@sentry.io/123 rails s