Sha256: f6b776ccddfbddac4504e381893688338bbffd1b70465092a0ece283f9448fe4

Contents?: true

Size: 1.49 KB

Versions: 9

Compression:

Stored size: 1.49 KB

Contents

require 'rack'
require 'listen'

module Flutterby
  class Server
    def initialize(root, port: 4004)
      @root = root
      @port = port
    end

    def run!
      # Set up listener
      listener = Listen.to(@root.fs_path) do |modified, added, removed|
        # puts "modified absolute path: #{modified}"
        # puts "added absolute path: #{added}"
        # puts "removed absolute path: #{removed}"

        Flutterby.logger.info "Change detected, reloading everything!"
        @root.reload!
        @root.stage!
      end

      # Set up server
      server = Rack::Handler::WEBrick

      # Make sure we handle interrupts correctly
      trap('INT') do
        listener.stop
        server.shutdown
      end

      # Go!
      listener.start
      server.run self, Port: @port, Logger: Flutterby.logger
    end

    def call(env)
      req  = Rack::Request.new(env)
      res = Rack::Response.new([], 200, {})

      # Look for target node in path registry
      if node = find_node_for_path(req.path)
        # Determine MIME type
        mime_type = MIME::Types.type_for(node.ext) || "text/plain"

        # Build response
        res.headers["Content-Type"] = mime_type
        res.body = [node.render]
      else
        res.status = 404
        res.headers["Content-Type"] = "text/html"
        res.body = ["404"]
      end

      res
    end

    def find_node_for_path(path)
      @root.paths[path] ||
      @root.paths[path + ".html"] ||
      @root.paths[::File.join(path, "index.html")]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
flutterby-0.1.0 lib/flutterby/server.rb
flutterby-0.0.25 lib/flutterby/server.rb
flutterby-0.0.24 lib/flutterby/server.rb
flutterby-0.0.23 lib/flutterby/server.rb
flutterby-0.0.22 lib/flutterby/server.rb
flutterby-0.0.21 lib/flutterby/server.rb
flutterby-0.0.20 lib/flutterby/server.rb
flutterby-0.0.19 lib/flutterby/server.rb
flutterby-0.0.18 lib/flutterby/server.rb