-
-
Save bbg/3340274db95dc29331ac10228f645ddd to your computer and use it in GitHub Desktop.
Kemal controllers pattern
This file contains hidden or 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 hidden or 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 hidden or 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