為Web服務器提供mruby和Rack-based API支持的Web框架:mruby-hibari

jopen 9年前發布 | 30K 次閱讀 Web框架 mruby-hibari

針對Web服務器的Web應用程序框架,提供mruby和基于Rack的API支持。

mruby-hibari 當前支持以下服務器:

Synopsis

Write once, run the same on any servers above.

OOP Style

class MyApp < Hibari::App
  def build
    res.code = 200
    res.headers["content-type"] = "text/html; charset=utf8"
    res.body.push("Hello, World!")
    req.params.each do |k,v|
      res.body.push("#{k}: #{v}")
    end
  end
end

MyApp.new.run

DSL Style

hibari do
  res.code = 200
  res.headers["content-type"] = "text/html; charset=utf8"
  res.body.push("Hello, World!")
  req.params.each do |k,v|
    res.body.push("#{k}: #{v}")
  end
end

How to Setup

ngx_mruby, mod_mruby

  1. Add dependency on mruby-hibari into the build_config.rb in your mruby installation.
  2. Write mruby handler like above.

h2o

  1. git cloneat h2o/deps to embed mruby-hibari:cd h2o/deps && git clone https://github.com/kentaro/mruby-hibari
  2. Write mruby handler like above.

API

Hibari::Request

env()=>Hash

Read-only accessor toENVpassed from Web server.

uri()=>URI

Builds URI and returns it asURIobject.

params()=>Hash

Parses query string and returns it asHashobject.

(This method currently does nothing about decoding or other; it just splits the query string with&and=)

request_method()=>String

script_name()=>String

path_info()=>String

request_uri()=>String

query_string()=>String

server_name()=>String

server_addr()=>String

remote_addr()=>String

remote_port()=>String

scheme()=>String

engine_name()=>String

Shortcuts toenv[XXX].

Hibari::Response

code()=>Int

code=(Int)=> ()

Accessor to HTTP response code.

headers()=>Hash

headers=(Hash)=> ()

Accessor to HTTP response headers.

body()=>Array

body=(Array)=> ()

Accessor to HTTP response body.

to_rack()=>Array

Returns an array that follows Rack's spec.

項目主頁:http://www.baiduhome.net/lib/view/home/1442470425399

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!