美文网首页
构建API服务器4

构建API服务器4

作者: yaya_pangdun | 来源:发表于2016-06-17 11:53 被阅读61次

限制API调用频率

使用redis-throttle

gem 'redis-throttle', git: 'git://github.com/andreareginato/redis-throttle.git'
$ bundle install

集成到Rails中,修改config/application.rb

+require 'rack/redis_throttle'

class Application < Rails::Application
+  config.middleware.use Rack::RedisThrottle::Daily, max: 3
end

redis-throttle的redis默认连接是redis://localhost:6379/0,也可以通过设置环境变量ENV['REDIS_RATE_LIMIT_URL']来改变redis-throttle的redis连接。

CORS

CORS可以允许其他域名的网页通过AJAX请求你的API。
我们可以使用rake-corsgem来帮助我们事先CORS。

gem 'rack-cors'

修改config/application.rb

module BuildAnApiRailsDemo
 class Application < Rails::Application
+   config.middleware.insert_before 0, "Rack::Cors" do
+     allow do
+       origins '*'
+       resource '*', :headers => :any, :methods => [:get, :post, :put, :patch, :delete, :options, :head]
+     end
+   end
 end
end

相关文章

  • 构建API服务器4

    限制API调用频率 使用redis-throttle 集成到Rails中,修改config/application...

  • 构建API服务器3

    分页 建立Micropost模型 migration修改 执行: 为id为1的用户创建100条微博记录:lib/t...

  • 构建API服务器2

    1. 实现unauthenticated!方法 2. 增加授权 修改app/controllers/api/v1/...

  • 构建API服务器6

    文档 将slate集成到项目中 集成slate 配置构建文件app/docs/slate/config.rb 现在...

  • 构建API服务器5

    Version 2 API 修改config/routes.rb 生成API::V2::UsersControll...

  • 构建API服务器1

    本文来源于ruby-chinahttps://ruby-china.org/topics/25822 1. 新建项...

  • IDEA+Maven+Embedded Jetty+Jersey

    一、简要介绍 最近做的项目用到了嵌入式Jetty当服务器,并用Jersey来构建Restful api,看了老师的...

  • API自动化测试与持续集成

    目的 如何使用SuperTest测试框架,进行API测试 如何将API测试与构建工具结合 如何将API测试、构建工...

  • Golang:使用 httprouter 构建 API 服务器

    https://medium.com/@gauravsingharoy/build-your-first-api-...

  • keras

    Keras设计了俩种构建模型的方式函数式模型API和顺序式模型API 顺序式模型API构建模型示例: from k...

网友评论

      本文标题:构建API服务器4

      本文链接:https://www.haomeiwen.com/subject/sllvdttx.html