em-midori 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3925b4b9ef7e976cc1e1af24b2979a0e3a276f06
4
- data.tar.gz: 6b8a7e0d0300709096247aed4755a285d128cf94
3
+ metadata.gz: '081416a49873c3fef7ed675ec2640bc3923284cc'
4
+ data.tar.gz: f2ab1575e5b6664572a2167f7dbdbced1b94a45a
5
5
  SHA512:
6
- metadata.gz: 449941b9d262ef8a1e8727524976e3af90effde6efc56d043258b37db1eb3a932dc16c6ff701ccd18911dbd23d9c0c821441d687ccabc9e811a5c91b197cf419
7
- data.tar.gz: 512051dcf9e5d764ba1c46b4cc1f817189b91d65875b08472a2337cdbe3da6d9aac72124e49da4b71b6a43ca3e607382d862ed9b699f6ae2aa3cc30cb7bc72dd
6
+ metadata.gz: 128bb0d384e2eb10978a03532c68db04d8bf668260a211ce6d94a46d5970bea2fa5e9bad94a9797329843a611e657618858d988313764031c5452cbf37b17458
7
+ data.tar.gz: 441c0ee3debb05818d91417533ef0a42f1ab609b3dd28a7b422ee28a9ce84b1b30208d6d20927fb8c6d6ccc90cc54b0e13bb7f881e260f9ace5f26c71d25d3de
data/lib/midori.rb CHANGED
@@ -19,6 +19,7 @@ require_relative 'midori/core_ext/safe_require'
19
19
  require_relative 'midori/version'
20
20
  require_relative 'midori/const'
21
21
  require_relative 'midori/exception'
22
+ require_relative 'midori/env'
22
23
  require_relative 'midori/clean_room'
23
24
  require_relative 'midori/server'
24
25
  require_relative 'midori/connection'
@@ -33,3 +34,5 @@ require_relative 'midori/eventsource'
33
34
  require_relative 'midori/middleware'
34
35
  require_relative 'midori/configure'
35
36
  require_relative 'midori/runner'
37
+ require_relative 'midori/logger'
38
+
@@ -52,12 +52,17 @@ class Midori::APIEngine
52
52
  clean_room = Midori::CleanRoom.new(request)
53
53
  if request.websocket?
54
54
  # Send 101 Switching Protocol
55
- connection.send_data Midori::Response.new(101, Midori::APIEngine.websocket_header(request.header['Sec-WebSocket-Key']), '')
55
+ connection.send_data Midori::Response.new(
56
+ status: 101,
57
+ header: Midori::APIEngine.websocket_header(request.header['Sec-WebSocket-Key']),
58
+ body: '')
56
59
  connection.websocket.request = request
57
60
  Midori::Sandbox.run(clean_room, route.function, connection.websocket)
58
61
  return Midori::Response.new
59
62
  elsif request.eventsource?
60
- connection.send_data Midori::Response.new(200, Midori::Const::EVENTSOURCE_HEADER, '')
63
+ connection.send_data Midori::Response.new(
64
+ status: 200,
65
+ header: Midori::Const::EVENTSOURCE_HEADER)
61
66
  Midori::Sandbox.run(clean_room, route.function, connection.eventsource)
62
67
  return Midori::Response.new
63
68
  else
@@ -19,6 +19,6 @@ class Midori::CleanRoom
19
19
  # Generate response from variables inside +Midori::CleanRoom+
20
20
  # @return [Midori::Response] midori response
21
21
  def raw_response
22
- Midori::Response.new(@status, @header, @body)
22
+ Midori::Response.new(status: @status, header: @header, body: @body)
23
23
  end
24
24
  end
data/lib/midori/env.rb ADDED
@@ -0,0 +1,18 @@
1
+ module Midori
2
+ # @return [String] midori environment
3
+ def self.env
4
+ ENV['MIDORI_ENV'] || 'development'
5
+ end
6
+ end
7
+
8
+ class String
9
+ # @return [TrueClass | FalseClass] if string is equal to production
10
+ def production?
11
+ self == 'production'
12
+ end
13
+
14
+ #@return [TrueClass | FalseClass] if string is equal to development
15
+ def development?
16
+ self == 'development'
17
+ end
18
+ end
@@ -17,7 +17,7 @@ module Hiredis
17
17
  # Meta-programming hiredis for redis async extension
18
18
  class Connection
19
19
  # Do redis query
20
- # @param [String] data generated string data
20
+ # @param [Array] args equal to Hiredis write args
21
21
  def query(args)
22
22
  await(Promise.new do |resolve|
23
23
  read_flag = false
@@ -65,22 +65,33 @@ end
65
65
 
66
66
  require 'redic'
67
67
 
68
+ ##
69
+ # Meta-programming Redic for redis async extension
68
70
  class Redic
71
+ # Meta-programming Redic for redis async extension
69
72
  class Client
73
+ # Connect redis, yield optional
70
74
  def connect
71
75
  establish_connection unless connected?
72
76
  if block_given?
77
+ # Redic default yield
78
+ # :nocov:
73
79
  @semaphore.synchronize do
74
80
  yield
75
81
  end
82
+ # :nocov:
76
83
  end
77
84
  end
78
-
85
+
86
+ # Call without thread lock
87
+ # @param [Array] args same params as Redic
79
88
  def call(*args)
80
89
  @connection.query(*args)
81
90
  end
82
91
  end
83
92
 
93
+ # Call without thread lock
94
+ # @param [Array] args same params as Redic
84
95
  def call(*args)
85
96
  @client.connect
86
97
  @client.call(args)
@@ -32,8 +32,11 @@ class Sequel::Postgres::Adapter
32
32
  EventLoop.deregister(socket_object)
33
33
  resolve.call(get_result)
34
34
  end
35
+ # For extra errors
36
+ # :nocov:
35
37
  rescue => e
36
38
  resolve.call(PromiseException.new(e))
39
+ # :nocov:
37
40
  end
38
41
  end
39
42
  end)
@@ -0,0 +1,16 @@
1
+ module Midori
2
+ class << self
3
+ # Return current logger midori is using
4
+ # @return [Logger] the current logger midori is using
5
+ def logger
6
+ @logger = ::Logger.new(STDOUT) if @logger.nil?
7
+ @logger
8
+ end
9
+
10
+ # Return midori's logger
11
+ # @param [Logger] logger set midori logger
12
+ def logger=(logger)
13
+ @logger = logger
14
+ end
15
+ end
16
+ end
@@ -6,14 +6,16 @@
6
6
  class Midori::Response
7
7
  attr_accessor :status, :header, :body
8
8
 
9
- # @param [Integer] code HTTP response code
10
- # @param [Hash] header HTTP response header
11
- # @param [String] body HTTP response body
9
+ # @param [Hash] options HTTP response
10
+ # @option options [Integer] code HTTP response code
11
+ # @option options [Hash] header HTTP response header
12
+ # @option options [String] body HTTP response body
12
13
  # Init a Response
13
- def initialize(code = 200, header = Midori::Const::DEFAULT_HEADER.clone, body = '')
14
+ def initialize(options = {})
15
+ code = options[:status] || 200
14
16
  @status = Midori::Const::STATUS_CODE[code]
15
- @header = header
16
- @body = body
17
+ @header = options[:header] || Midori::Const::DEFAULT_HEADER.clone
18
+ @body = options[:body] || ''
17
19
  end
18
20
 
19
21
  # Generate header string from hash
data/lib/midori/runner.rb CHANGED
@@ -11,6 +11,7 @@ class Midori::Runner
11
11
  # @param [Class] configure inherited from [Midori::Configure]
12
12
  def initialize(api, configure = Midori::Configure)
13
13
  @logger = configure.logger
14
+ Midori.logger = configure.logger
14
15
  @bind = configure.bind
15
16
  @port = configure.port
16
17
  @api = ((api.is_a?Midori::APIEngine) ? api : Midori::APIEngine.new(api, configure.route_type))
@@ -4,8 +4,8 @@ class Midori::Sandbox
4
4
  class << self
5
5
  def class_initialize
6
6
  @handlers = Hash.new
7
- @handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(500, {}, "#{e.inspect} #{e.backtrace}")}
8
- @handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(404, {}, '404 Not Found')}
7
+ @handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(status: 500, body: "#{e.inspect} #{e.backtrace}")}
8
+ @handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(status: 404, body: '404 Not Found')}
9
9
  end
10
10
 
11
11
  # Add a rule to Sandbox
data/lib/midori/server.rb CHANGED
@@ -41,7 +41,7 @@ module Midori::Server
41
41
  end
42
42
  rescue => e
43
43
  close_connection
44
- @logger.warn "#{@request.ip} - - #{e.backtrace.join("\n")}".yellow
44
+ @logger.warn "#{@request.ip} - - #{e.class} #{e.backtrace.join("\n")}".yellow
45
45
  end
46
46
  end)
47
47
  end.call
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.2.4'.freeze
4
+ VERSION = '0.3.0'.freeze
5
5
  end
@@ -92,7 +92,7 @@ Using bunlder could make dependency management much easier, which helps a lot in
92
92
 
93
93
  You may probably meet problems with rubygems due to unstable overseas internet connection issues in China. The most popular way to solve it is to use mirror provided by [RubyChina](https://gems.ruby-china.org/) or [TUNA](https://mirror.tuna.tsinghua.edu.cn/help/rubygems/) as your gem source. This may have some side effects in development, because there's a few minutes' delay in receiving gem updates.
94
94
 
95
- Alternatively, you could use proxy to connect the main repository directly to avoid the delay problem. But using proxy is a little too complex in production environment.
95
+ Alternatively, you could use proxy to connect to the main repository directly to avoid the delay problem. But using proxy is a little too complex in production environment.
96
96
 
97
97
  Choose the solution better fits your requirements. Mixing the solutions like using proxy in development and using mirror in production is also a good choice.
98
98
 
@@ -40,7 +40,7 @@ class ExampleAPI < Midori::API
40
40
  end
41
41
 
42
42
  get '/case_1' do
43
- status = 418
43
+ @status = 418
44
44
  "I\'m a teapot"
45
45
  # HTTP/1.1 418 I'm a teapot
46
46
  # Server: Midori/1.0
@@ -51,21 +51,23 @@ class ExampleAPI < Midori::API
51
51
  get '/case_2' do
52
52
  header['Example-Header'] = 'Example-Value'
53
53
  'Hello'
54
+ # HTTP/1.1 200 OK
55
+ # Server: Midori/1.0
56
+ # Example-Header: Example-Value
57
+ #
58
+ # Hello
54
59
  end
55
- # HTTP/1.1 200 OK
56
- # Server: Midori/1.0
57
- # Example-Header: Example-Value
58
- #
59
- # Hello
60
60
 
61
61
  get '/case_3' do
62
- status = 202
62
+ @status = 202
63
63
  header['Example-Header'] = 'Example-Value'
64
- Midori::Response.new(200, {}, 'Hello')
64
+ Midori::Response.new(status: 200, header: {}, body: 'Hello') # Overrides everything else
65
+ # HTTP/1.1 200 OK
66
+ # Server: Midori/1.0
67
+ # Example-Header: Example-Value
68
+ #
69
+ # Hello
65
70
  end
66
- # HTTP/1.1 200 OK
67
- #
68
- # Hello
69
71
  end
70
72
  ```
71
73
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-midori
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HeckPsi Lab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-06 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r
@@ -77,6 +77,7 @@ files:
77
77
  - lib/midori/core_ext/safe_require.rb
78
78
  - lib/midori/core_ext/string.rb
79
79
  - lib/midori/core_ext/timer.rb
80
+ - lib/midori/env.rb
80
81
  - lib/midori/eventsource.rb
81
82
  - lib/midori/exception.rb
82
83
  - lib/midori/extension/file.rb
@@ -84,6 +85,7 @@ files:
84
85
  - lib/midori/extension/redic.rb
85
86
  - lib/midori/extension/sequel/mysql2.rb
86
87
  - lib/midori/extension/sequel/postgres.rb
88
+ - lib/midori/logger.rb
87
89
  - lib/midori/middleware.rb
88
90
  - lib/midori/request.rb
89
91
  - lib/midori/response.rb