Michalos
Michalos

Reputation: 21

problem with task rake, ruby

I have got a task in rake that run my server sinatra , it doesn't work , the same script in ruby works. Why ?? can I run server sinatra in rake task??

task :server do

begin
require 'rubygems' 
require 'sinatra' 
rescue LoadError
  p "first install sinatra using:"
  p "gem install sinatra"
  exit 1
end

get '/:file_name' do |file_name|
  File.read(File.join('public', file_name))
end

exit 0
end

Upvotes: 2

Views: 462

Answers (1)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

Create a class that is inherited from a Sinatra::Base class

#app.rb
require 'sinatra'

class TestApp < Sinatra::Base
  get '/' do
    "Test"
  end
end

And then run your application from rake:

#Rakefile
$:.unshift File.join(File.dirname(__FILE__), ".")

require 'rake'
require 'app' 

task :server do
  TestApp.run!
end

Upvotes: 3

Related Questions