Created
October 4, 2020 15:02
-
-
Save onliniak/c2fa3c035b83527a3fb94d03caa54b10 to your computer and use it in GitHub Desktop.
Crystal router 2.0
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
| require "./router.cr" | |
| server = HTTP::Server.new(Router.new) | |
| server.bind_tcp "127.0.0.1", 8080 | |
| server.listen |
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
| require "http/server" | |
| class Router | |
| include HTTP::Handler | |
| macro route(a, b, c = " ", d = "text/plain") | |
| if method == {{a}} && | |
| path == "/{{b}}" | |
| context.response.content_type = {{d}} | |
| {{b}} | |
| context.response.print {{c}} | |
| context.response.close | |
| end | |
| end | |
| def initialize | |
| end | |
| def app | |
| puts "Never fear, app is here" | |
| end | |
| def call(context) | |
| method = context.request.method | |
| path = context.request.path | |
| route "GET", app, "Hello World!" | |
| context.response.respond_with_status(500) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.