Class: Octodown::Renderer::Server
- Inherits:
-
Object
- Object
- Octodown::Renderer::Server
- Defined in:
- lib/octodown/renderer/server.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#app ⇒ Object
Cascade through this app and Rack::File app.
- #boot_server ⇒ Object
- #call(env) ⇒ Object
-
#initialize(_content, options = {}) ⇒ Server
constructor
A new instance of Server.
- #maybe_launch_browser ⇒ Object
- #present ⇒ Object
Constructor Details
#initialize(_content, options = {}) ⇒ Server
Returns a new instance of Server.
16 17 18 19 20 21 22 23 24 |
# File 'lib/octodown/renderer/server.rb', line 16 def initialize(_content, = {}) @logger = [:logger] @file = [:file] @options = @path = File.dirname(File.(file.path)) @port = [:port] @websockets = [] @mutex = Mutex.new end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
14 15 16 |
# File 'lib/octodown/renderer/server.rb', line 14 def file @file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/octodown/renderer/server.rb', line 14 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/octodown/renderer/server.rb', line 14 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/octodown/renderer/server.rb', line 14 def path @path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
14 15 16 |
# File 'lib/octodown/renderer/server.rb', line 14 def port @port end |
Instance Method Details
#app ⇒ Object
Cascade through this app and Rack::File app. If Server returns 404, Rack::File will try to serve a static file.
67 68 69 |
# File 'lib/octodown/renderer/server.rb', line 67 def app @app ||= Rack::Cascade.new([self, Rack::File.new(path)]) end |
#boot_server ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/octodown/renderer/server.rb', line 36 def boot_server logger.info "#{file.path} is getting octodown'd" logger.info "Server running on http://localhost:#{port}" Rack::Handler::Puma.run app, Host: 'localhost', Port: port, Silent: true, Threads: '2:8' end |
#call(env) ⇒ Object
61 62 63 |
# File 'lib/octodown/renderer/server.rb', line 61 def call(env) ::Faye::WebSocket.websocket?(env) ? render_ws(env) : render_http(env) end |
#maybe_launch_browser ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/octodown/renderer/server.rb', line 46 def maybe_launch_browser return if [:no_open] sleep 2.5 @mutex.synchronize do if @websockets.empty? logger.info 'Loading preview in a new browser tab' Launchy.open "http://localhost:#{port}" else logger.info 'Re-using existing browser tab' end end end |
#present ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/octodown/renderer/server.rb', line 26 def present register_listener Thread.new do maybe_launch_browser end boot_server end |