Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document adding a RESTful API #151

Open
dblock opened this issue Oct 27, 2022 · 5 comments
Open

Document adding a RESTful API #151

dblock opened this issue Oct 27, 2022 · 5 comments

Comments

@dblock
Copy link
Collaborator

dblock commented Oct 27, 2022

Coming from #150 (comment), document how to add a RESTful API.

This monkey-patching should work:

module SlackRubyBotServer
  module Api
    module Endpoints
      class MyEndpoint < Grape::API
         get 'foobar' do
             { ok: true }
         end
      end

      class RootEndpoint < Grape::API
        mount MyEndpoint
      end
    end
  end
end

We should document, and possibly implement a cleaner way to do this.

It's a bit of a rabbit hole. The root endpoint is https://github.com/slack-ruby/slack-ruby-bot-server/blob/master/lib/slack-ruby-bot-server/api/endpoints/root_endpoint.rb, invoked from

Endpoints::RootEndpoint.call(env)
. It's extended in https://github.com/slack-ruby/slack-ruby-bot-server-events/blob/fb68e18314a58f7d345f98e015243307b24c8f88/lib/slack-ruby-bot-server/api/endpoints.rb#L24 for events. The instance of that middleware ( ) is started in

@Altonymous
Copy link

So I had already figured out how to do it on my own prior to looking at this.

In essence all I did was modify config.ru to run SlackRubyBotServer alongside my Grape::API class...

run Rack::Cascade.new [SlackRubyBotServer::Api::Middleware.instance, MyApp::API]

@Altonymous
Copy link

It looks like the recommended way is to mount my Grape API in config/routes.rb for Rails 7. However, when I attempt to do that instead of the above it stops working. The only way I've been able to get both to successfully work together is the Cascade method above.

@dblock
Copy link
Collaborator Author

dblock commented Oct 28, 2022

I like the Rack::Cascade solution because it's clean and independent, but then it doesn't sit behind other stack items, it's a whole stack of its own (e.g. caching or Rack::Cors). I think in the end the mot efficient solution is a single middleware stack where the bot API is mounted next to any other API. Would love to see 1) a proper extension point, 2) a working example added to the sample app, 3) documentation, and 4) a Rails sample/version.

@Altonymous
Copy link

Altonymous commented Oct 28, 2022

Yeah, I'm at the point now where the Cascade isn't working so great. I'm trying to understand better what your monkey-patch above is doing.. to see if I can get it working in my app. Not sure I quite understand what it's doing quite yet though.

Thanks for the guidance so far!

@Altonymous
Copy link

Altonymous commented Oct 28, 2022

I have been unsuccessful in getting your monkey-patch to work yet.

I tried adding the below to my config.ru. Which I thought was an equivalent to what you posted above.. but it continues to block other routes.

module SlackRubyBotServer
  module Api
    module Endpoints
      class MyEndpoint < Grape::API
         mount MyApp::API
      end

      class RootEndpoint < Grape::API
        mount MyEndpoint
      end
    end
  end
end

run SlackRubyBotServer::Api::Middleware.instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants