MyGit

v1.8

the-teacher/rails7-startkit

版本发布时间: 2023-02-27 21:34:04

the-teacher/rails7-startkit最新发布版本:v2.1(2023-09-18 15:15:47)

Rails 7. Start Kit. Production Mode and Configurations

Screenshot 2023-02-27 at 15 23 11

Install and Run Ruby on Rails now!

Copy & Paste in your terminal

git clone https://github.com/the-teacher/rails7-startkit.git && \
  cd rails7-startkit && \
  bin/setup

Rails 7. My Configuration Approach

It was time when I finally had to manage configuration of my Rails application.

A Rails app always full of configuration parameters. There are some places where you can meet them

It is normal when you need to provide many different parameters for different parts of your "system".

Configuration files and "Classic approach"

Probably you know, that from the very beginning of the software engineering it was fine to have configurations in files. In rails you can find an example of it in config/database.yml. It originally looks like that

development:
  pool: 5
  adapter: mysql2
  encoding: utf8
  database: app_development
  username: root
  password: qwerty
  socket: /tmp/mysql.sock

ENV variables for configuration. "Cloud oriented approach"

After cloud calculations and automatic infrastructure scaling took the world many applications started to use ENV variables for application configuration by default.

Lets take the same config/database.yml and show how it may look in Rails

development:
  pool: 5
  adapter: mysql2
  encoding: utf8
  database: <%= ENV['DATABASE_NAME']%>
  username: <%= ENV['DATABASE_USER']%>
  password: <%= ENV['DATABASE_PASSWORD']%>
  socket: /tmp/mysql.sock

What an approach is better?

If you ask people around "what is better?" they may answer differently. I think it is a "holy war" topic. I can provide only my own opinion:

File configuration

ENV configuration

Synthetic approach

I prefer to use a Synthetic configuration approach. It can be implemented with using to ruby gems

The idea is clear

Synthetic Configuration Approach in Practice

We have ENV variables on the level of the OS, or for local development in .env files

Screenshot 2023-02-27 at 16 14 21

.env file

Screenshot 2023-02-27 at 16 14 47

All this parameters go to config/_CONFIG.yml. This file is being managed by gem config

Screenshot 2023-02-27 at 16 17 27

Now in my application I use so beauty way to setup required configurations:

For database:

Screenshot 2023-02-27 at 16 19 36

For elastic:

Screenshot 2023-02-27 at 16 20 23

Also sometimes it is very great to do something like that Settings.redis.to_hash:

Screenshot 2023-02-27 at 16 24 08

⚠️ Combining 2 different solutions for rails configuration I keep my code clean, maintainable, ready for clouds services, and easy to use.

And one more time:

The idea and following changes was provided in the PR

Production Mode

Finally I spent some time to improve my code to make possible running Rails 7. Start Kit in Production mode. You can easily use production mode, just with setting a required ENV variable.

RAILS_ENV=production bin/setup

RAILS_ENV=production bin/exec start
RAILS_ENV=production bin/exec stop

RAILS_ENV=production bin/exec start_all
RAILS_ENV=production bin/exec stop_all

That is it!

👉 Subscribe to the project to know about most recent updates.

Happy coding with Rails 7. Start Kit

相关地址:原始地址 下载(tar) 下载(zip)

查看:2023-02-27发行的版本