Class: SchemaRD::WebServer

Inherits:
Object
  • Object
show all
Defined in:
lib/schemard/web_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ WebServer

Returns a new instance of WebServer.



7
8
9
# File 'lib/schemard/web_server.rb', line 7

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/schemard/web_server.rb', line 6

def config
  @config
end

Instance Method Details

#setup(srv, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schemard/web_server.rb', line 10

def setup(srv, options)
  Signal.trap("INT"){ srv.shutdown }
  Signal.trap("TERM"){ srv.shutdown }

  # アクションメソッド
  srv.mount_proc '/' do |req, res|
    controller = SchemaRD::Controller.new(self.config)
    case req.path
    when "/", "/index"
      controller.index(req, res)
    when /\/tables\/.+/
      controller.show(req, res) if req.request_method == "GET"
      controller.update(req, res) if req.request_method == "POST"
    when /.*\.(js|css|ico)/
      controller.static_file(req, res)
    else
      res.status = Rack::Utils.status_code(404)
    end
  end
end

#startObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schemard/web_server.rb', line 31

def start()
  options = {
    :Host => config.webserver_host,
    :Port => config.webserver_port,
    :ServerType => WEBrick::SimpleServer,
    :Logger => WEBrick::Log.new(config.log_output),
  }
  begin
    srv = WEBrick::HTTPServer.new(options);
    setup(srv, options)
    srv.start
  rescue Errno::EADDRINUSE => e
    (options[:Logger] || Logger.new(STDOUT)).error("#{__FILE__}: #{e.to_s}")
  end
end