lib/zine/server.rb in zine-0.4.0 vs lib/zine/server.rb in zine-0.5.0
- old
+ new
@@ -1,72 +1,73 @@
-require 'highline'
require 'rainbow'
require 'rack'
require 'thin'
require 'zine/upload'
require 'zine/watcher'
module Zine
# Local preview web server
class Server
- # def initialize(_posts, rel_path_build, _rel_path_source, upload_options,
+ # Create a new instance of Server, used to preview the built site locally
+ #
+ # ==== Attributes
+ #
+ # * +rel_path_build+ - string, relative path to the build folder
+ # * +upload_options+ - hash created from the upload section of zine.yaml
+ # * +delete_array+ - array of path strings of files to delete
+ # * +upload_array+ - ditto for files to upload, both can include duplicates
+ #
def initialize(rel_path_build, upload_options, delete_array, upload_array)
@delete_array = delete_array
@upload_array = upload_array
- root = File.absolute_path(rel_path_build)
- motd
+ @thin = create_server File.absolute_path(rel_path_build)
+ motd unless upload_options['test']
+ @thin.start
+ possible_upload rel_path_build, upload_options
+ @thin
+ end
- @thin = Thin::Server.new('127.0.0.1', 8080) do
+ # Stop the server - only used in test
+ def stop
+ @thin.stop
+ end
+
+ private
+
+ def create_server(root)
+ rules = header_rules
+ Thin::Logging.silent = true
+ Thin::Server.new('127.0.0.1', 8080) do
use Rack::Static,
urls: ['/'],
index: 'index.html',
- root: root
- now = Time.now
- a_long_time = 100**4
- run lambda { |_env|
- [200,
- {
- 'Content-Type' => 'text/html',
- 'ETag' => nil,
- 'Last-Modified' => now + a_long_time,
- 'Cache-Control' =>
- 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
- 'Pragma' => 'no-cache',
- 'Expires' => now - a_long_time
- },
- [File.open(File.join(root, 'index.html'), File::RDONLY)]]
- }
+ root: root,
+ header_rules: rules
+ run ->(_env) { [200, {}, ['Have to call run...']] }
end
- @thin.start
-
- return if upload_options['method'] == 'none' ||
- (@delete_array.empty? && @upload_array.empty?)
- cli = HighLine.new
- answer = cli.ask('Upload files? (Y/n)') { |q| q.default = 'Y' }
- return if answer != 'Y'
- file_upload rel_path_build, upload_options
end
- # deploy
- def file_upload(rel_path_build, upload_options)
- puts Rainbow('Connecting...').green
- begin
- upload = Zine::Upload.new rel_path_build, upload_options,
- # @guard.delete_array, @guard.upload_array
- @delete_array, @upload_array
- upload.delete
- upload.deploy
- rescue Errno::ENETUNREACH
- puts Rainbow("Unable to connect to #{upload_options['host']}").red
- rescue Net::SSH::AuthenticationFailed
- puts Rainbow("Authentication failed for #{upload_options['host']}").red
- puts 'Check your credential file, and maybe run ssh-add?'
- end
+ def header_rules
+ [[:all,
+ { 'ETag' => nil,
+ 'Last-Modified' => (Time.now + 100**4).to_s,
+ 'Cache-Control' =>
+ 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
+ 'Pragma' => 'no-cache',
+ 'Expires' => (Time.now - 100**4).to_s }]]
end
def motd
puts "\nPreview running on " +
Rainbow('http://127.0.0.1:8080/').blue.underline +
"\nCommand double click the URL to open, Control C to quit\n"
+ end
+
+ def possible_upload(rel_path_build, upload_options)
+ return if upload_options['method'] == 'none' ||
+ (@delete_array.empty? && @upload_array.empty?)
+ uploader = Zine::Upload.new rel_path_build, upload_options,
+ @delete_array, @upload_array
+ uploader.upload_decision
end
end
end