Class: Yeah::Web::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/yeah/web/server.rb

Overview

The Web::Server serves a game over the web. To start a game web server, enter yeah serve in a command-line within a game project.

Defined Under Namespace

Classes: Runner

Instance Method Summary collapse

Instance Method Details

#start(port = 1234) ⇒ nil

Start web server for game in working directory.

Parameters:

  • port (Integer) (defaults to: 1234)

Returns:

  • (nil)

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yeah/web/server.rb', line 15

def start(port = 1234)
  Opal::Processor.inline_operators_enabled = true

  runner = Runner.new

  assets = Opal::Environment.new

  # Append standard library code paths.
  $LOAD_PATH.each { |p| assets.append_path(p) }

  # Append gem code paths.
  assets.append_path gem_path.join('lib')
  assets.append_path gem_path.join('opal')

  # Append game code and asset paths.
  assets.append_path 'assets'
  assets.append_path 'code'

  source_maps = Opal::SourceMapServer.new(assets, '/assets')

  application = Rack::Builder.new do
    use Rack::Deflater

    map '/' do
      run runner
    end

    map '/assets' do
      run Rack::Cascade.new([assets, source_maps])
    end
  end

  Rack::Server.start(app: application, Port: port)
end