hanami / api

Minimal, lightweight, fastest Ruby framework for HTTP APIs.
https://hanamirb.org
MIT License
350 stars 13 forks source link

Can't connect my route using method 'use' #6

Closed serjpetrenko closed 4 years ago

serjpetrenko commented 4 years ago

Hi everyone,

How to correctly use namespace /api/v1/ for example in Roda?

I faced the problem of connecting my route using Rack Middleware it's from this section.

I use the next structure:

# app.rb file

# frozen_string_literal: true

require 'bundler/setup'
require 'hanami/api'

Dir.glob('./api/v1/*.rb').sort.each { |f| require f }

class App < Hanami::API
  scope 'api' do
    scope 'v1' do
      use API::V1::Base
    end
  end
end

Gemfile


# frozen_string_literal: true

source "https://rubygems.org"

ruby '2.7.0'

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'hanami-api'

config.ru

# frozen_string_literal: true

require_relative 'app'

run App.new

Folder with file /api/v1/base.rb

# frozen_string_literal: true

module API
  module V1
    class Base < Hanami::API
      get '/users' do
        json({ user: 'Test', message: 'Hello'})
      end
    end
  end
end

I run the application with command bundle exec rackup then open the browser on the address localhost:9292/api/v1/users and have next issue

Ruby | /Users/sergiypetrenko/.rvm/gems/ruby-2.7.0/gems/hanami-api-0.1.0/lib/hanami/api.rb: in initialize, line 301
-- | --
GET localhost/api/v1/users
jodosha commented 4 years ago

@serjpetrenko Hello, this isn't intended to be used like this. The invocation to .use is meant to mount a Rack Middleware, not to plug in Ruby namespaces. You may want to use .mount instead.

class App < Hanami::API
  scope 'api' do
    mount API::V1::Base.new, at: "/v1"
  end
end

I will document this in README.

serjpetrenko commented 4 years ago

@jodosha Thanks for your comment.