ruby_wolf 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d60914f1d8dd41e9242834687e19f41409036932
4
- data.tar.gz: 8a51798908bce2046da3d23bd7845af237df05d9
3
+ metadata.gz: 1791ad834e8ba7733e34262dce2bc95d254fc653
4
+ data.tar.gz: 415e919027a607f3fe876d1e43bbd65d713aa638
5
5
  SHA512:
6
- metadata.gz: 5fc70290442f8fef6ff599bd3d21f2421851e8dd384a5cdb37333c765958d95d92a6f7bf040c24019d16984fe0cba16fc37a0b4fd4ed191b5f2eb28e08bac0de
7
- data.tar.gz: 9ebfa97ef988cbe019d2a11e707d21f8245d0f8339e3ac580fdb7bea5065d493598d5fc7b525894e658c435e18785c7fd9c5574abdb753cddff49b8b749e7aec
6
+ metadata.gz: 8d96997f1596a88713002211c8f19847fd944577d1d66e80d8df48d14642d78a96b4dd882ac059d88747598e999139a5bca168f9bc00c0d7f38bb4a01d049305
7
+ data.tar.gz: 2d00b55807d1a798e1f4e8c944c8e449ea9fbf9b7b93f9b0e2a39b810b1acafc6ab482b74c785752cdebc52f34f6f02b86615f206c19a2d0b93f8db0e40ad274
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ tags
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in ruby_wolf.gemspec
3
+ gem 'rack', '< 3.0'
4
4
  gemspec
data/lib/ruby_wolf/cli.rb CHANGED
@@ -12,6 +12,8 @@ module RubyWolf
12
12
 
13
13
  def run
14
14
  parse_options
15
+ set_environment
16
+
15
17
  raise 'Rack file not found' unless File.exist?(rack_file)
16
18
 
17
19
  @server = RubyWolf::Server.new(rack_file, configs)
@@ -38,6 +40,10 @@ module RubyWolf
38
40
  @configs[:worker] = arg.to_i
39
41
  end
40
42
 
43
+ opts.on('-e ENVIRONMENT', '--environment=ENVIRONMENT', 'Current environment') do |arg|
44
+ @configs[:environment] = arg
45
+ end
46
+
41
47
  opts.on('-h', '--help', 'Show the usages') do
42
48
  puts opts
43
49
  exit
@@ -52,5 +58,10 @@ module RubyWolf
52
58
  def rack_file
53
59
  "#{@app_root}/config.ru"
54
60
  end
61
+
62
+ def set_environment
63
+ ENV['RAILS_ENV'] = configs[:environment]
64
+ ENV['RACK_ENV'] = configs[:environment]
65
+ end
55
66
  end
56
67
  end
@@ -4,13 +4,15 @@ module RubyWolf
4
4
  DEFAULT_HOST = '0.0.0.0'.freeze
5
5
  DEFAULT_PORT = 3000
6
6
  DEFAULT_WORKER = 4
7
+ DEFAULT_ENVIRONMENT = 'development'.freeze
7
8
 
8
9
  def initialize
9
10
  @configs = {
10
11
  daemon: DEFAULT_DAEMON,
11
12
  worker: DEFAULT_WORKER,
12
13
  host: DEFAULT_HOST,
13
- port: DEFAULT_PORT
14
+ port: DEFAULT_PORT,
15
+ environment: DEFAULT_ENVIRONMENT
14
16
  }
15
17
  end
16
18
 
@@ -3,7 +3,7 @@ require 'http/parser'
3
3
  module RubyWolf
4
4
  class Handler
5
5
 
6
- attr_reader :app, :env, :connection, :response, :callback
6
+ attr_reader :app, :env, :connection, :response, :callback, :body, :headers, :status
7
7
 
8
8
  def initialize(app, connection, &callback)
9
9
  @app = app
@@ -47,25 +47,16 @@ module RubyWolf
47
47
 
48
48
  'rack.input' => StringIO.new(connection.read_chunk)
49
49
  )
50
-
51
- RubyWolf.log(
52
- [
53
- 'HTTP/1.1',
54
- connection.method,
55
- "#{connection.path}?#{connection.query}"
56
- ].join(' ')
57
- )
50
+ log_request
58
51
  end
59
52
 
60
53
  def generate_response
61
- status, headers, body = app.call(env)
62
- RubyWolf.log(
63
- "Response HTTP/1.1 #{status}"
64
- )
65
- compose_response(status, headers, body)
54
+ @status, @headers, @body = app.call(env)
55
+ compose_response
56
+ log_response
66
57
  end
67
58
 
68
- def compose_response(status, headers, body)
59
+ def compose_response
69
60
  @response += "HTTP/1.1 #{status} #{RubyWolf::CRLF}"
70
61
  headers.each do |key, value|
71
62
  @response += "#{key}: #{value}#{RubyWolf::CRLF}"
@@ -78,5 +69,31 @@ module RubyWolf
78
69
  ensure
79
70
  body.close if body.respond_to? :close
80
71
  end
72
+
73
+ private
74
+
75
+ def log_request
76
+ RubyWolf.logger.info(
77
+ [
78
+ 'HTTP/1.1',
79
+ connection.method,
80
+ request_path
81
+ ].join(' ')
82
+ )
83
+ end
84
+
85
+ def log_response
86
+ RubyWolf.logger.info(
87
+ "Response HTTP/1.1 #{status}"
88
+ )
89
+ end
90
+
91
+ def request_path
92
+ if connection.query
93
+ "#{connection.path}?#{connection.query}"
94
+ else
95
+ connection.path
96
+ end
97
+ end
81
98
  end
82
99
  end
@@ -0,0 +1,44 @@
1
+ require 'logger'
2
+
3
+ module RubyWolf
4
+ class Logger < ::Logger
5
+ def info(contents = "")
6
+ pre_process(contents) do |content|
7
+ super(content)
8
+ end
9
+ end
10
+
11
+ def warn(contents = "")
12
+ pre_process(contents) do |content|
13
+ super(content)
14
+ end
15
+ end
16
+
17
+ def debug(contents = "")
18
+ pre_process(contents) do |content|
19
+ super(content)
20
+ end
21
+ end
22
+
23
+ def error(contents = "")
24
+ pre_process(contents) do |content|
25
+ super(content)
26
+ end
27
+ end
28
+
29
+ def fatal(contents = "")
30
+ pre_process(contents) do |content|
31
+ super(content)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def pre_process(contents)
38
+ object = Process.pid == MAIN_PID ? '[Main]' : "[Worker #{Process.pid}]"
39
+ contents.to_s.split("\n").each do |line|
40
+ yield("#{object} #{line}")
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,5 +1,3 @@
1
- require 'logger'
2
-
3
1
  module RubyWolf
4
2
  class Server
5
3
  attr_reader :app, :configs, :socket, :workers
@@ -25,16 +23,18 @@ module RubyWolf
25
23
  private
26
24
 
27
25
  def setup_rack
28
- RubyWolf.log('~~~ Ruby Wolf ~~~')
29
- RubyWolf.log('Loading Rack application')
26
+ RubyWolf.logger.info('~~~ Ruby Wolf ~~~')
27
+ RubyWolf.logger.info('Loading Rack application')
30
28
  @app, _rack_options = ::Rack::Builder.parse_file(@rack_file)
29
+ Rails.logger = RubyWolf.logger if defined?(Rails)
30
+ ActiveRecord::Base.logger = RubyWolf.logger if defined?(ActiveRecord)
31
31
  end
32
32
 
33
33
  def setup_socket
34
34
  @socket = TCPServer.new(configs[:host], configs[:port])
35
- RubyWolf.log("Server is running on #{configs[:host]}:#{configs[:port]}")
36
- RubyWolf.log("Process pid is #{Process.pid}")
37
- RubyWolf.log("Number of worker: #{configs[:worker]}")
35
+ RubyWolf.logger.info("Server is running on #{configs[:host]}:#{configs[:port]}")
36
+ RubyWolf.logger.info("Process pid is #{Process.pid}")
37
+ RubyWolf.logger.info("Number of worker: #{configs[:worker]}")
38
38
  end
39
39
 
40
40
  def handle_loop
@@ -42,7 +42,7 @@ module RubyWolf
42
42
  stopped_worker = workers.find { |w| w.pid == stopped_pid }
43
43
  next unless stopped_worker
44
44
 
45
- RubyWolf.log("Worker with pid #{stopped_pid} suddenly stopped", :error)
45
+ RubyWolf.logger.info("Worker with pid #{stopped_pid} suddenly stopped", :error)
46
46
 
47
47
  sleep(1)
48
48
  worker = RubyWolf::Worker.new(self)
@@ -1,3 +1,3 @@
1
1
  module RubyWolf
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -11,7 +11,7 @@ module RubyWolf
11
11
 
12
12
  def start
13
13
  @pid = fork do
14
- RubyWolf.log('Worker is ready')
14
+ RubyWolf.logger.info('Worker is ready')
15
15
  handle_loop
16
16
  end
17
17
  end
data/lib/ruby_wolf.rb CHANGED
@@ -2,6 +2,7 @@ require 'byebug'
2
2
  require 'rack'
3
3
 
4
4
  require 'ruby_wolf/version'
5
+ require 'ruby_wolf/logger'
5
6
  require 'ruby_wolf/configuration'
6
7
  require 'ruby_wolf/connection'
7
8
  require 'ruby_wolf/handler'
@@ -16,14 +17,6 @@ module RubyWolf
16
17
  READ_SIZE = 16 * 1024
17
18
 
18
19
  def self.logger
19
- @logger ||= Logger.new(STDOUT)
20
- end
21
-
22
- def self.log(content, mode = :info)
23
- contents = content.to_s.split("\n")
24
- object = Process.pid == MAIN_PID ? '[Main]' : "[Worker #{Process.pid}]"
25
- contents.each do |line|
26
- logger.send(mode, "#{object} #{line}")
27
- end
20
+ @logger ||= RubyWolf::Logger.new(STDOUT)
28
21
  end
29
22
  end
@@ -111,5 +111,22 @@ describe RubyWolf::CLI do
111
111
  it { expect(cli.configs[:port]).to eql(20) }
112
112
  end
113
113
  end
114
+
115
+ describe 'environment option' do
116
+ context 'default environment' do
117
+ let(:args) { [] }
118
+ it { expect(cli.configs[:environment]).to eql('development') }
119
+ end
120
+
121
+ context 'short form' do
122
+ let(:args) { ['-w production'] }
123
+ it { expect(cli.configs[:environment]).to eql('production') }
124
+ end
125
+
126
+ context 'full form' do
127
+ let(:args) { ['--environment=20'] }
128
+ it { expect(cli.configs[:environment]).to eql('production') }
129
+ end
130
+ end
114
131
  end
115
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_wolf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nguyễn Quang Minh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-01 00:00:00.000000000 Z
11
+ date: 2017-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -93,6 +93,7 @@ files:
93
93
  - lib/ruby_wolf/configuration.rb
94
94
  - lib/ruby_wolf/connection.rb
95
95
  - lib/ruby_wolf/handler.rb
96
+ - lib/ruby_wolf/logger.rb
96
97
  - lib/ruby_wolf/server.rb
97
98
  - lib/ruby_wolf/version.rb
98
99
  - lib/ruby_wolf/worker.rb