Sha256: f7d4331a2ffb996459a64780cc3792784ad85c44889626d1c39f3cc2586d63a6

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require "em-websocket"
require "eventmachine"

module Mado
  class Server
    def self.run(options)
      EM.kqueue = true if EM.kqueue?

      EM.run do
        sockets = []

        host = options[:host] || "0.0.0.0"
        port = options[:port] || "8080"
        debug = options[:debug] || false
        markdown_path = options[:markdown]

        EM.watch_file(markdown_path, Mado::FileHandler, sockets)

        EM::WebSocket.start(host: host, port: 8081, debug: debug) do |ws|
          ws.onopen do
            ws.send(Mado::Markdown.convert_markdown(markdown_path))
            sockets << ws unless sockets.include?(ws)
          end

          ws.onclose do
            sockets.delete(ws)
          end
        end

        app = Rack::Builder.app do
          Mado::App.set :markdown_path, markdown_path

          map "/" do
            run Mado::App
          end
        end

        Rack::Server.start(app: app, Host: host, Port: port)

        trap("TERM") { EM.stop }
        trap("INT") { EM.stop }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mado-0.1.0 lib/mado/server.rb