request_id
request_id
Middleware for generates / pick up a unique request ID for Crystal servers.
This module set response HTTP Header like this:
X-Request-ID: 753a468b-4179-40c4-bb58-ff508cb39d67
Installation
Add this to your application's shard.yml
:
dependencies:
request_id:
github: SuperPaintman/request-id
Usage
With pure Crystal server
require "request_id"
require "http/server"
server = HTTP::Server.new("0.0.0.0", 8080,
[
RequestID::Handler.new
]) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello request id!"
end
server.listen
With Kemal
require "request_id"
require "kemal"
add_handler RequestID::Handler.new
get "/" do |env|
"Hello request id!"
end
Kemal.run
Custom Generator
By default:
uuid
require "request_id"
require "http/server"
class CustomRequestIDHandler < RequestID::Handler
def generator
"custom_request_id_generator"
end
end
server = HTTP::Server.new("0.0.0.0", 8080,
[
CustomRequestIDHandler.new
]) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello request id!"
end
server.listen
Examples
Random ID
curl -I localhost:8080
# HTTP/1.1 200 OK
# X-Request-ID: 5e3df631-85e6-4eaf-bb1c-68375291332e
Set ID via Header
curl -I -H 'X-Request-ID:4ce4089d-e01e-4c7f-b35c-2ca383c7abf5' localhost:8080
# HTTP/1.1 200 OK
# X-Request-ID: 4ce4089d-e01e-4c7f-b35c-2ca383c7abf5
Test
crystal spec
Contributing
- Fork it (https://github.com/SuperPaintman/request-id/fork)
- Create your feature branch (
git checkout -b feature/<feature_name>
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin feature/<feature_name>
) - Create a new Pull Request
Contributors
- SuperPaintman SuperPaintman - creator, maintainer