Skip to content

Commit f43269f

Browse files
committed
Add web host configuration
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.
1 parent 6cf4881 commit f43269f

File tree

8 files changed

+52
-14
lines changed

8 files changed

+52
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ This browser front-end supports the following features:
142142
* Upload and create new files within your shares
143143
* Manage share settings and other configuration options
144144

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

147147
### Conflict Resolution
148148

lib/gitdocs/cli.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.source_root
1515
desc 'start', 'Starts a daemonized gitdocs process'
1616
method_option :foreground, type: :boolean, aliases: '-fg'
1717
method_option :verbose, type: :boolean, aliases: '-v'
18+
method_option :host, type: :string, aliases: '-h'
1819
method_option :port, type: :string, aliases: '-p'
1920
method_option :pid, type: :string, aliases: '-P'
2021
def start
@@ -28,12 +29,12 @@ def start
2829
if options[:foreground]
2930
say 'Run in the foreground', :yellow
3031
Gitdocs::Initializer.foreground = true
31-
Manager.start(web_port)
32+
Manager.start(web_host, web_port)
3233
else
3334
# Clear the arguments so that they will not be processed by the
3435
# Dante execution.
3536
ARGV.clear
36-
runner.execute { Manager.start(web_port) }
37+
runner.execute { Manager.start(web_host, web_port) }
3738

3839
if running?
3940
say 'Started gitdocs', :green
@@ -139,14 +140,15 @@ def status
139140
end
140141

141142
desc 'open', 'Open the Web UI'
143+
method_option :host, type: :string, aliases: '-h'
142144
method_option :port, type: :string, aliases: '-p'
143145
def open
144146
unless running?
145147
say 'Gitdocs is not running, cannot open the UI', :red
146148
return
147149
end
148150

149-
Launchy.open("http://localhost:#{web_port}/")
151+
Launchy.open("http://#{web_host}:#{web_port}/")
150152
end
151153

152154
# TODO: make this work
@@ -195,6 +197,13 @@ def web_port
195197
result.to_i
196198
end
197199

200+
# @return [String]
201+
def web_host
202+
result = options[:host]
203+
result ||= Configuration.web_frontend_host
204+
result
205+
end
206+
198207
# @param [String] path
199208
# @return [String]
200209
def normalize_path(path)

lib/gitdocs/configuration.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def self.web_frontend_port
1414
Config.global.web_frontend_port
1515
end
1616

17+
# @return [String]
18+
def self.web_frontend_host
19+
Config.global.web_frontend_host
20+
end
21+
1722
# @param [Hash] new_config
1823
def self.update(new_config)
1924
Config.global.update_attributes(new_config)
@@ -23,10 +28,12 @@ def self.update(new_config)
2328
# database table. There are other ways to achieve this, but this seemed most
2429
# clear for now. [2015-06-26 -- acant]
2530
#
26-
# @!attribute start_frontend_port
31+
# @!attribute start_web_frontend
2732
# @return [Boolean] defaults to true
2833
# @!attribute web_frontend_port
2934
# @return [Integer] defaults to 8888
35+
# @!attribute web_frontend_host
36+
# @return [String] defaults to '127.0.0.1'
3037
class Config < ActiveRecord::Base
3138
# @return [Gitdocs::Configuration::Config]
3239
def self.global

lib/gitdocs/manager.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def self.listen_method
2121
:notification
2222
end
2323

24-
# @param [Integer] web_port
24+
# @param [String] host
25+
# @param [Integer] port
2526
# @return [void]
26-
def start(web_port)
27+
def start(host, port)
2728
Gitdocs.log_info("Starting Gitdocs v#{VERSION}...")
2829
Gitdocs.log_info(
2930
"Using configuration root: '#{Initializer.root_dirname}'"
@@ -48,8 +49,8 @@ def start(web_port)
4849
args: [
4950
app,
5051
{
51-
Host: '127.0.0.1',
52-
Port: web_port,
52+
Host: host,
53+
Port: port,
5354
quiet: true
5455
}
5556
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- encoding : utf-8 -*-
2+
3+
class AddWebHostToConfig < ActiveRecord::Migration
4+
def self.up
5+
add_column :configs, :web_frontend_host, :string, default: '127.0.0.1'
6+
end
7+
8+
def self.down
9+
fail
10+
end
11+
end

lib/gitdocs/views/settings.haml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
%h2 Gitdocs
66
.field.config#config
77
%dl
8+
%dt Web Frontend Host
9+
%dd
10+
%input{ type: 'input', name: 'config[web_frontend_host]', value: Gitdocs::Configuration.web_frontend_host }
811
%dt Web Frontend Port
912
%dd
1013
%input{ type: 'input', name: 'config[web_frontend_port]', value: Gitdocs::Configuration.web_frontend_port }

test/integration/test_helper.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
require_relative filename
1616
end
1717

18-
Capybara.app_host = 'http://localhost:7777/'
18+
Capybara.app_host = 'http://127.0.0.1:7777/'
1919
Capybara.default_driver = :poltergeist
2020
Capybara.run_server = false
21-
Capybara.default_max_wait_time = ENV['TRAVIS'] ? 120 : 30
21+
Capybara.default_max_wait_time = ENV['TRAVIS'] ? 120 : 120
2222

2323
Capybara.register_driver :poltergeist do |app|
2424
Capybara::Poltergeist::Driver.new(
@@ -170,7 +170,11 @@ def gitdocs_command(method, *args)
170170
# @return [void]
171171
def gitdocs_start
172172
FileUtils.rm_rf(PID_FILE)
173-
gitdocs_command('start', '--verbose', '--port=7777', 'Started gitdocs')
173+
gitdocs_command(
174+
'start',
175+
'--verbose', '--host=127.0.0.1', '--port=7777',
176+
'Started gitdocs'
177+
)
174178
end
175179

176180
# @overload abs_current_dir
@@ -261,7 +265,7 @@ def wait_for_assert(interval = 0.1)
261265
# @return [void]
262266
def visit_and_click_link(locator)
263267
wait_for_assert(1) do
264-
visit('http://localhost:7777/')
268+
visit('http://127.0.0.1:7777/')
265269
click_link(locator)
266270
end
267271
end

test/unit/configuration_test.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
describe 'Config.update' do
1111
before do
1212
Gitdocs::Configuration.update(
13-
'start_web_frontend' => false, 'web_frontend_port' => 9999
13+
start_web_frontend: false,
14+
web_frontend_port: 9999,
15+
web_frontend_host: 'example.com'
1416
)
1517
end
1618

1719
it { Gitdocs::Configuration.start_web_frontend.must_equal(false) }
1820
it { Gitdocs::Configuration.web_frontend_port.must_equal(9999) }
21+
it { Gitdocs::Configuration.web_frontend_host.must_equal('example.com') }
1922
end
2023
end

0 commit comments

Comments
 (0)