sinatra 不同于rails,是一个轻量级的ruby 框架,非常轻巧灵活。
基本用法如官网所述:
1
2
3
4
5
6
|
#!/usr/bin/env ruby
require
'sinatra'
get
'/frank-says'
do
'Put this in your pipe & smoke it!'
end
|
默认会监听在127.0.0.1:4567 ,可以稍加配置,如下:
1
2
3
4
5
6
7
8
9
10
11
|
#!/usr/bin/env ruby
require
'sinatra'
configure
do
set
:bind
,
'0.0.0.0'
set
:port
,
'1234'
end
get
'/frank-says'
do
'Put this in your pipe & smoke it!'
end
|
处理404 和 50x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/usr/bin/env ruby
require
'sinatra'
configure
do
set
:bind
,
'0.0.0.0'
set
:port
,
'1234'
end
get
'/frank-says'
do
'Put this in your pipe & smoke it!'
end
not_found
do
status
404
"page not found"
end
error
do
'Sorry there was a error - '
+ env[
'sinatra.error'
].messageend
end
|
变量分为自定义变量和内置变量,使用变量的办法如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env ruby
require
'sinatra'
configure
do
set
:bind
,
'0.0.0.0'
set
:port
,
'1234'
end
get
'/frank'
do
name =
"Frank"
"hello #{name}"
end
get
'/frank-says'
do
"your ip address is #{request.ip}"
end
|
其他一些有用的内置变量如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
request.body # 被客户端设定的请求体(见下)
request.scheme # "http"
request.script_name # "/example"
request.path_info # "/foo"
request.port # 80
request.request_method # "GET"
request.query_string # ""
request.content_length # request.body的长度
request.media_type # request.body的媒体类型
request.host # "example.com"
request.get? # true (其他动词也具有类似方法)
request.form_data? # false
request["SOME_HEADER"] # SOME_HEADER header的值
request.referrer # 客户端的referrer 或者 '/'
request.user_agent # user agent (被 :agent 条件使用)
request.cookies # 浏览器 cookies 哈希
request.xhr? # 这是否是ajax请求?
request.url # "http://example.com/example/foo"
request.path # "/example/foo"
request.ip # 客户端IP地址
request.secure? # false(如果是ssl则为true)
request.forwarded? # true (如果是运行在反向代理之后)
request.env # Rack中使用的未处理的env哈希
|
为了避免root运行的安全问题,可以切换一下身份(apache,uid 48)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/env ruby
require
'sinatra'
Process::
UID
.change_privilege(
48
)
if
Process.uid ==
0
configure
do
set
:bind
,
'0.0.0.0'
set
:port
,
'1234'
end
get
'/frank-says'
do
"your ip address is #{request.ip}"
end
|
采用ruby xxx.rb 的方式启动sinatra程序,缺点是必须在前台运行。
如果需要在后台运行,可以采用supervisor,创建配置文件/etc/supervisor/conf.d/sinatra.conf,内容如下,仅供参考:
1
2
3
4
5
6
7
8
9
10
11
12
|
[program:sinatra]
command=/usr/bin/ruby sinatra.rb
process_name=%(program_name)s
numprocs=1
directory=/data/ruby
umask=022
priority=999
autostart=true
user=apache
redirect_stderr=true
stdout_logfile=/var/log/sinatra/access.log
stderr_logfile=/var/log/sinatra/error.log
|
附
CentOS7 安装ruby + sinatra 环境
1
2
|
yum
install
ruby ruby-devel rubygems rubygem-rack
gem
install
sinatra thin
|
ubuntu 16.04 安装ruby + sinatra 环境
1
|
apt-get
install
ruby-sinatra ruby-sinatra-contrib thin
|
ps:官方推荐安装thin,因为它比默认的webrick性能更好,sinatra默认优先使用thin
update 2017-03-20
jruby效率高于ruby,如果使用jruby,由于thin是c扩展,jruby不支持,可以考虑在jruby中使用puma
,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/usr/bin/env ruby
require
'sinatra'
require
'puma'
#switch to apache user if run as root
Process::UID.change_privilege(48)
if
Process.uid == 0
configure
do
set
:bind,
'0.0.0.0'
set
:port,
'4566'
set
:logging,
'log/sinatra.log'
set
:server, :puma
end
get
'/'
do
"your ip address is #{request.ip}"
end
|
本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/1895952,如需转载请自行联系原作者