-
Notifications
You must be signed in to change notification settings - Fork 30
Use Case
miminashi edited this page Mar 25, 2016
·
29 revisions
- Hello World
- Reverse Proxy Selectively Measuring Busy Worker Rate
- Basic Auth with Redis
- Digest Auth
- Output Filter Convering Markdown to HTML
- Markdown Web Page
- Input filter for POST method(in Japanese)
- Insert footer string using output filter(in Japanese)
- Count downloaded file like mod_security(in Japanese)
Apache.echo "Hello World"
<Location /hello>
mrubyHandlerMiddle /path/to/hello.rb;
</Location>
$ curl http://127.0.0.1/hello
Hello World
<Location /proxy>
mrubyTranslateNameMiddle /path/to/proxy.rb
</Location>
# 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
<Location /basic/>
AuthType basic
AuthName "Message for clients"
AuthBasicProvider mruby
mrubyAuthnCheckPassword /path/to/authn_basic.rb
require valid-user
</Location>
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
<Location /digest/>
AuthType digest
AuthName "hobbits"
AuthBasicProvider mruby
mrubyAuthnGetRealmHash /path/to/authn_digest.rb
require valid-user
</Location>
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
.md
markdown file convert to html using output filter
# use markdown on mod_mruby
conf.gem :git => 'git://github.com/matsumoto-r/mruby-discount.git'
<FilesMatch "^.*\.md$">
AddType text/html .md
SetOutputFilter mruby
mrubyOutputFilter /path/to/filter.rb
</FilesMatch>
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
# Section
## aaa
- hoge
- foo
## bbb
__code__
a = 1
b = a + 1
http://example.com/test.md image
# use markdown on mod_mruby
conf.gem :git => 'git://github.com/matsumoto-r/mruby-discount.git'
<Location /mruby>
AddHandler mruby-script .rb
</Location>
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
Access to http://example.com/md.rb