https://github.com/shellbear/qleex
A minimal HTTP Middleware router for Crystal.
https://github.com/shellbear/qleex
crystal http middleware radix router
Last synced: 12 months ago
JSON representation
A minimal HTTP Middleware router for Crystal.
- Host: GitHub
- URL: https://github.com/shellbear/qleex
- Owner: shellbear
- Created: 2019-04-20T01:08:35.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-20T02:03:10.000Z (almost 7 years ago)
- Last Synced: 2024-12-31T21:24:24.050Z (about 1 year ago)
- Topics: crystal, http, middleware, radix, router
- Language: Crystal
- Size: 1.95 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Qleex
A minimal HTTP Middleware router for Crystal.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
qleex:
github: ShellBear/qleex
```
## Usage
```crystal
require "http/server"
require "qleex"
router = Qleex::Router.new
# You can define one or more paths for get, post, put, delete, patch and options HTTP methods
router.get("/hello", -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "Hello world!"
})
router.post("/post", -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "This is POST request"
})
# You can define multiple methods for one or more paths.
router.route(["GET", "POST"], "/ping", -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "pong"
})
# You can define multiple paths for one or more methods.
router.route("GET", ["/ex1", "/ex2", "/ex3"], -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "You matched one of these URLs: [/ex1, /ex2, /ex3]"
})
# You can add parameters to the path by using the ":" character
router.route("GET", "/user/:id", -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "User #{params["id"]}"
})
# You can use a wildcard to match everything
router.route("GET", "/*", -> (context : Qleex::Context, params : Qleex::Parameters) {
context.response.content_type = "text/plain"
context.response.print "Wow"
})
server = HTTP::Server.new(router)
address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen
```