首页
/ Cuba 微框架技术文档

Cuba 微框架技术文档

2024-12-20 07:10:29作者:宗隆裙

1. 安装指南

安装步骤

  1. 确保你已经安装了 Ruby 环境。
  2. 打开终端并运行以下命令来安装 Cuba 框架:
$ gem install cuba

安装完成后,你就可以开始使用 Cuba 框架来开发 Web 应用了。

2. 项目的使用说明

创建一个简单的应用

以下是一个简单的 Cuba 应用示例:

# hello_world.rb
require "cuba"
require "cuba/safe"

Cuba.use Rack::Session::Cookie, :secret => "__a_very_long_string__"

Cuba.plugin Cuba::Safe

Cuba.define do
  on get do
    on "hello" do
      res.write "Hello world!"
    end

    on root do
      res.redirect "/hello"
    end
  end
end

测试文件

你可以编写一个测试文件来验证应用的功能:

# hello_world_test.rb
require "cuba/test"
require "./hello_world"

scope do
  test "Homepage" do
    get "/"

    follow_redirect!

    assert_equal "Hello world!", last_response.body
  end
end

运行应用

创建一个 config.ru 文件来运行你的应用:

# config.ru
require "./hello_world"

run Cuba

然后使用 rackup 命令启动服务器:

$ rackup

访问 http://localhost:9292,你应该能看到 "Hello world!" 的输出。

3. 项目API使用文档

路由匹配器(Matchers)

Cuba 提供了多种路由匹配器,用于处理不同的请求路径和参数。以下是一些常见的匹配器示例:

require "cuba"
require "cuba/safe"

Cuba.use Rack::Session::Cookie, :secret => "__a_very_long_string__"

Cuba.plugin Cuba::Safe

Cuba.define do
  # 仅处理 GET 请求
  on get do
    # 匹配根路径 /
    on root do
      res.write "Home"
    end

    # 匹配 /about
    on "about" do
      res.write "About"
    end

    # 匹配 /styles/basic.css
    on "styles", extension("css") do |file|
      res.write "Filename: #{file}" #=> "Filename: basic"
    end

    # 匹配 /post/2011/02/16/hello
    on "post/:y/:m/:d/:slug" do |y, m, d, slug|
      res.write "#{y}-#{m}-#{d} #{slug}" #=> "2011-02-16 hello"
    end

    # 匹配 /username/foobar
    on "username/:username" do |username|
      user = User.find_by_username(username) # username == "foobar"

      # 匹配 /username/foobar/posts
      on "posts" do
        res.write "Total Posts: #{user.posts.size}" #=> "Total Posts: 6"
      end

      # 匹配 /username/foobar/following
      on "following" do
        res.write user.following.size #=> "1301"
      end
    end

    # 匹配 /search?q=barbaz
    on "search", param("q") do |query|
      res.write "Searched for #{query}" #=> "Searched for barbaz"
    end
  end

  # 仅处理 POST 请求
  on post do
    on "login" do
      # 匹配 POST /login, user: foo, pass: baz
      on param("user"), param("pass") do |user, pass|
        res.write "#{user}:#{pass}" #=> "foo:baz"
      end

      # 如果参数 `user` 和 `pass` 未提供,执行此块
      on true do
        res.write "You need to provide user and pass!"
      end
    end
  end
end

状态码处理

Cuba 会自动处理状态码。如果你没有设置状态码并且没有向 res 对象写入内容,状态码将默认为 404。你可以通过重写 not_found 方法来自定义状态码的处理。

Cuba.define do
  on get do
    on "hello" do
      res.write "hello world"
    end
  end
end

安全插件

Cuba 提供了 Cuba::Safe 插件,用于增强应用的安全性。你可以通过以下方式启用它:

require "cuba/safe"

Cuba.plugin Cuba::Safe

4. 项目安装方式

通过 RubyGems 安装

Cuba 可以通过 RubyGems 安装,只需运行以下命令:

$ gem install cuba

安装完成后,你可以在你的 Ruby 项目中引入 Cuba:

require "cuba"

手动安装

如果你不想使用 RubyGems,也可以手动下载 Cuba 的源码并将其添加到你的项目中。

  1. 从 GitHub 下载 Cuba 的源码:Cuba GitHub 仓库
  2. 将下载的文件解压并放置在你的项目目录中。
  3. 在你的项目中引入 Cuba:
require "./path/to/cuba"

通过以上步骤,你就可以在你的项目中使用 Cuba 框架了。

登录后查看全文
热门项目推荐
相关项目推荐