Class: Rackula::Command::Generate

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/rackula/command/generate.rb

Overview

Server setup commands.

Instance Method Summary collapse

Instance Method Details

#callObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rackula/command/generate.rb', line 100

def call
	# We set the default RACK_ENV to static unless it was already set to something.
	ENV['RACK_ENV'] ||= 'static'
	
	Async::Reactor.run do
		endpoint = Async::IO::Endpoint.tcp("localhost", 0, reuse_port: true)
		
		# We bind to a socket to generate a temporary port:
		endpoint.bind do |socket|
			run(socket.local_address, Pathname.new(parent.root))
		end
	end
end

#copy_and_fetch(port, root) ⇒ Object



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
# File 'lib/rackula/command/generate.rb', line 48

def copy_and_fetch(port, root)
	output_path = root + @options[:output_path]
	
	if output_path.exist?
		if @options[:force]
			# Delete any existing stuff:
			FileUtils.rm_rf(output_path.to_s)
		else
			# puts "Failing due to error..."
			raise Samovar::Failure.new("Output path already exists!")
		end
	end

	# Create output directory
	Dir.mkdir(output_path)
	
	# Copy all public assets:
	asset_pattern = root + @options[:public] + '*'
	Dir.glob(asset_pattern.to_s).each do |path|
		FileUtils.cp_r(path, File.join(output_path, "."))
	end
	
	# Generate HTML pages:
	system("wget", "--mirror", "--recursive", "--continue", "--convert-links", "--adjust-extension", "--no-host-directories", "--directory-prefix", output_path.to_s, "http://localhost:#{port}")
end

#run(address, root) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rackula/command/generate.rb', line 88

def run(address, root)
	endpoint = Async::HTTP::URLEndpoint.parse("http://localhost", port: address.ip_port, reuse_port: true)
	
	puts "Setting up container to serve site on port #{address.ip_port}..."
	container = serve(endpoint, root)
	
	# puts "Copy and fetch site to static..."
	copy_and_fetch(address.ip_port, root)
ensure
	container&.stop
end

#serve(endpoint, root) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rackula/command/generate.rb', line 74

def serve(endpoint, root)
	container = Async::Container::Threaded.new
	
	config_path = root + @options[:config]
	rack_app, options = Rack::Builder.parse_file(config_path.to_s)
	
	app = ::Falcon::Server.middleware(rack_app, verbose: @options[:verbose])
	server = ::Falcon::Server.new(app, endpoint)
	
	container.run(count: @options[:count]) do
		server.run
	end
end