Class: Narou::AppServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/web/appserver.rb

Constant Summary collapse

ALLOW_HOSTS =
[].tap do |hosts|
  Downloader.load_settings.each do |s|
    hosts << s["domain"]
  end
  hosts.freeze
end

Class Method Summary collapse

Class Method Details

.create_address(user_port = nil) ⇒ Object

サーバのアドレスを生成

portは初回起動時にランダムで設定する。次回からは同じ設定を引き継ぐ。 bindは自分で設定する場合は narou s server-bind=address で行う。 bindは設定しなかった場合は起動したPCのプライベートIPアドレスが設定される。 この場合はLAN内からアクセス出来る。 bindがlocalhostの場合は実際には127.0.0.1で処理される。(起動したPCでしか アクセス出来ない) 0.0.0.0 を指定した場合はアクセスに制限がかからない(外部からアクセス可能) セキュリティ上オススメ出来ない。



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/web/appserver.rb', line 140

def self.create_address(user_port = nil)
  global_setting = Inventory.load("global_setting", :global)
  port, bind = global_setting["server-port"], global_setting["server-bind"]
  port = user_port if user_port
  ipaddress = my_ipaddress
  unless port
    port = rand(4000..65000)
    global_setting["server-port"] = port
    global_setting.save
  end
  bind = "127.0.0.1" if bind == "localhost"
  host = bind ? bind : ipaddress
  set :port, port
  set :bind, host
  {
    host: host,
    port: port
  }
end

.my_ipaddressObject

自分のIPアドレス取得

参考:qiita.com/saltheads/items/cc49fcf2af37cb277c4f



165
166
167
168
169
170
171
172
173
# File 'lib/web/appserver.rb', line 165

def self.my_ipaddress
  @@__ipaddress ||= -> {
    udp = UDPSocket.new
    udp.connect("128.0.0.0", 7)
    adrs = Socket.unpack_sockaddr_in(udp.getsockname)[1]
    udp.close
    adrs
  }.call
end

.push_server=(server) ⇒ Object



124
125
126
# File 'lib/web/appserver.rb', line 124

def self.push_server=(server)
  @@push_server = server
end