Skip to content

Instantly share code, notes, and snippets.

@cjgajard
Last active September 12, 2019 08:20
Show Gist options
  • Save cjgajard/b36ffed8070afc53f682b418ebf5476e to your computer and use it in GitHub Desktop.
Save cjgajard/b36ffed8070afc53f682b418ebf5476e to your computer and use it in GitHub Desktop.
Kemal controllers pattern
# src/{{app_name}}.cr
require "kemal"
require "./controllers/*"
alias Env = HTTP::Server::Context # this could be provided by Kemal
module Main
get "/", &->index(Env)
get "/:name", &->greet(Env)
end
module Posts
get "/posts/", &->index(Env)
get "/posts/:id", &->show(Env)
end
Kemal.run
# src/controllers/main_controller.cr
module Main
extend self
def index(env)
"main#index"
end
def greet(env)
"Hello #{env.params.url["name"]}"
end
end
# src/controllers/posts_controller.cr
module Posts
extend self
def index(env)
"posts#index"
end
def show(env)
"posts#show:#{env.params.url["id"]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment