-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from tbrand/feature/api-to-action
api to action
- Loading branch information
Showing
10 changed files
with
171 additions
and
206 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,54 @@ | ||
require "../src/router" | ||
|
||
class WebServer | ||
# Add Router functions to WebServer.class | ||
# Add Router functions to WebServer | ||
include Router | ||
|
||
# Initialize RouteHandler | ||
@route_handler = RouteHandler.new | ||
|
||
# To define API, call API.new with context and params(optional) where | ||
# context : HTTP::Server::Context | ||
# params : Hash(String, String) | ||
# | ||
# HTTP::Server::Context is a default context of the http request/response | ||
# This includes body, header, response and so on | ||
# See https://crystal-lang.org/api/HTTP/Server/Context.html | ||
# All API have to return HTTP::Server::Context | ||
# For this, Basically, you just put the context on the last line of each API | ||
|
||
# GET "/" | ||
@index = API.new do |context| | ||
context.response.print "Hello router.cr" | ||
context | ||
end | ||
|
||
# params is used when you define parameters in your url such as '/user/:id' (path parameters) | ||
# In this case, you can get the 'id' by params["id"] | ||
# GET "/user/:id" | ||
@user = API.new do |context, params| | ||
context.response.print params["id"] # get :id in url from params | ||
context | ||
def initialize | ||
end | ||
|
||
# Define a method to draw routes of your server | ||
# Here we define | ||
# GET "/" | ||
# GET "/user/:id" | ||
# POST "/user" | ||
@register_user = API.new do |context| | ||
context | ||
end | ||
def draw_routes | ||
# Define index access for this server | ||
# We just print a result "Hello router.cr!" here | ||
get "/" do |context, params| | ||
context.response.print "Hello router.cr!" | ||
context | ||
end | ||
|
||
def initialize | ||
# Define routes in `draw` block | ||
draw(@route_handler) do | ||
get "/", @index | ||
get "/user/:id", @user | ||
post "/user", @register_user | ||
# You can get path parameter from `params` param | ||
# It's a Hash of String => String | ||
get "/user/:id" do |context, params| | ||
context.response.print params["id"] | ||
context | ||
end | ||
|
||
# Try | ||
# curl localhost:3000 | ||
# curl localhost:3000/user/3 | ||
# curl localhost:3000/user -X POST | ||
# Currently you can define a methods in following list | ||
# get -> GET | ||
# post -> POST | ||
# put -> PUT | ||
# patch -> PATCH | ||
# delete -> DELETE | ||
# options -> OPTIONS | ||
# Here we define POST route | ||
post "/user" do |context, params| | ||
context | ||
end | ||
end | ||
|
||
# Running this server on port 3000 | ||
# router_handler getter of RouteHandler | ||
# that's defined in Router module | ||
def run | ||
# set RouteHandler to your server | ||
server = HTTP::Server.new(3000, @route_handler) | ||
server = HTTP::Server.new(3000, route_handler) | ||
server.listen | ||
end | ||
end | ||
|
||
# Initialize WebServer.class | ||
web_server = WebServer.new | ||
|
||
# Start running | ||
web_server.draw_routes | ||
web_server.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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
require "./router/*" | ||
require "./router/handler" | ||
require "./router/version" | ||
|
||
module Router | ||
# Main module | ||
alias Action = HTTP::Server::Context, Hash(String, String) -> HTTP::Server::Context | ||
alias RouteContext = NamedTuple(action: Action, params: Hash(String, String)) | ||
|
||
getter route_handler : RouteHandler = RouteHandler.new | ||
|
||
HTTP_METHODS = %w(get post put patch delete options) | ||
|
||
# Define each method for supported http methods | ||
{% for http_method in HTTP_METHODS %} | ||
def {{http_method.id}}(path : String, &block : Action) | ||
@route_handler.add_route("{{http_method.id.upcase}}" + path, block) | ||
end | ||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require "radix" | ||
require "http/server" | ||
require "./handler/handler" |
Oops, something went wrong.