Skip to content

Commit

Permalink
Make the usage consistend between ngx_mruby/mod_mruby and h2o
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaro committed Sep 16, 2015
1 parent 3e24925 commit 3cb4014
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
35 changes: 16 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

A Web application framework for Web servers that support mruby and Rack-based API.

## Usage
mruby-hibari currently supports the Web servers below:

### [ngx_mruby](http://ngx.mruby.org/), [mod_mruby](http://mod.mruby.org/)
* [ngx_mruby](http://ngx.mruby.org/) enabled Nginx
* [mod_mruby](http://mod.mruby.org/) enabled Apache
* [h2o](https://h2o.examp1e.net/)

1. Add dependency on `mruby-hibari` into `build_config.rb`.
2. Write mruby handler like below:
## Synopsis

Write once, run the same on any servers above.

```ruby
class MyApp < Hibari::App
Expand All @@ -18,27 +21,20 @@ class MyApp < Hibari::App
end
end

run MyApp.new
MyApp.new.run
```

### [h2o](https://h2o.examp1e.net/)
## How to Setup

1. `git clone` at h2o/deps: `cd h2o/deps && git clone https://github.com/kentaro/mruby-hibari`
2. Write mruby handler like below:
### ngx_mruby, mod_mruby

```ruby
class MyApp < Hibari::App
def build
res.code = 200
res.headers["content-type"] = "text/html; charset=utf8"
res.body.push("Hello, World!")
end
end
1. Add dependency on mruby-hibari into the build_config.rb in mruby installation.
2. Write mruby handler like above.

MyApp.new
```
### h2o

h2o doesn't requre to execute `run` method unlike Rack's spec.
1. `git clone` at h2o/deps to embed mruby-hibari: `cd h2o/deps && git clone https://github.com/kentaro/mruby-hibari`
2. Write mruby handler like above.

## API

Expand All @@ -62,6 +58,7 @@ Build URI and returns it as `URI` object.
#### `remote_addr()` => `String`
#### `remote_port()` => `String`
#### `scheme()` => `String`
#### `engine_name()` => `String`

Shortcuts to `env[XXX`].

Expand Down
22 changes: 21 additions & 1 deletion mrblib/hibari/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ module Hibari
class App
attr_reader :req, :res

def initialize
@res = Response.new
end

def call(env)
@env = env
@req = Request.new(env)
@res = Response.new

build
res.to_rack
end

def run
engine = if Kernel.const_defined?(:Nginx)
'nginx'
elsif Kernel.const_defined?(:Apache)
'apache'
else
'h2o'
end

case engine
when 'nginx' || 'apache'
Kernel.run(self)
when 'h2o' # presume it's h2o
self
end
end
end
end
4 changes: 4 additions & 0 deletions mrblib/hibari/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ def params
def scheme
env['rack.url_scheme']
end

def engine_name
env['server.name']
end
end
end
23 changes: 22 additions & 1 deletion test/hibari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'rack.multiprocess' => true,
'rack.run_once' => false,
'rack.hijack?' => false,
'server.name' => 'Hibari',
'server.name' => 'test server',
'server.version' => '1.0',
}

Expand All @@ -36,6 +36,26 @@ def build
assert_equal ['test'], res[2]
end

assert 'Hibari::App#run' do
app = TestApp.new

# When the Web server is Nginx or Apache
(Proc.new {
Kernel.const_set(:Nginx, true)
Kernel.define_method(:run) {|obj| obj.res.to_rack }

res = app.run
assert_equal Array, res.class

Kernel.send(:remove_const, :Nginx)
Kernel.send(:remove_method, :run)
}).call

# Or it is h2o
res = app.run
assert_equal TestApp, res.class
end

assert 'Hibari::Request#params' do
req = Hibari::Request.new(env)
assert_equal(URI::HTTP, req.uri.class)
Expand All @@ -55,6 +75,7 @@ def build
assert_equal req.remote_addr, env['REMOTE_ADDR']
assert_equal req.remote_port, env['REMOTE_PORT']
assert_equal req.scheme, env['rack.url_scheme']
assert_equal req.engine_name, env['server.name']
end

assert 'Hibari::Request#to_rack' do
Expand Down

0 comments on commit 3cb4014

Please sign in to comment.