In a Rails project, there's a namespace called api
. I'm looking into making the API versioned so that api/v1
would be the preferred namespace.
For the routing, I was thinking of doing this:
namespace :api do
redirect_api_path_to_api_v1_path
namespace :v1 do
...
end
end
Its a large project, so I was thinking that I would still support the v1 endpoint and just redirect people to v1. Then slowly add v2 and at a point in time, do a v2 redirect.
What I've tried:
namespace :api do
namespace :v1 do
...
resources :countries
...
end
match "*", to: redirect(-> (params, request) {
"https://#{request.host_with_port}/api/v1#{request.path.split("/api").last}"
}), via: [:get, :post, :put, :post]
end
Specs return:
it "should redirect to v1" do
get "/api/countries"
expect(response).to have_http_status 302
end
Failures:
1) Version /api should redirect to v1
Failure/Error: get "/api/countries"
ActionController::RoutingError:
No route matches [GET] "/api/countries"