Last active
September 12, 2019 08:20
-
-
Save cjgajard/b36ffed8070afc53f682b418ebf5476e to your computer and use it in GitHub Desktop.
Kemal controllers pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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