forked from nsidc/search-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_local_webserver.rb
executable file
·94 lines (77 loc) · 2.94 KB
/
run_local_webserver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env ruby
require 'webrick'
require 'open-uri'
require 'yaml'
class SsiHandler < WEBrick::HTTPServlet::FileHandler
def initialize(server, root, options = {})
super
@host = options[:Host]
@enabled = options[:Enabled]
end
def service(req, res)
super
return unless @enabled && res.body.path.include?('.html')
body = res.body.read
body.scan(/\<!--#include virtual=".*" --\>/).each do |match|
sub_start = match.index('"') + 1
sub_end = match.index('"', sub_start)
ssi_file = match[sub_start, sub_end - sub_start]
ssi_content = open("#{@host}#{ssi_file}").read
body.insert(
body.index(match) + match.length,
ssi_content.gsub(%r{href="/}, "href=\"#{@host}/").gsub(%r{src="/}, "src=\"#{@host}/")
)
end
res.body = body
res.content_length = body.length
end
end
def start_webrick(config = {})
local_config = YAML.load_file('config/local_webserver_config.yaml')
if config[:Port].nil?
config.update(Port: local_config['webserver']['local']['port'])
end
server = WEBrick::HTTPServer.new(config)
local_config['webserver']['local']['mount_dirs'].each do |d|
puts "Mounting #{d['file_path']} at #{d['server_path']}"
server.mount(
d['server_path'],
SsiHandler,
d['file_path'],
FancyIndexing: true,
Host: local_config['webserver']['ssi']['host'],
Enabled: local_config['webserver']['ssi']['enabled']
)
end
proxed_paths = {}
proxies = config[:Proxies] || 'search_proxies'
local_config['webserver']['local'][proxies].each do |search_proxy|
next unless search_proxy['enabled']
url = "http://#{search_proxy['remote_host']}:#{search_proxy['port']}#{search_proxy['path']}"
proxed_paths[url] = search_proxy['server_path']
server.unmount search_proxy['server_path']
server.mount_proc search_proxy['server_path'] do |req, res|
search_url = "#{url}?#{req.query_string}"
res.status = 200
res['Content-Type'] = search_proxy['content_type']
res['Access-Control-Allow-Origin'] = '*' # may as well throw in a CORS header...
res['Access-Control-Allow-Methods'] = '*' # may as well throw in a CORS header...
res['Access-Control-Allow-Headers'] = '*' # may as well throw in a CORS header...
res['Access-Control-Request-Method'] = '*' # may as well throw in a CORS header...
# It's hokey, but it seems to work: after pulling the content down from
# the remote server, replace any paths that are proxied by this server.
puts "search_url = #{search_url}"
body = open(search_url).read
proxed_paths.each do |remote_path, local_path|
body = body.gsub(remote_path, "http://#{req.host}:#{req.port}#{local_path}")
end
res.body = body
end
end
yield server if block_given?
%w(INT TERM).each do |signal|
trap(signal) { server.shutdown }
end
server.start
end
start_webrick(DocumentRoot: Dir.pwd, Port: ARGV[0], Proxies: ARGV[1])