Sha256: 534e10ce645bc8f00d4e31f2efdf9c9fa93cb30cb64fecd56cdf9616a71006ec

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

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 :environment, :production
          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

2 entries across 2 versions & 1 rubygems

Version Path
mado-0.2.1 lib/mado/server.rb
mado-0.2.0 lib/mado/server.rb