Skip to content

設定ファイルを書き出す責務をクラス分割 - #65

Merged
haruyan-hopemucci merged 3 commits into
masterfrom
refactor/extract-renderer-classes
May 13, 2026
Merged

haruyan-hopemucci merged 3 commits into
masterfrom
refactor/extract-renderer-classes

Conversation

@lobin-z0x50

@lobin-z0x50 lobin-z0x50 commented May 13, 2026 •

Copy link
Copy Markdown
Member

#57 (comment)

  • 設定ファイルを書き出す責務をそれぞれのクラスに分割しました。
  • Config クラスから、設定ファイルを書き出す責務がなくなりました。
  • Config#generate_nginx_config がまだ残っていますが、これも将来的にはクラス切り出ししていく流れです。
  • AbstractFactory パターンまで行ければよかったですが、今回はここまで。
  • 8割ぐらいは Copilot に指示してリファクタリングしてもらいました。
  • テストも書いてもらいました。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the configuration generation logic by extracting Nginx and logrotate rendering into dedicated renderer classes and adding corresponding unit tests. Feedback highlights critical issues where Ruby 3.1 keyword argument shorthand is incorrectly applied to methods, which will result in NameErrors. Additionally, a new check for the 'socat' binary in the SocatManager initializer is flagged as out-of-scope and potentially disruptive to environments not requiring socat.

Comment thread src/app/lib/config.rb
if @no_ssl
# HTTPS 非対応
render_nginx template: :http
nginx_renderer.render(template: :http, output_path:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Ruby 3.1 で導入されたキーワード引数の省略記法 (key:) は、ローカル変数に対してのみ有効です。output_path はメソッドであるため、この記法では NameError が発生します。メソッドの結果を渡す場合は、明示的に output_path: output_path と記述する必要があります。

      nginx_renderer.render(template: :http, output_path: output_path)

Comment thread src/app/lib/config.rb
# 証明書ファイルが指定されていなければ、Let's Encrypt を使って生成する
if force_update_cert || !File.exist?("/etc/letsencrypt/live/#{domain}")
render_nginx template: :cert
nginx_renderer.render(template: :cert, output_path:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

前述の通り、output_path はメソッドであるため、キーワード引数の省略記法は使用できません。明示的に output_path: output_path と記述してください。

          nginx_renderer.render(template: :cert, output_path: output_path)

Comment thread src/app/lib/config.rb
end

render_nginx template: :https
nginx_renderer.render(template: :https, output_path:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

前述の通り、output_path はメソッドであるため、キーワード引数の省略記法は使用できません。明示的に output_path: output_path と記述してください。

      nginx_renderer.render(template: :https, output_path: output_path)


def initialize
@dest_list = {} # { dest => unix_socket } な連想配列
raise "socat is not installed" unless system('which socat > /dev/null 2>&1')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

この変更は、設定ファイルの書き出し責務の分割という本 PR の主旨とは無関係な変更(サイドエフェクト)に見えます。また、SocatManager は Singleton であり、initialize で例外を投げると、socat を使用しない設定(アダプターが direct の場合など)であっても SocatManager.instance が呼ばれた時点でアプリケーションがクラッシュしてしまいます。このチェックは、実際に socat を必要とする処理の直前で行うか、あるいは設定を確認して socat が必要な場合にのみ実行されるようにすべきです。

Comment thread src/test/all_test.rb
require_relative 'test_helper'

Dir[File.join(__dir__, "*_test.rb")].sort.each do |file|
Dir[File.join(__dir__, "**/*_test.rb")].sort.each do |file|

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

サブディレクトリが増えたので、その対応


def initialize
@dest_list = {} # { dest => unix_socket } な連想配列
raise "socat is not installed" unless system('which socat > /dev/null 2>&1')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

socat がインストールされているかのチェックを追加

@haruyan-hopemucci haruyan-hopemucci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューOKです! ありがとうございました!

Comment on lines +5 to +10
class BaseRenderer
def initialize(config)
@config = config
end

def render(template_path:, output_path:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +14 to +17
def render(template:, output_path:)
template_path = File.join(TEMPLATE_DIR, "#{template}.erb")
super template_path:, output_path:
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread src/app/lib/config.rb
if @no_ssl
# HTTPS 非対応
render_nginx template: :http
nginx_renderer.render(template: :http, output_path:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

require 'lib/config'
require 'lib/renderers/nginx_by_domain/renderer'

class NginxByDomainRendererTest < Minitest::Test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
レンダラーごとのテストに分割できて保守性が上がったと思います!

@haruyan-hopemucci
haruyan-hopemucci merged commit 86addd4 into master May 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants