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
memorycontroller (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_HOSTand other database data in.envfile; - Run
bundle exec rails db:createto create database; - Run
bundle exec rails db:migrateto run migrations; - Optional: Run
bundle exec rails db:seedto seed database; - Check you have installed and configured
Redis(ping-pongworks); - Check you can start
Sidekiq; - Check you have installed and configured
Elasticsearch(curl localhost:9200or your ownElasticURL works); - Setup
Elasticsearchusername & password and changr.envfile if needed; - Run
bundle exec rails searchkick:reindex:custom:allto reindex your models; - Configure and start
Nginxif needed; - Generate
crontabwithbundle 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
.envfile and runbin/setup-production; - Execute
bin/docker-init.sqlandbin/pghero-permissions.sqlinpsqlconsole on your production database;
Finally, run foreman start -f Procfile to start server in production mode;
- Run
bin/docker-setupto setup your.env.dockerfile anf build containers. - Use
bin/docker-dev-serverto run server in dev mode withmailcatcher. - Use
bin/docker-prod-serverto run server in production mode.
Some ENV settings you can use at self-hosted Followy API:
-
FULL_IP_BAN(falseis default):- Description: Ban ANY requests for all IPs that are marked as
blocked - Usage:
FULL_IP_BAN=trueat.envfile or on server start
- Description: Ban ANY requests for all IPs that are marked as
-
DISABLE_RACK_ATTACK(falseis default):- Description: Disable
Rack::Attackin production - Usage:
DISABLE_RACK_ATTACK=trueat.envfile 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(40is default):- Description: Set length of generated confirmation token;
- Usage:
CONFIRMATION_TOKEN_LENGTH=Nat.envfile or on server start
-
RAILS_LOG_TO_STDOUT(falseis default):- Description: Enables logging to STDOUT in production;
- Usage:
RAILS_LOG_TO_STDOUT=trueon server start
rails db:seed– seeds database and exports seeded data to.csvfile by default.- Use
rails db:seed export=falseto not generate csv files. - Use
rails db:seed force=trueto seed db even if there is existing data.
- Use
rails db:seed load=true– loads data to database fromdb/fixtures/*.csvfiles.rails db:kill_postgres_connections– Kills all PostgreSQL connections.rails pghero:analyze– RunPgHerodatabase analyzer.rails pghero:rails pghero:autoindex– RunPgHeroauto-indexer.rails pghero:capture_query_stats– RunPgHerocapture query stats.rails pghero:capture_space_stats– RunPgHerocapture space stats.
rake searchkick:reindex:all– Reindex all models with defaultSearchkicktool;rake searchkick:reindex:custom:all– Custom models reindex with eager loading;
- Run
bundle exec yardocto generate app documentation todocs/yardfolder; - Run
bundle exec rails erdto generate domain model diagram;
bin/clear-sidekiqto flush existing Sidekiq data.
bin/rswagto generateswagger.jsonfile.
swagger_schema:- run
rails g swagger_schema -hto 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_modelsto annotate models;rails annotate_routesto 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 --parallelto check the quality of code with rubocop;bin/brakeman --quiet --skip-libs --exit-on-warn --no-pagerto run brakeman;bin/bundle-audit updateandbin/bundle-auditto run bundler-audit;bin/bundle exec rails_best_practices . --spec -c config/rails_best_practices.ymlto run rails_best_practices;bin/bundle exec rake active_record_doctorto 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
serializersandinteractordata; - Use decorators to use an object-oriented layer of presentation logic;
/admin/sign_inpage and sign in as Admin or Developer;/adminpage to use ActiveAdmin dashboard (role ≥ admin);/pgheropage to seePgHerodashboard (role = developer);/sidekiqpage to seeSidekiq Webdashboard (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=disableto disable;?pp=enableto enable;?pp=helpto 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
crontabwithbundle exec whenever --update-crontab tollowy_development;
- Use ruby-prof to profile application.
- Use
ProfilingToolclass to profile requests; - You can use
qcachegrindto visualise profiling information - You can use
tools/benchmark/populate_db_for_posts_votes_bench.rbto 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