Bind rails server to 127.0.0.1 by default

前端 未结 4 900
长发绾君心
长发绾君心 2021-02-19 01:09

I\'d like to bind the rails server to 127.0.0.1, instead of 0.0.0.0 so its not accessible when I\'m working from coffee shops.

Is there a configuration file where I can

4条回答
  •  时光取名叫无心
    2021-02-19 01:47

    If you put the default options on config/boot.rb then all command attributes for rake and rails fails (example: rake -T or rails g model user)! So, append this to bin/rails after line require_relative '../config/boot' and the code is executed only for the rails server command:

    if ARGV.first == 's' || ARGV.first == 'server'
      require 'rails/commands/server'
      module Rails
        class Server
          def default_options
            super.merge(Host:  '127.0.0.1', Port: 10524)
          end
        end
      end
    end
    

    The bin/rails file loks like this:

    #!/usr/bin/env ruby
    APP_PATH = File.expand_path('../../config/application',  __FILE__)
    require_relative '../config/boot'
    
    # Set default host and port to rails server
    if ARGV.first == 's' || ARGV.first == 'server'
      require 'rails/commands/server'
      module Rails
        class Server
          def default_options
            super.merge(Host:  '127.0.0.1', Port: 10524)
          end
        end
      end
    end
    
    require 'rails/commands'
    

提交回复
热议问题