|
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] |
4 | 5 |
|
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. |
| 6 | +Leopard is a small framework for building concurrent {service-api} workers. |
| 7 | +It uses `Ractor` to manage multiple workers in a single process and provides a |
| 8 | +minimal DSL for defining endpoints and middleware. |
| 9 | + |
| 10 | +== Features |
| 11 | + |
| 12 | +* Declarative endpoint definitions with `endpoint`. |
| 13 | +* Middleware support using `use`. |
| 14 | +* Simple concurrency via `run` with a configurable number of instances. |
| 15 | +* JSON aware message wrapper that gracefully handles parse errors. |
| 16 | +* Dry::Configurable settings container (currently exposing `root` and `libroot` paths). |
| 17 | + |
| 18 | +== Requirements |
| 19 | + |
| 20 | +* Ruby >= 3.3.0 |
| 21 | +* A running NATS server with the Service API enabled. |
| 22 | + |
| 23 | +== Installation |
| 24 | + |
| 25 | +Add the gem to your project: |
| 26 | + |
| 27 | +[source,ruby] |
| 28 | +---- |
| 29 | +# Gemfile |
| 30 | +gem 'leopard' |
| 31 | +---- |
| 32 | + |
| 33 | +Then install it with Bundler. |
| 34 | + |
| 35 | +[source,bash] |
| 36 | +---- |
| 37 | +$ bundle install |
| 38 | +---- |
| 39 | + |
| 40 | +== Usage |
| 41 | + |
| 42 | +Create a service class and include `Rubyists::Leopard::NatsApiServer`. |
| 43 | +Define one or more endpoints. Each endpoint receives a |
| 44 | +`Rubyists::Leopard::MessageWrapper` object. |
| 45 | + |
| 46 | +[source,ruby] |
| 47 | +---- |
| 48 | +class EchoService |
| 49 | + include Rubyists::Leopard::NatsApiServer |
| 50 | +
|
| 51 | + endpoint :echo do |msg| |
| 52 | + Success(msg.data) |
| 53 | + end |
| 54 | +end |
| 55 | +---- |
| 56 | + |
| 57 | +Run the service by providing the NATS connection details and service options: |
| 58 | + |
| 59 | +[source,ruby] |
| 60 | +---- |
| 61 | +EchoService.run( |
| 62 | + nats_url: 'nats://localhost:4222', |
| 63 | + service_opts: { name: 'echo' }, |
| 64 | + instances: 4 |
| 65 | +) |
| 66 | +---- |
| 67 | + |
| 68 | +Middleware can be inserted around endpoint dispatch: |
| 69 | + |
| 70 | +[source,ruby] |
| 71 | +---- |
| 72 | +class LoggerMiddleware |
| 73 | + def initialize(app) |
| 74 | + @app = app |
| 75 | + end |
| 76 | +
|
| 77 | + def call(wrapper) |
| 78 | + puts "received: #{wrapper.data.inspect}" |
| 79 | + @app.call(wrapper) |
| 80 | + end |
| 81 | +end |
| 82 | +
|
| 83 | +EchoService.use LoggerMiddleware |
| 84 | +---- |
| 85 | + |
| 86 | +== Development |
| 87 | + |
| 88 | +The project uses Minitest and RuboCop. Run tests with Rake: |
| 89 | + |
| 90 | +[source,bash] |
| 91 | +---- |
| 92 | +$ bundle exec rake |
| 93 | +---- |
| 94 | + |
| 95 | +=== Conventional Commits (semantic commit messages) |
| 96 | + |
| 97 | +This project follows the {conventional-commits} specification. |
| 98 | + |
| 99 | +To contribute, please follow that commit message format, |
| 100 | +or your pull request may be rejected. |
| 101 | + |
| 102 | +== License |
| 103 | + |
| 104 | +MIT |
0 commit comments