Class: Ro::Script::Server

Inherits:
Object show all
Defined in:
lib/ro/script/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script:) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
# File 'lib/ro/script/server.rb', line 9

def initialize(script:)
  @script = script

  @port = @script.opts.fetch(:port)
end

Class Method Details

.run!Object



4
5
6
# File 'lib/ro/script/server.rb', line 4

def run!(...)
  new(...).run!
end

Instance Method Details

#build!Object



41
42
43
# File 'lib/ro/script/server.rb', line 41

def build!
  system "RO_URL=#{Ro.config.url} RO_ROOT=#{Ro.config.root} ro build"
end

#run!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ro/script/server.rb', line 15

def run!
  Ro.config.set(:url, server_url)

  #build!

  threads = [watcher!, server!]

  trap('INT') do
    threads.each do |thread|
      thread.kill
    rescue StandardError
      nil
    end
    exit
  end

  sleep
end

#server!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ro/script/server.rb', line 52

def server!
  require 'webrick'

  build = Ro.config.build
  document_root = build.dirname

  index_url = File.join(server_url, 'index.json')

  @script.say("ro.server: @ #{index_url}", color: :magenta)

  Thread.new do
    Thread.current.abort_on_exception = true

    server = WEBrick::HTTPServer.new(
      DocumentRoot: document_root,
      Port: @port
    )

    ::Kernel.at_exit { server.shutdown }

    server.start
  end
end

#server_urlObject



34
35
36
37
38
39
# File 'lib/ro/script/server.rb', line 34

def server_url
  build = Ro.config.build
  document_root = build.dirname
  path_info = build.relative_to(document_root)
  Ro.normalize_url("http://localhost:#{@port}/#{path_info}")
end

#watcher!Object



45
46
47
48
49
50
# File 'lib/ro/script/server.rb', line 45

def watcher!
  Thread.new do
    Thread.current.abort_on_exception = true
    system "RO_URL=#{Ro.config.url} RO_ROOT=#{Ro.config.root} ro build --watch"
  end
end