|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require 'json' |
| 4 | +require 'puma' |
2 | 5 |
|
3 | 6 | module Oxidized |
4 | 7 | module API |
5 | 8 | class Web |
6 | | - require 'rack/handler/puma' |
| 9 | + include SemanticLogger::Loggable |
| 10 | + |
7 | 11 | attr_reader :thread |
8 | 12 |
|
| 13 | + DEFAULT_HOST = '127.0.0.1' |
| 14 | + DEFAULT_PORT = 8888 |
| 15 | + DEFAULT_URI_PREFIX = '' |
| 16 | + |
9 | 17 | def initialize(nodes, configuration) |
10 | 18 | require 'oxidized/web/webapp' |
11 | | - if configuration.instance_of? Asetus::ConfigStruct |
12 | | - # New configuration syle: extensions.oxidized-web |
13 | | - addr = configuration.listen? || '127.0.0.1' |
14 | | - port = configuration.port? || 8888 |
15 | | - uri = configuration.url_prefix? || '' |
16 | | - vhosts = configuration.vhosts? || [] |
17 | | - else |
18 | | - # Old configuration stlyle: "rest: 127.0.0.1:8888/prefix" |
19 | | - listen, uri = configuration.split '/' |
20 | | - addr, _, port = listen.rpartition ':' |
21 | | - unless port |
22 | | - port = addr |
23 | | - addr = nil |
24 | | - end |
25 | | - vhosts = [] |
26 | | - end |
27 | | - uri = "/#{uri}" |
28 | | - @opts = { |
29 | | - Host: addr, |
30 | | - Port: port |
31 | | - } |
| 19 | + @addr, @port, uri_prefix, vhosts = self.class.parse_configuration(configuration) |
| 20 | + uri_prefix = "/#{uri_prefix}" |
32 | 21 | WebApp.set :nodes, nodes |
33 | 22 | WebApp.set :host_authorization, { permitted_hosts: vhosts } |
34 | 23 | @app = Rack::Builder.new do |
35 | | - map uri do |
| 24 | + map uri_prefix do |
36 | 25 | run WebApp |
37 | 26 | end |
38 | 27 | end |
39 | 28 | end |
40 | 29 |
|
41 | 30 | def run |
42 | 31 | @thread = Thread.new do |
43 | | - Rack::Handler::Puma.run @app, **@opts |
44 | | - exit! |
| 32 | + @server = Puma::Server.new @app |
| 33 | + @server.add_tcp_listener @addr, @port |
| 34 | + logger.info "Oxidized-web server listening on #{@addr}:#{@port}" |
| 35 | + @server.run.join |
45 | 36 | end |
46 | 37 | end |
| 38 | + |
| 39 | + def self.parse_configuration(configuration) |
| 40 | + if configuration.instance_of? Asetus::ConfigStruct |
| 41 | + parse_new_configuration(configuration) |
| 42 | + else |
| 43 | + parse_legacy_configuration(configuration) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + # New configuration style: extensions.oxidized-web |
| 48 | + def self.parse_new_configuration(configuration) |
| 49 | + addr = configuration.listen? || DEFAULT_HOST |
| 50 | + port = configuration.port? || DEFAULT_PORT |
| 51 | + uri_prefix = configuration.url_prefix? || DEFAULT_URI_PREFIX |
| 52 | + vhosts = configuration.vhosts? || [] |
| 53 | + |
| 54 | + [addr, port, uri_prefix, vhosts] |
| 55 | + end |
| 56 | + |
| 57 | + # Legacy configuration style: "rest: 127.0.0.1:8888/prefix" |
| 58 | + def self.parse_legacy_configuration(configuration) |
| 59 | + listen, uri_prefix = configuration.split('/', 2) |
| 60 | + addr, _, port = listen.rpartition ':' |
| 61 | + unless port |
| 62 | + port = addr |
| 63 | + addr = nil |
| 64 | + end |
| 65 | + port = port.to_i |
| 66 | + uri_prefix ||= DEFAULT_URI_PREFIX |
| 67 | + vhosts = [] |
| 68 | + |
| 69 | + [addr, port, uri_prefix, vhosts] |
| 70 | + end |
47 | 71 | end |
48 | 72 | end |
49 | 73 | end |
0 commit comments