Class: Hyraft::Server::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/server/launcher.rb

Overview

Main server launcher for Hyraft framework

Handles starting web and API servers with support for multiple Ruby server backends (Puma, Thin, Falcon, Iodine)

Usage Examples

hyr s thin                              # Start web server with Thin
hyr s thin --api                        # Start API server with Thin  
hyraft-server puma -p 3000              # Start on custom port
hyraft-server falcon --http2            # Start with HTTP/2

Examples:

Start a web server

launcher = Hyraft::Server::Launcher.new
launcher.start('thin', [])

Start an API server

launcher = Hyraft::Server::Launcher.new
launcher.start('puma', ['--api'])

Constant Summary collapse

COLORS =

ANSI color codes for terminal output

{
  lime: "\033[92m",
  yellow: "\033[93m",
  reset: "\033[0m"
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Launcher

Initialize a new launcher instance

Examples:

Basic initialization

Launcher.new

With custom options

Launcher.new(port: 3000, server: 'puma')

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :server (String)

    Server type (puma, thin, falcon, iodine)

  • :host (String)

    Host to bind to (default: “localhost”)

  • :port (Integer)

    Web server port (default: 1091)

  • :port_api (Integer)

    API server port (default: 1092)

  • :rack_socket (String)

    Rack config file for web server

  • :rack_socket_api (String)

    Rack config file for API server

  • :http2 (Boolean)

    Enable HTTP/2 (Falcon only)

  • :http3 (Boolean)

    Enable HTTP/3 (Falcon only)

  • :api (Boolean)

    Enable API server mode



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hyraft/server/launcher.rb', line 52

def initialize(options = {})
  @options = {
    server: nil,
    host: "localhost",
    port: 1091,
    port_api: 1092,
    rack_socket: "infra/server/web-server.ru",
    rack_socket_api: "infra/server/api-server.ru",
    http2: false,
    http3: false,
    api: false
  }.merge(options)
end

Instance Method Details

#show_usagevoid

This method returns an undefined value.

Display comprehensive usage information

Shows available commands, options, and examples for the Hyraft server. Includes colorized output when not in test environment.

Command Variants

Hotkey:

hyr s [server-name]                        Start web server
hyr s [server-name] --api                  Start API server directly
hyr s-v                                    Show version
hyr s-h                                    Show this help

Shortkey:

hyr-serve [server-name]                    Start web server
hyr-serve [server-name] --api              Start API server directly
hyr-serve s-v                              Show version
hyr-serve s-h                              Show this help

Standard:

hyraft-server [server-name] [options]      Start web server
hyraft-server [server-name] --api [options] Start API server directly
hyraft-server server-version               Show version
hyraft-server server-help                  Show this help

Examples

hyr s thin                              # Start web server with Thin
hyr-serve thin                          # Start web server with Thin
hyraft-server thin                      # Start web server with Thin
hyraft-server thin --api                # Start API server with Thin
hyraft-server puma -p 1091              # Start web server on port 1091
hyraft-server falcon --http2            # Start with HTTP/2 (Falcon)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/hyraft/server/launcher.rb', line 162

def show_usage
  title_color   = colorize? ? COLORS[:lime]   : ""
  header_color  = colorize? ? COLORS[:yellow] : ""
  reset_color   = colorize? ? COLORS[:reset]  : ""

  puts "#{title_color}Hyraft Server #{Hyraft::Server::VERSION}#{reset_color}"
  puts "High-performance web server with hexagonal architecture"
  puts ""
  puts "#{header_color}Usage:#{reset_color}"
  puts "  hyraft-server [server-name] [options]"
  puts ""
  puts "#{header_color}Hotkey:#{reset_color}"
  puts "  hyr s [server-name]                        Start web server (legacy)"
  puts "  hyr s [server-name] --api                  Start API server directly"
  puts "  hyr s-v                                    Show version"
  puts "  hyr s-h                                    Show this help"
  puts ""
  puts "#{header_color}Shortkey:#{reset_color}"
  puts "  hyr-serve [server-name]                    Start web server (legacy)"
  puts "  hyr-serve [server-name] --api              Start API server directly"
  puts "  hyr-serve s-v                              Show version"
  puts "  hyr-serve s-h                              Show this help"
  puts ""
  puts "#{header_color}Standard Key:#{reset_color}"
  puts "  hyraft-server [server-name] [options]      Start web server (legacy)"
  puts "  hyraft-server [server-name] --api [options] Start API server directly"
  puts "  hyraft-server server-version               Show version"
  puts "  hyraft-server server-help                  Show this help"
  puts ""
  puts "#{header_color}Servers:#{reset_color} puma, thin, falcon, iodine"
  puts ""
  puts "#{header_color}Options:#{reset_color}"
  puts "  -s, --server SERVER    Server (puma, thin, falcon, iodine)"
  puts "  -b, --bind HOST        Host (default: localhost)"
  puts "  -p, --port PORT        Port (default: 1091)"
  puts "  --port-api PORT        API Port (default: 1092)"
  puts "  -c, --config FILE      Rack config file"
  puts "  --config-api FILE      API Rack config file"
  puts "  --api                  Enable API server"
  puts "  --http2                Enable HTTP/2 (Falcon only)"
  puts "  --http3                Enable HTTP/3 (Falcon only)"
  puts ""
  puts "#{header_color}Examples:#{reset_color}"
  example_color = colorize? ? COLORS[:lime] : ""
  puts "#{example_color}  hyr s thin                              #{reset_color}# Start web server with Thin - Hotkey"
  puts "#{example_color}  hyr-serve thin                          #{reset_color}# Start web server with Thin - Shortkey"
  puts "#{example_color}  hyraft-server thin                      #{reset_color}# Start web server with Thin"
  puts "#{example_color}  hyraft-server thin --api                #{reset_color}# Start API server with Thin"
  puts "#{example_color}  hyraft-server puma -p 1091              #{reset_color}# Start web server on port 1091"
  puts "#{example_color}  hyraft-server falcon --http2            #{reset_color}# Start with HTTP/2 (Falcon)"
  puts "#{example_color}  hyraft-server s thin                    #{reset_color}# Legacy syntax (still works)"
end

#start(cmd, args) ⇒ void

This method returns an undefined value.

Start the server with given command and arguments

Supports multiple command formats and server types with comprehensive error handling and user feedback.

Examples:

Start web server with Thin

start('thin', [])

Start API server with Puma

start('puma', ['--api'])

Show help

start('server-help', [])

Show version

start('server-version', [])

Parameters:

  • cmd (String)

    Command to execute

    • Server name: ‘puma’, ‘thin’, ‘falcon’, ‘iodine’

    • Action: ‘server’, ‘s’, ‘svr’, ‘serve’, ‘server-help’, ‘server-version’

  • args (Array<String>)

    Command line arguments

Raises:

  • (OptionParser::InvalidOption)

    When invalid options are provided

  • (OptionParser::MissingArgument)

    When required arguments are missing



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hyraft/server/launcher.rb', line 92

def start(cmd, args)
  begin
    # Check if command is a direct server name
    if %w[thin puma falcon iodine].include?(cmd)
      @options[:server] = cmd
      parse_server_options!(args)
      launch_server
    else
      case cmd
      when 'server', 's', 'svr', 'serve'
        parse_server_options!(args)
        launch_server
      when 'server-version', 'server-v', 's-v'
        show_version
      when 'server-help', 'server-h', 's-h'
        show_usage
      when nil, ''
        puts "#{COLORS[:yellow]}[!] No command provided. Showing help...#{COLORS[:reset]}"
        show_usage
      else
        puts "#{COLORS[:yellow]}[!] Unknown command: '#{cmd}'. Showing help...#{COLORS[:reset]}"
        show_usage
      end
    end
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
    puts "#{COLORS[:yellow]}[!] #{e.message}#{COLORS[:reset]}"
    puts "#{COLORS[:yellow]}[!] Falling back to help info...#{COLORS[:reset]}"
    show_usage
  rescue => e
    puts "#{COLORS[:yellow]}[!] Unexpected error: #{e.message}#{COLORS[:reset]}"
    puts "#{COLORS[:yellow]}[!] Showing help for reference...#{COLORS[:reset]}"
    show_usage
  end
end