Skip to content

Use Case

miminashi edited this page Mar 25, 2016 · 29 revisions
Clone this wiki locally

Use Case

Hello World

/path/to/hello.rb

Apache.echo "Hello World"

httpd.conf

<Location /hello>
    mrubyHandlerMiddle /path/to/hello.rb;
</Location>

Access

$ curl http://127.0.0.1/hello
Hello World

Reverse Proxy Selectively Measuring Busy Worker Rate

httpd.conf

<Location /proxy>
    mrubyTranslateNameMiddle /path/to/proxy.rb
</Location>

proxy.rb

# Or get backend url from Redis
backends = [
    "http://192.168.0.101:8888/",
    "http://192.168.0.102:8888/",
    "http://192.168.0.103:8888/",
    "http://192.168.0.104:8888/",
]

sb = Apache::Scoreboard.new
busyrate = sb.busy_worker / (sb.process_limit * sb.worker_limit)

if busyrate * 100 > 50
    proxy_rate = backends.length - 2
else if busyrate * 100 > 70
    proxy_rate = backends.length - 1
else if busyrate * 100 > 90
    proxy_rate = backends.length
else
    proxy_rate = backends.length - 3
end

r = Apache::Request.new

r.handler  = "proxy-server"
r.proxyreq = Apache::PROXYREQ_REVERSE
r.filename = "proxy:" + backends[rand(proxy_late)] + r.uri

Basic Auth with Redis

httpd.conf

<Location /basic/>
  AuthType basic
  AuthName "Message for clients"
  AuthBasicProvider mruby
  mrubyAuthnCheckPassword /path/to/authn_basic.rb
  require valid-user
</Location>

authn_basic.rb

host    = "127.0.0.1"
port    = 6379
anp     = Apache::AuthnProvider.new
redis   = Redis.new host, port

if redis.get(anp.user) == anp.password
  Apache.return Apache::AuthnProvider::AUTH_GRANTED
else
  Apache.return Apache::AuthnProvider::AUTH_DENIED
end

Digest Auth

httpd.conf

<Location /digest/>
  AuthType digest
  AuthName "hobbits"
  AuthBasicProvider mruby
  mrubyAuthnGetRealmHash /path/to/authn_digest.rb
  require valid-user
</Location>

/path/to/authn_digest.rb

realm_user_list = {
  "hobbits" => {
    "bilbo" => "foo",
    "frodo" => "bar",
    "samwise" => "baz",
  },
  "humans" => {
    "aragorn" => "qux",
  },
  "elves" => {
    "legolas" => "quux",
  },
  "dwarves" => {
    "gimli" => "corge",
  },
}

anp = Apache::AuthnProvider.new

user_list = realm_user_list[anp.realm]
if user_list.nil?
  Apache.return Apache::AuthnProvider::AUTH_USER_NOT_FOUND
else
  password = user_list[anp.user]
  if password.nil?
    Apache.return Apache::AuthnProvider::AUTH_USER_NOT_FOUND
  else
    anp.rethash = Digest::MD5.hexdigest([anp.user, anp.realm, password].join(":"))
    Apache.return Apache::AuthnProvider::AUTH_USER_FOUND
  end
end

Output Filter Convering Markdown to HTML

.md markdown file convert to html using output filter

Activate mruby-discount when build mod_mruby

  # use markdown on mod_mruby
  conf.gem :git => 'git://github.com/matsumoto-r/mruby-discount.git'

httpd.conf

<FilesMatch "^.*\.md$">
    AddType text/html .md
    SetOutputFilter mruby
    mrubyOutputFilter /path/to/filter.rb
</FilesMatch>

/path/to/filter.rb

f = Apache::Filter.new

# Get response data
md_data = f.flatten
f.cleanup

# Setup markdown convert engine
css = "https://gist.github.com/andyferra/2554919/raw/2e66cabdafe1c9a7f354aa2ebf5bc38265e638e5/github.css"
title = "markdown"
md = Discount.new css, title

# Convert markdown document to html
html = md.md2html md_data

# Set response data
f.insert_tail html
f.insert_eos

test.md

# Section
## aaa

- hoge
- foo

## bbb

__code__

    a = 1
    b = a + 1

Access markdown file

http://example.com/test.md image


response


Markdown Web Page

Activate mruby-discount when build mod_mruby

  # use markdown on mod_mruby
  conf.gem :git => 'git://github.com/matsumoto-r/mruby-discount.git'

httpd.conf

<Location /mruby>
  AddHandler mruby-script .rb
</Location>

md.rb

r = Apache::Request.new
r.content_type = "text/html"

# setup markdown engine
title = "md test"
css = "https://gist.github.com/andyferra/2554919/raw/2e66cabdafe1c9a7f354aa2ebf5bc38265e638e5/github.css"
md = Discount.new css, title

# create markdown data
body = <<DATA

# Section
## aaa

- hoge
- foo

## bbb

__code__

    a = 1
    b = a + 1

DATA

# create html
html = md.header
html << body.to_html
html << md.footer

# create reponse
Apache.echo html

Response (images)

Access to http://example.com/md.rb response

Something went wrong with that request. Please try again.