|
1 | | -# Leopard Nats ServiceApi Server |
| 1 | += Leopard NATS ServiceApi Server |
2 | 2 | bougyman <me@bougyman.com> |
3 | | -:service-api: https://github.com/rubyists/nats-pure.rb/blob/main/docs/service_api.md[Service API] |
| 3 | +:service-api: https://github.com/rubyists/nats-pure.rb/blob/main/docs/service_api.md[NATS Service API] |
| 4 | +:conventional-commits: https://www.conventionalcommits.org/en/v1.0.0/[Conventional Commits] |
| 5 | +:dry-configurable: https://github.com/dry-rb/dry-configurable[Dry::Configurable] |
| 6 | +:dry-monads: https://github.com/dry-rb/dry-monads[Dry::Monads] |
4 | 7 |
|
5 | | -The leopard nats serviceapi server provides a simple concurrency |
6 | | -model for NATS {service-api} workers. It is designed to be used |
7 | | -similarly to a web server (inspired by puma), defining endpoints |
8 | | -in your classes, and then serving them via the leopard (Ractor-based) |
9 | | -service supervisor. |
| 8 | +Leopard is a small framework for building concurrent {service-api} workers. |
| 9 | +It uses `Concurrent::FixedThreadPool` to manage multiple workers in a single process and provides a |
| 10 | +minimal DSL for defining endpoints and middleware. |
| 11 | + |
| 12 | +== Features |
| 13 | + |
| 14 | +* Declarative endpoint definitions with `endpoint`. |
| 15 | +* Middleware support using `use`. |
| 16 | +* Simple concurrency via `run` with a configurable number of instances. |
| 17 | +* JSON aware message wrapper that gracefully handles parse errors. |
| 18 | +* Railway Oriented Design, using {dry-monads} for success and failure handling. |
| 19 | +* {dry-configurable} settings container. |
| 20 | + |
| 21 | +== Requirements |
| 22 | + |
| 23 | +* Ruby >= 3.3.0 |
| 24 | +* A running NATS server with the Service API enabled. |
| 25 | + |
| 26 | +== Installation |
| 27 | + |
| 28 | +Add the gem to your project: |
| 29 | + |
| 30 | +[source,ruby] |
| 31 | +---- |
| 32 | +# Gemfile |
| 33 | +gem 'leopard' |
| 34 | +---- |
| 35 | + |
| 36 | +Then install it with Bundler. |
| 37 | + |
| 38 | +[source,bash] |
| 39 | +---- |
| 40 | +$ bundle install |
| 41 | +---- |
| 42 | + |
| 43 | +== Usage |
| 44 | + |
| 45 | +Create a service class and include `Rubyists::Leopard::NatsApiServer`. |
| 46 | +Define one or more endpoints. Each endpoint receives a |
| 47 | +`Rubyists::Leopard::MessageWrapper` object for each request to the {service-api} endpoint |
| 48 | +that service class is is subscribed to (subject:, or name:). The message handler/callback |
| 49 | +is expected to return a `Dry::Monads[:result]` object, typically a `Success` or `Failure`. |
| 50 | + |
| 51 | +[source,ruby] |
| 52 | +---- |
| 53 | +class EchoService |
| 54 | + include Rubyists::Leopard::NatsApiServer |
| 55 | +
|
| 56 | + endpoint :echo do |msg| |
| 57 | + Success(msg.data) |
| 58 | + end |
| 59 | +end |
| 60 | +---- |
| 61 | + |
| 62 | +Run the service by providing the NATS connection details and service options: |
| 63 | + |
| 64 | +[source,ruby] |
| 65 | +---- |
| 66 | +EchoService.run( |
| 67 | + nats_url: 'nats://localhost:4222', |
| 68 | + service_opts: { name: 'echo' }, |
| 69 | + instances: 4 |
| 70 | +) |
| 71 | +---- |
| 72 | + |
| 73 | +Middleware can be inserted around endpoint dispatch: |
| 74 | + |
| 75 | +[source,ruby] |
| 76 | +---- |
| 77 | +class LoggerMiddleware |
| 78 | + def initialize(app) |
| 79 | + @app = app |
| 80 | + end |
| 81 | +
|
| 82 | + def call(wrapper) |
| 83 | + puts "received: #{wrapper.data.inspect}" |
| 84 | + @app.call(wrapper) |
| 85 | + end |
| 86 | +end |
| 87 | +
|
| 88 | +EchoService.use LoggerMiddleware |
| 89 | +---- |
| 90 | + |
| 91 | +== Development |
| 92 | + |
| 93 | +The project uses Minitest and RuboCop. Run tests with Rake: |
| 94 | + |
| 95 | +[source,bash] |
| 96 | +---- |
| 97 | +$ bundle exec rake |
| 98 | +---- |
| 99 | + |
| 100 | +=== Conventional Commits (semantic commit messages) |
| 101 | + |
| 102 | +This project follows the {conventional-commits} specification. |
| 103 | + |
| 104 | +To contribute, please follow that commit message format, |
| 105 | +or your pull request may be rejected. |
| 106 | + |
| 107 | +== License |
| 108 | + |
| 109 | +MIT |
0 commit comments