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.
15 16 17 18 19 20 21 22 23 |
# File 'lib/octodown/renderer/server.rb', line 15 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.
13 14 15 |
# File 'lib/octodown/renderer/server.rb', line 13 def file @file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/octodown/renderer/server.rb', line 13 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/octodown/renderer/server.rb', line 13 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/octodown/renderer/server.rb', line 13 def path @path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/octodown/renderer/server.rb', line 13 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.
66 67 68 |
# File 'lib/octodown/renderer/server.rb', line 66 def app @app ||= Rack::Cascade.new([self, Rack::File.new(path)]) end |
#boot_server ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/octodown/renderer/server.rb', line 35 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
60 61 62 |
# File 'lib/octodown/renderer/server.rb', line 60 def call(env) ::Faye::WebSocket.websocket?(env) ? render_ws(env) : render_http(env) end |
#maybe_launch_browser ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/octodown/renderer/server.rb', line 45 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/octodown/renderer/server.rb', line 25 def present register_listener Thread.new do maybe_launch_browser end boot_server end |