Lucky 是一个用 Crystal 写的 Web 框架,它可以帮助你在编译时捕获 bug,并提供快速的响应,使编写 Web 应用程序快速、有趣和容易。
Lucky 使用 Action 类来处理 HTTP 请求和响应,类将它处理的路由和参数映射到响应块。
特性:
JSON API
class Api::Users::Show < ApiAction get "/api/users/:user_id" do json user_json end private def user_json user = UserQuery.find(user_id) {name: user.name, email: user.email} end end
Database
# Set up the model class User < BaseModel table :users do column last_active_at : Time column last_name : String end end # Add some methods to help query the database class UserQuery < User::BaseQuery def recently_active last_active_at.gt(1.week.ago) end def sorted_by_last_name last_name.lower.desc_order end end # Query the database UserQuery.new.recently_active.sorted_by_last_name
Rendering HTML:
class Users::Index < BrowserAction route do users = UserQuery.new.sorted_by_last_name render IndexPage, users: users end end class Users::IndexPage < MainLayout needs users : UserQuery def content render_new_user_button render_user_list end private def render_new_user_button link "New User", to: Users::New end private def render_user_list ul class: "user-list" do @users.each do |user| li do link user.name, to: Users::Show.with(user.id) text " - " text user.nickname || "No Nickname" end end end end end