Skip to content

Repository files navigation

Casino - Timeslot Rails Backend

Architecture

Casino diagram

Notes

No delete & destroy

Delete and Destroy are globally disabled in lib/ts_prevent_deletion.rb, instead we use a db-column called deleted_at or set a specific state on the model. There is one exception, Device(s) can be removed from the db.

Authorization

Authorization is done via a token in the http header which is compared to a locally saved token for every user. The token is invalidated on logout and regenerated on login. On first signup the token is also created so a signed up user is already logged in.

Authentication

We use Pundit for Authentication. In the app/policies/ folder are all files which contain authentication logic.

Environment

We develop on OSX and Ubuntu.

Env Variables

Setting environment variables can be done e.g. via .env or with another approach which we use atm, but we'll might switch to Rails secrets.yaml. If deploying to AWS with ElasticBeanstalk the secret environment variables like database credentials are fetched from an S3 Bucket ( "https://s3.eu-central-1.amazonaws.com/timeslot.casino.beanstalk.configs/") and are expected to be defined in a file called '{ELASTICBEANSTALK_ENVIRONMENT_NAME}_config_variables.rb'. In this file the variables are defined in the same way as below. On a local machine the configs are expected at ```secret-configs`` folder, which is not checked into version control.

ENV['MAX_THREADS'] # number of concurrent Puma Webserver threads, defaults to 5 if not set
ENV['NOTIFICATION_WORKERS'] # number of concurrent SuckerPunch Notification Workers, defaults to 5 if not set
ENV['INLINE_WORKERS'] = 'false' # controls if sucker punch jobs run async
ENV['DB_POOL'] # number of available database connections, defaults to MAX_THREADS or 10 if both are not set, BUT should be at least MAX_THREADS + NOTIFICATION_WORKERS
# maximum on heroku free plan is 20

ENV['PG_EXPLAIN'] = 'true' # show output of pg EXPLAIN ANALYZE for all SELECT queries, use carefully

# pagination
ENV['PAGINATION_DEFAULT_FILTER'] = none' # if not provided by client
ENV['PAGINATION_DEFAULT_MODE'] = 'all' # if not provided by client
ENV['PAGINATION_DEFAULT_LIMIT'] = '40' # if not provided by client
ENV['PAGINATION_MAX_LIMIT'] = '100' # depends on the environment...

ENV['TS_SLOT_WEBSHARING_URL'] = 'http://timesl.ot/' # domain name for the slot websharing service app, given we have one
ENV['WEB_IMPORTER_PASSWORD'] = 'Q8NUwaR4' # password used by web-import service
ENV['WEB_CONCURRENCY] = 2 # number of puma workers

# maintenance
ENV['ENABLE_IOS_DB_CLEAN'] = 'true' # to enable the endpoint for db cleaning

# let the frontend know if it needs to update itself
ENV['IOS_CURRENT_CLIENT_VERSION'] = '1.000'
ENV['IOS_MINIMUM_CLIENT_VERSION'] = '1.000'
ENV['ANDROID_CURRENT_CLIENT_VERSION'] = '1.000'
ENV['ANDROID_MINIMUM_CLIENT_VERSION'] = '1.000'

Internal Services

Search Service (Elastic Search, Crawler)

The Data Team provides an elasticSearch - Slot Search Interface for it's crawler data.

ENV['TS_GLOBALSLOTS_SEARCH_SERVICE_URL']) # elastic search url
ENV['TS_GLOBALSLOTS_SEARCH_SERVICE_NAME'] # username
ENV['TS_GLOBALSLOTS_SEARCH_SERVICE_PASSWORD'] # password

Global Slot Retrieval Service

Allows to get a Global Slot by its muid (slot_uuid).

ENV['TS_DATA_MALL_URL']) # data mall url
ENV['TS_DATA_MALL_NAME'] # username
ENV['TS_DATA_MALL_PASSWORD'] # password

External Services

The following env variables are available/expected:

AWS

Docs

ENV['AWS_ACCESS_KEY_ID']
ENV['AWS_SECRET_ACCESS_KEY']
ENV['AWS_REGION']

We use the following services

ENV['AWS_PLATFORM_APPLICATION_IOS'] # aws arn endpoint (iOS)

# enable push endpoints, if not set explicitly endpoint is enabled by default
# ENV['PUSH_DEFAULT'] = 'true'
ENV['PUSH_APNS'] = 'false'
ENV['PUSH_APNS_SANDBOX'] = 'true'

Cloudinary

Cloud Service for our Media Data, via Heroku Addon, Docs.

For local testing a free account can be opened and the following vars need to be set:

ENV['TS_RAILS_BACKEND_CLOUDINARY_CLOUD_NAME']
ENV['TS_RAILS_BACKEND_CLOUDINARY_API_KEY']
ENV['TS_RAILS_BACKEND_CLOUDINARY_API_SECRET']

Redis

Redis is to store our Activity Streams via a service like Heroku Redis Addon. The following var need to be set:

ENV['REDIS_URL']

For local testing you need a locally running redis server listening on localhost:6379

Airbrake

for exception monitoring, via Heroku Addon, Docs | Github

Papertrail

Log management service, collects logs from AWS Instances via remote_syslog2 deamon, which gets installed on all EC2 instances via ebextensions config.

Papertrail Account for Dev environments is registered with email casino-dev@timeslot.rocks, Password is in LastPass Company Account.

For ease of use check out Papertrail CLI.

  • sometimes the rsyslog deamon stops working on the ec2 instance
  • to restart ssh into ec2 & sudo /sbin/service remote_syslog restart

More Tools

Webserver (Puma)

  • Start default rails dev server (s=server)
rails s

Specs / Tests

Flags

Flag Effect
:focus let only marked specs run
:db print the database interactions to the console, does also :focus the marked spec
:explain print postgres EXPLAIN ANALYZE for SELECT queries to console, does also :focus the marked spec
:commit persist data in database, see below
:seed load database seeds
:keep_data doesn't clean metaslot, baseslot, stdslot, reslot, friendship & user table for marked group, cleans after group has run, for read-only specs
:vcr use vcr to mock external requests, see below
:aws use aws to mock external requests to AWS service, see below
:redis use redis to indicates that data is stored into redis which has to be cleaned after each test
:async use async to mock asynchronously requests through sucker punch workers, see below
:activity used to turn on triggering activities (is off by default)

Manages database transactions for specs/tests. Default strategy is transaction. If a spec needs truncation, a :commit flag can be set on the test.

describe "auditing", :commit do
# spec goes here
end

The specs uses the vcr gem (and webmock gem), which records external requests on the first run and on previous runs always returns this response. This makes that specs faster and allows them to be run offline, but also hides if there had been changes in the external API which breaks the comunication.

To enable outgoing http requests in specs (Rails test env) edit the first line in the spec/support/vcr_setup.rb file.

To enable outgoing http requests in the Rails Dev Env there is a setting in the development.rb file:

WebMock.allow_net_connect!

AWS

The specs uses the AWS webmock, which simulate external requests to AWS services. This allows to test in an special environment where the changes would not store live.

The specs uses a webmock to handle asynchronously tasks, which is internally handled by the sucker punch gem. This allows to test worker threads inside rails specs.

Detects n+1 database queries.

Guard

To run specs automatically if a file was edited. At the end of this document is my somewhat messy Guardfile with some customizations.

Debugger / REPL

API Documentation

The documentation is based on the spec/acceptance tests. The DSL is outlined here.

Generating API documentation

rake docs:generate

Per default the documentation is generated as markdown. The generated Markdown files will be in doc/api folder.

Entity-Relationship Diagrams

Can be automatically created in two flavours, both with their up- and downsides:

  • install graphviz with pango/cairo support.
brew install Caskroom/cask/xquartz
brew install graphviz --with-pangocairo
  • gen simple ERD
bin/rake generate_lean_erd
  • gen detailed ERD
bin/rake generate_adv_erd

Git

At the moment we use a mix of the GitFlow Workflow and the Forking Workflow.

Git Flow

brew install git git-flow-avh

Git Commit Commands for JIRA

  • When commiting use the following commit message format:

ACTION KEY #resolved <comment>

  • e.g. git commit -m "ADD BKD-2 including module to stretch lifetime"

  • KEY is the Jira Issue Number

  • ACTION should be one of ADDition, DELetion, MODification, IMProvement, FIX for small bugfixes, WIP if you need to commit while work in progress, WTF in special cases...

  • More detailed information on JIRA smart commit messages here.

However, this is likely to change sooner or later... The JIRA-Github integration is not yet done.

Coding Conventions / Styleguide

  • If patching third party libraries we use a ts_ prefix.
  • The API Responses are in lowerCamelCase, the transformation from snake_case is automatically handled by jbuilder gem
  • rubocop
  • rails best practices

Additional software you may want to use

JSON Tools

Database Tools

Markdown viewers/editors for Mac

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages