Skip to content

Commit

Permalink
Add web host configuration
Browse files Browse the repository at this point in the history
Similar to configuring the web port this option will allow the web host
to be configured in the settings and overridden on the command-line.

The default value will be 127.0.0.1, so that the web interface is not
exposed to non-local traffic by default.
  • Loading branch information
acant committed Sep 9, 2016
1 parent 6cf4881 commit f43269f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ This browser front-end supports the following features:
* Upload and create new files within your shares
* Manage share settings and other configuration options

To check out the front-end, simply visit `http://localhost:8888` whenever gitdocs is running.
To check out the front-end, simply visit `http://127.0.0.1:8888` whenever gitdocs is running.

### Conflict Resolution

Expand Down
15 changes: 12 additions & 3 deletions lib/gitdocs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def self.source_root
desc 'start', 'Starts a daemonized gitdocs process'
method_option :foreground, type: :boolean, aliases: '-fg'
method_option :verbose, type: :boolean, aliases: '-v'
method_option :host, type: :string, aliases: '-h'
method_option :port, type: :string, aliases: '-p'
method_option :pid, type: :string, aliases: '-P'
def start
Expand All @@ -28,12 +29,12 @@ def start
if options[:foreground]
say 'Run in the foreground', :yellow
Gitdocs::Initializer.foreground = true
Manager.start(web_port)
Manager.start(web_host, web_port)
else
# Clear the arguments so that they will not be processed by the
# Dante execution.
ARGV.clear
runner.execute { Manager.start(web_port) }
runner.execute { Manager.start(web_host, web_port) }

if running?
say 'Started gitdocs', :green
Expand Down Expand Up @@ -139,14 +140,15 @@ def status
end

desc 'open', 'Open the Web UI'
method_option :host, type: :string, aliases: '-h'
method_option :port, type: :string, aliases: '-p'
def open
unless running?
say 'Gitdocs is not running, cannot open the UI', :red
return
end

Launchy.open("http://localhost:#{web_port}/")
Launchy.open("http://#{web_host}:#{web_port}/")
end

# TODO: make this work
Expand Down Expand Up @@ -195,6 +197,13 @@ def web_port
result.to_i
end

# @return [String]
def web_host
result = options[:host]
result ||= Configuration.web_frontend_host
result
end

# @param [String] path
# @return [String]
def normalize_path(path)
Expand Down
9 changes: 8 additions & 1 deletion lib/gitdocs/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def self.web_frontend_port
Config.global.web_frontend_port
end

# @return [String]
def self.web_frontend_host
Config.global.web_frontend_host
end

# @param [Hash] new_config
def self.update(new_config)
Config.global.update_attributes(new_config)
Expand All @@ -23,10 +28,12 @@ def self.update(new_config)
# database table. There are other ways to achieve this, but this seemed most
# clear for now. [2015-06-26 -- acant]
#
# @!attribute start_frontend_port
# @!attribute start_web_frontend
# @return [Boolean] defaults to true
# @!attribute web_frontend_port
# @return [Integer] defaults to 8888
# @!attribute web_frontend_host
# @return [String] defaults to '127.0.0.1'
class Config < ActiveRecord::Base
# @return [Gitdocs::Configuration::Config]
def self.global
Expand Down
9 changes: 5 additions & 4 deletions lib/gitdocs/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def self.listen_method
:notification
end

# @param [Integer] web_port
# @param [String] host
# @param [Integer] port
# @return [void]
def start(web_port)
def start(host, port)
Gitdocs.log_info("Starting Gitdocs v#{VERSION}...")
Gitdocs.log_info(
"Using configuration root: '#{Initializer.root_dirname}'"
Expand All @@ -48,8 +49,8 @@ def start(web_port)
args: [
app,
{
Host: '127.0.0.1',
Port: web_port,
Host: host,
Port: port,
quiet: true
}
]
Expand Down
11 changes: 11 additions & 0 deletions lib/gitdocs/migration/008_add_web_host_to_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- encoding : utf-8 -*-

class AddWebHostToConfig < ActiveRecord::Migration
def self.up
add_column :configs, :web_frontend_host, :string, default: '127.0.0.1'
end

def self.down
fail
end
end
3 changes: 3 additions & 0 deletions lib/gitdocs/views/settings.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
%h2 Gitdocs
.field.config#config
%dl
%dt Web Frontend Host
%dd
%input{ type: 'input', name: 'config[web_frontend_host]', value: Gitdocs::Configuration.web_frontend_host }
%dt Web Frontend Port
%dd
%input{ type: 'input', name: 'config[web_frontend_port]', value: Gitdocs::Configuration.web_frontend_port }
Expand Down
12 changes: 8 additions & 4 deletions test/integration/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
require_relative filename
end

Capybara.app_host = 'http://localhost:7777/'
Capybara.app_host = 'http://127.0.0.1:7777/'
Capybara.default_driver = :poltergeist
Capybara.run_server = false
Capybara.default_max_wait_time = ENV['TRAVIS'] ? 120 : 30
Capybara.default_max_wait_time = ENV['TRAVIS'] ? 120 : 120

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(
Expand Down Expand Up @@ -170,7 +170,11 @@ def gitdocs_command(method, *args)
# @return [void]
def gitdocs_start
FileUtils.rm_rf(PID_FILE)
gitdocs_command('start', '--verbose', '--port=7777', 'Started gitdocs')
gitdocs_command(
'start',
'--verbose', '--host=127.0.0.1', '--port=7777',
'Started gitdocs'
)
end

# @overload abs_current_dir
Expand Down Expand Up @@ -261,7 +265,7 @@ def wait_for_assert(interval = 0.1)
# @return [void]
def visit_and_click_link(locator)
wait_for_assert(1) do
visit('http://localhost:7777/')
visit('http://127.0.0.1:7777/')
click_link(locator)
end
end
Expand Down
5 changes: 4 additions & 1 deletion test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
describe 'Config.update' do
before do
Gitdocs::Configuration.update(
'start_web_frontend' => false, 'web_frontend_port' => 9999
start_web_frontend: false,
web_frontend_port: 9999,
web_frontend_host: 'example.com'
)
end

it { Gitdocs::Configuration.start_web_frontend.must_equal(false) }
it { Gitdocs::Configuration.web_frontend_port.must_equal(9999) }
it { Gitdocs::Configuration.web_frontend_host.must_equal('example.com') }
end
end

0 comments on commit f43269f

Please sign in to comment.