您好,登录后才能下订单哦!
sinatra 不同于rails,是一个轻量级的ruby 框架,非常轻巧灵活。
基本用法如官网所述:
#!/usr/bin/env ruby require 'sinatra' get '/frank-says' do 'Put this in your pipe & smoke it!' end
默认会监听在127.0.0.1:4567 ,可以稍加配置,如下:
#!/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
#!/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
变量分为自定义变量和内置变量,使用变量的办法如下
#!/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
其他一些有用的内置变量如下:
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)
#!/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,内容如下,仅供参考:
[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 环境
yum install ruby ruby-devel rubygems rubygem-rack gem install sinatra thin
ubuntu 16.04 安装ruby + sinatra 环境
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
,代码如下:
#!/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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。