https://github.com/threez/context.cr
The missing context shard for crystal
https://github.com/threez/context.cr
Last synced: 1 day ago
JSON representation
The missing context shard for crystal
- Host: GitHub
- URL: https://github.com/threez/context.cr
- Owner: threez
- License: mit
- Created: 2023-04-16T13:53:37.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-16T14:04:28.000Z (about 3 years ago)
- Last Synced: 2025-02-07T08:12:34.362Z (over 1 year ago)
- Language: Crystal
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# context [](https://github.com/threez/context.cr/actions/workflows/ci.yml) [](https://threez.github.io/context.cr/)
Coming from languages like golang you might miss the equivalent of a context providing
library to help support your cross functional requirements. This library adds the
context right to the current `Fiber`. It handles immutable context variables using `Fiber.current.with_values` that can be accessed by `Fiber.current[]`.
The heavy lifting is done by the stdlib `Log::Metadata` implementation.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
context:
github: threez/context.cr
```
2. Run `shards install`
## Usage
```crystal
require "context"
require "http/server"
require "http/client"
class AuthHandler
include HTTP::Handler
def call(context)
# take token from request and verify it
Fiber.current.with_values token: "123" do
call_next(context)
end
end
end
# lets assume that is a different internal micro service that wants to get the token...
client = HTTP::Client.new(URI.parse("https://example.com"))
client.before_request do |context|
context.headers["Authorization"] = "Bearer #{Fiber.current[:token]}"
end
server = HTTP::Server.new([
AuthHandler.new,
]) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world, got #{context.request.path}!"
client.get("/secret/data")
end
puts "Listening on http://127.0.0.1:8080"
server.listen(8080)
```
## Resources
* [Forum Discussion](https://forum.crystal-lang.org/t/how-do-you-manage-context/5572)
## Contributing
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [threez](https://github.com/threez) - creator and maintainer