1

Basically I have multiple API applications in one Sinatra / GrapeAPI file

require 'sinatra'
require 'grape'
require 'webrick'
require 'webrick/https'
require 'openssl'

CERT_PATH = '/opt/myCA/server/'

class WebApp  < Sinatra::Base
  post '/' do
    "Hellow, world!"
  end            
end

class Api1  < Grape::API
  get '/test1' do
     {xyz: 'test1' }
  end            
end

class Api2  < Grape::API
  get '/test2' do
    {xyz: 'test2' }
  end          
end

webrick_options1 = {
        :Port               => 8443,
        :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
        :DocumentRoot       => "/ruby/htdocs",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "my-server.crt")).read),
        :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "my-server.key")).read),
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ]
}
webrick_options2 = {...}

Rack::Handler::WEBrick.run Api1, webrick_options1 # this will work
Rack::Handler::WEBrick.run Api2, webrick_options2 # but when I try another one, 
                                                 # the other will not 

Grape gem is recommending to use cascade:

run Rack::Cascade.new [Api1, Api2, WebApp]

However this will ignore my precious SSL settings.

How can I run multiple server configurations?

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
equivalent8
  • 13,754
  • 8
  • 81
  • 109
  • 1
    I asked a similar (I believe) question, and this was the answer I got: http://stackoverflow.com/a/16348440/335847 – ian Oct 07 '14 at 22:12
  • @iain thx, I was afraid that this will be the case. I'll try unicorn. – equivalent8 Oct 08 '14 at 08:27
  • 1
    Is it just for the SSL or to run several servers? The SSL is probably best handled by a proxy like Nginx anyway. – ian Oct 08 '14 at 13:00
  • @iain I agree, good point, however I'm doing something really stupid: a simple replica of API (our clients), upon which while developing the main project I make all the requests. So this is just Sinatra app on my localhost, not the real API. Yes I could configure localhost to use NginX it's just I'm trying to avoid that if possible so that other developers don't have to configure NginX just to use this "dummy API". (at this point we don't us Chef or Docker that would simplify this ) – equivalent8 Oct 12 '14 at 20:09
  • Yeah, Chef/Docker/Puppet et al would be a good idea, but the Nginx install isn't bad. You really just need the config file, and from having tried something similar, it's definitely easier! :) – ian Oct 12 '14 at 20:11
  • @iain can you please create brief answer with what we discussed here (that it's not possible with webrick, to use Unicorn on other more comprehensive server, or use NginX)? so I can accept it (so you get the credits) ... I guess you covered pretty much all the possible cases :) – equivalent8 Oct 13 '14 at 10:25
  • Sure, but it made me think that a reverse proxy for Ruby would be good, and checked to see if anyone had done one (which I should've done in the first place) http://rubygems.org/gems/rack-reverse-proxy. If that works then you can have the honour of answering :) – ian Oct 14 '14 at 03:34

0 Answers0