Skip to content

Create custom server configuration (example with thin server with ssl in development mode)

randoum edited this page Jan 17, 2014 · 1 revision

This example show how to create a custom server configuration.

We will configure a custom action in zeus to launch thin server with ssl activated in development mode. The same logic can be followed for any similar case

Prerequisites

  • zeus installed and configured
  • thin server

Create custom action

We want to start out server with the command zeus ssl

Edit zeus.json to add ssl command, and bind it to server_ssl method, like so:

{
  "command": "ruby -rubygems -r./custom_plan -eZeus.go",

  "plan": {
    "boot": {
      "default_bundle": {
        "development_environment": {
          ...
          "server_ssl": ["ssl"],
          ...
        },
        ...

Then edit your custom plan to add the method server_ssl:

require 'zeus/rails'

class CustomPlan < Zeus::Rails
  def server_ssl
    require 'rails/commands/server'
    server = ::Rails::Server.new

    Dir.chdir(::Rails.application.root)

    server.start
  end
end

Zeus.plan = CustomPlan.new

As you can notice we simply copied the code from Zeus::Rails.server method. If we now start zeus ssl thin starts with a default configuration

Configuring rails server

Let's start by adding options to the Rails part of the server. We need to change the port and the pid file. We modify our custom plan like this:

class CustomPlan < Zeus::Rails
  def server_ssl
    require 'rails/commands/server'
    server = ::Rails::Server.new
    server.options[:Port] = 3001
    server.options[:pid] = File.expand_path('tmp/pids/server_ssl.pid')

    Dir.chdir(::Rails.application.root)

    server.start 
  end
end

When we run zeus ssl a thin server listening on port 3001 and using server_ssl.pid file starts.

We can open another terminal window and launch zeus s to start at the same time another thin server with a default configuration listening on port 3000.

Configuring thin server

Passing options to thin is done in a different fashion:

class CustomPlan < Zeus::Rails
  def server_ssl
    require 'rails/commands/server'
    server = ::Rails::Server.new
    server.options[:Port] = 3001
    server.options[:pid] = File.expand_path('tmp/pids/server_ssl.pid')

    Dir.chdir(::Rails.application.root)

    server.start do |srv|
      srv.ssl = true
      srv.ssl_options = { :private_key_file => 'ssl/server.key', :cert_chain_file => 'ssl/server.crt', :verify_peer => true }
    end
  end
end

The block will be executed by Rack::Handler::Thin class, and the ssl and ssl_options are specific to the implementation of thin server (You can study the source code of your server at this point to find the options that you need. This is beyond the scope of this tutorial).

As a result we can now start in 2 terminal windows zeus s and zeus ssl to start one http server and one https server.