Yet Another Followy API built with Rails 7
- Technologies
- Getting Started
- Available Scripts
- Testing
- Linting
- Tools
- Other
- Contributing
- Code of Conduct
- License
Make sure you have git
installed.
To build and run this app locally you will need a few things:
- Use Unix-like OS;
- Install ImageMagick (tested with 7.1.0-44);
- Check you have Cron installed (tested with cronie 1.6.1);
- Install Ruby (strictly higher 3.1.2);
- Install Ruby on Rails (7.0.3);
- Install PostgreSQL (tested with 14.3);
- Install Redis (tested with 7.0.0);
- Install JDK (tested with OpenJDK 17);
- Install Elasticsearch (tested with 8.2.0);
- Check you have cgroups V2 with
memory
controller (otherwise there will be ElasticSearch error); - Install Nginx (tested with 1.20.2);
or
- Install Docker;
- Install docker-compose;
Clone this repository:
git clone git@github.com:maxbarsukov/tollowy-api.git
Install dependencies:
bundle install
Setup git hooks:
overcommit --install
Setup CrystalBall
for regression test selection :
CRYSTALBALL=true DONT_GENERATE_REPORT=true bin/tests
To setup PgHero
you should enable query stats.
Add the following to your postgresql.conf
:
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all
pg_stat_statements.max = 10000
track_activity_query_size = 2048
Then restart PostgreSQL. As a superuser from the psql
console, run:
CREATE extension pg_stat_statements;
SELECT pg_stat_statements_reset();
For security, Postgres doesn’t allow you to see queries from other users without being a superuser. However, you likely don’t want to run PgHero
as a superuser. You can use SECURITY DEFINER
to give non-superusers access to superuser functions.
With a superuser, run:
$ psql -U postgres -d tollowy_production -a -f bin/pghero-permissions.sql -v pghero_password="'pghero_user_password'" -v database_name=tollowy_production
$ psql -U postgres -d tollowy_development -a -f bin/pghero-permissions.sql -v pghero_password="'pghero_user_password'" -v database_name=tollowy_development
Add to elasticsearch.yml
http.cors.allow-origin: "/.*/"
http.cors.enabled: true
network.bind_host: 0.0.0.0
network.host: localhost
discovery.type: single-node
To use whenever
gem you should have cron
installed.
Run whenever --update-crontab <identifier_name>
to update your crontab.
Use --set 'environment=production
argument to set environment.
Need more detailed installation instructions? We have them.
- Check you have installed and configured
PostgreSQL
; - Change
DATABASE_HOST
and other database data in.env
file; - Run
bundle exec rails db:create
to create database; - Run
bundle exec rails db:migrate
to run migrations; - Optional: Run
bundle exec rails db:seed
to seed database; - Check you have installed and configured
Redis
(ping-pong
works); - Check you can start
Sidekiq
; - Check you have installed and configured
Elasticsearch
(curl localhost:9200
or your ownElastic
URL works); - Setup
Elasticsearch
username & password and changr.env
file if needed; - Run
bundle exec rails searchkick:reindex:custom:all
to reindex your models; - Configure and start
Nginx
if needed; - Generate
crontab
withbundle exec whenever --update-crontab tollowy_development
;
Finally, run foreman start
and check your http://localhost:3000
(or :80
if you use Nginx
)
- Repeat 1-12 steps from development guide.
- Configure your
.env
file and runbin/setup-production
; - Execute
bin/docker-init.sql
andbin/pghero-permissions.sql
inpsql
console on your production database;
Finally, run foreman start -f Procfile
to start server in production mode;
- Run
bin/docker-setup
to setup your.env.docker
file anf build containers. - Use
bin/docker-dev-server
to run server in dev mode withmailcatcher
. - Use
bin/docker-prod-server
to run server in production mode.
Some ENV
settings you can use at self-hosted Followy API
:
-
FULL_IP_BAN
(false
is default):- Description: Ban ANY requests for all IPs that are marked as
blocked
- Usage:
FULL_IP_BAN=true
at.env
file or on server start
- Description: Ban ANY requests for all IPs that are marked as
-
DISABLE_RACK_ATTACK
(false
is default):- Description: Disable
Rack::Attack
in production - Usage:
DISABLE_RACK_ATTACK=true
at.env
file or on server start
- Description: Disable
MAIL_USERNAME
: Your mail service username;MAIL_PASSWORD
: Your mail service password;MAIL_ADDRESS
: Your mail service address;MAIL_PORT
: Your mail service port;MAIL_DOMAIN
: Your mail service domain;MAILER_SENDER_ADDRESS
: Your mail sender address (e.gnoreply@followy.ru
);SMTP_OPENSSL_VERIFY_MODE
: Enable/Disable mailer OpenSSL verify mode;
-
CONFIRMATION_TOKEN_LENGTH
(40
is default):- Description: Set length of generated confirmation token;
- Usage:
CONFIRMATION_TOKEN_LENGTH=N
at.env
file or on server start
-
RAILS_LOG_TO_STDOUT
(false
is default):- Description: Enables logging to STDOUT in production;
- Usage:
RAILS_LOG_TO_STDOUT=true
on server start
rails db:seed
– seeds database and exports seeded data to.csv
file by default.- Use
rails db:seed export=false
to not generate csv files. - Use
rails db:seed force=true
to seed db even if there is existing data.
- Use
rails db:seed load=true
– loads data to database fromdb/fixtures/*.csv
files.rails db:kill_postgres_connections
– Kills all PostgreSQL connections.rails pghero:analyze
– RunPgHero
database analyzer.rails pghero:rails pghero:autoindex
– RunPgHero
auto-indexer.rails pghero:capture_query_stats
– RunPgHero
capture query stats.rails pghero:capture_space_stats
– RunPgHero
capture space stats.
rake searchkick:reindex:all
– Reindex all models with defaultSearchkick
tool;rake searchkick:reindex:custom:all
– Custom models reindex with eager loading;
- Run
bundle exec yardoc
to generate app documentation todocs/yard
folder; - Run
bundle exec rails erd
to generate domain model diagram;
bin/clear-sidekiq
to flush existing Sidekiq data.
bin/rswag
to generateswagger.json
file.
swagger_schema
:- run
rails g swagger_schema -h
to see help message; - Examples:
rails g swagger_schema comments/comment --type model
– Create model schema;rails g swagger_schema comments/destroy --type response
– Create response schema;rails g swagger_schema comments/create --type parameter
– Create parameter schema;
- run
- Annotations:
rails annotate_models
to annotate models;rails annotate_routes
to annotate routes;
rails log:clear
- Truncates all/specified *.log files in log/ to zero bytesrails middleware
- Prints out your Rack middleware stackrails secret
- Generate a cryptographically secure secret keyrails stats
- Report code statistics (KLOCs, etc) from the application or enginerails tmp:clear
- Clear cache, socket and screenshot files from tmp/rails tmp:create
- Creates tmp directories for cache, sockets, and pidsrails zeitwerk:check
- Checks project structure for Zeitwerk compatibility
Run bin/tests
or bundle exec rails spec
to launch the test runner.
Run bin/docker-setup
and bin/docker-tests
to launch tests runner in Docker;
bin/rubocop --parallel
to check the quality of code with rubocop;bin/brakeman --quiet --skip-libs --exit-on-warn --no-pager
to run brakeman;bin/bundle-audit update
andbin/bundle-audit
to run bundler-audit;bin/bundle exec rails_best_practices . --spec -c config/rails_best_practices.yml
to run rails_best_practices;bin/bundle exec rake active_record_doctor
to run active_record_doctor;
or run it together with bin/quality
Run bin/docker-setup
and bin/docker-quality
to launch quality checkers in Docker;
Followy API
is an ordinary Rails monolithic MVC application.
- Use interactors to take the logic out of the controller
- Use payloads to build a response using serialized data from
serializers
andinteractor
data; - Use decorators to use an object-oriented layer of presentation logic;
/admin/sign_in
page and sign in as Admin or Developer;/admin
page to use ActiveAdmin dashboard (role ≥ admin);/pghero
page to seePgHero
dashboard (role = developer);/sidekiq
page to seeSidekiq Web
dashboard (role = developer);
All localization files are in the config/locales
folder with ru.yml
and en.yml
files in subfolders;
Default locale is en
, available locales - en
and ru
.
You can change locale with ?locale=ru
parameter in API
request or Admin dashboard URL
To add your own locale, create yourlang.yml
in /locale
folder subfolders and update I18n
config at config/application.rb
file (add new available locale and configure fallbacks).
Use URL parameters to enable/disable rack-mini-profiler
:
?pp=disable
to disable;?pp=enable
to enable;?pp=help
to see help message;
Bullet is a Rails gem that helps to detect N+1
queries and unused eager loading;
- Logs are located at
log/bullet.log
- You can find configuration in
config/environment/*
config files;
Cross-Origin Resource Sharing (CORS) configuration placed at config/initializers/cors.rb
;
You can preview your mails at /rails/mailers/<mailer_name>
;
For example, AuthMailer#password_recovery
preview in HTML format with ru
locale you can find at /rails/mailers/auth_mailer/password_recovery.html?locale=ru
Monkey-patched gems located at config/monkey_patches/*.rb
.
Please, use refinements instead of monkey-patching if ypu can;
- Logs are located at
log/cron_log.log
- Update
crontab
withbundle exec whenever --update-crontab tollowy_development
;
- Use ruby-prof to profile application.
- Use
ProfilingTool
class to profile requests; - You can use
qcachegrind
to visualise profiling information - You can use
tools/benchmark/populate_db_for_posts_votes_bench.rb
to populate database with many users, posts and votes;
Codacy | |
Codecov | |
Coveralls | |
CodeClimate |
|
Codefactor | |
DeepSource |
|
Depfu |
|
Bug reports and pull requests are welcome on GitHub at https://github.com/maxbarsukov/tollowy-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
Everyone interacting in the Tollowy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
The gem is available as open source under the terms of the MIT License. Copyright 2022 Tollowy