Class: Miniserver::Server
- Inherits:
-
Object
- Object
- Miniserver::Server
- Defined in:
- lib/miniserver/server.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Instance Method Summary collapse
- #daemonize_app ⇒ Object
-
#initialize(app, options = {}) ⇒ Server
constructor
A new instance of Server.
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 15 16 17 |
# File 'lib/miniserver/server.rb', line 10 def initialize(app, ={}) @app = app @options = @script = $PROGRAM_NAME FileUtils.mkdir_p(File.dirname([:log])) Miniserver.logger = Logger.new([:log]) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/miniserver/server.rb', line 8 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/miniserver/server.rb', line 8 def @options end |
#script ⇒ Object (readonly)
Returns the value of attribute script.
8 9 10 |
# File 'lib/miniserver/server.rb', line 8 def script @script end |
Instance Method Details
#daemonize_app ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/miniserver/server.rb', line 43 def daemonize_app Process.daemon(true) $0 = "Miniserver #{Miniserver::VERSION} (#{[:host]}:#{[:port]})" FileUtils.mkdir_p(File.dirname([:pid])) File.open([:pid], "w") do |f| f.puts(Process.pid) end end |
#restart ⇒ Object
59 60 61 62 63 |
# File 'lib/miniserver/server.rb', line 59 def restart stop Command.restart(script, ) end |
#start ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/miniserver/server.rb', line 19 def start Miniserver.logger.info "=> #{Time.now.asctime}" Miniserver.logger.info "=> Starting Miniserver #{Miniserver::VERSION}" Miniserver.logger.info "=> Listening on #{[:host]}:#{[:port]}" if [:daemonize] daemonize_app else puts "=> Miniserver #{Miniserver::VERSION}" puts "=> Listening on #{[:host]}:#{[:port]}" puts "=> CTRL+C to stop" end trap(:INT) { stop } trap(:TERM) { stop } trap(:HUP) { restart } EM.run do EM.start_server([:host], [:port], Connection) do |connection| connection.app = app end end end |
#stop ⇒ Object
55 56 57 |
# File 'lib/miniserver/server.rb', line 55 def stop EM.stop end |