-
Notifications
You must be signed in to change notification settings - Fork 15
/
sample.cr
55 lines (48 loc) · 1.21 KB
/
sample.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "../src/router"
class WebServer
# Add Router functions to WebServer
include Router
def initialize
end
# Define a method to draw routes of your server
# Here we define
# GET "/"
# GET "/user/:id"
# POST "/user"
def draw_routes
# Define index access for this server
# We just print a result "Hello router.cr!" here
get "/" do |context, _|
context.response.print "Hello router.cr!"
context
end
# 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
# 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, _|
context
end
end
# Running this server on port 3000
# router_handler getter of RouteHandler
# that's defined in Router module
def run
server = HTTP::Server.new(route_handler)
server.bind_tcp("127.0.0.1", 3000)
server.listen
end
end
web_server = WebServer.new
web_server.draw_routes
web_server.run