Sha256: 19754118c3d8eb63b34321f91550e9ea33b518e78f5887f530b476f51f30ddde

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

require 'rack'
require 'rack/static'
require 'tempfile'

module Spade
  module Server
    def self.run(working, port)
      rootdir = Spade.discover_root(working)
      static = Rack::Static.new(nil, :urls => ['/'], :root => rootdir)
      static = CommandRunner.new(static)
      static = Rack::ShowStatus.new(Rack::ShowExceptions.new(static))

      Rack::Handler::WEBrick.run static, :Port => port.to_i
    end

    def self.shutdown
      Rack::Handler::WEBrick.shutdown
    end

    class CommandRunner
      def initialize(app)
        @app = app
      end

      #FIXME: This is not very safe, we should have some restrictions
      def call(env)
        if env['PATH_INFO'] == '/_spade/command'
          rack_input = env["rack.input"].read
          params = Rack::Utils.parse_query(rack_input, "&")

          command_path = params['command']
          return [500, {}, "command required"] if command_path.nil? || command_path.empty?

          if ((root = params['pkgRoot']) && !root.nil?)
            command_path = File.expand_path(File.join(command_path), root)
          end

          tempfile = Tempfile.new('spade-server')
          tempfile.write(params['code'])
          tempfile.close

          puts "Running: #{command_path}"
          output = `#{command_path} < #{tempfile.path}`

          tempfile.delete

          [200, {}, output]
        else
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spade-0.0.5 lib/spade/server.rb
spade-0.0.4 lib/spade/server.rb
spade-0.0.3 lib/spade/server.rb
spade-0.0.2 lib/spade/server.rb
spade-0.0.1 lib/spade/server.rb