Reputation: 6080
I have a route working just fine in my development environment, but for some reason it's not working in production and I can't figure out why.
When I go to http://localhost:3000/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b
in my development environment, it works great.
However, when I go to http://example.com/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b
it does not resolve, and ends up falling to a "page not found" which is the catch all route at the bottom of my routes file.
In my routes.rb
file I have this:
constraints(ApiConstraint) do
namespace :api, defaults: {format: 'json'} do
namespace :v1, defaults: {format: 'json'} do
match "register_panelist", to: "appusers#register_panelist", via: 'get'
match "get_surveys", to: "appusers#get_surveys", via: 'get'
end
end
match "api/v1/*path", to: "api/v1/misc#api_not_found_404", via: :all, format: 'json'
end
...
#at the very bottom
match "*path", to: "static_pages#not_found_404", via: :all, format: false #, :constraints => {:format => [:html, :png]}
In my development environment it seems to resolve correctly. But in production it seems to be falling through to the bottom of the routes.rb
file.
Any ideas why?
EDIT: Adding Logs:
Production:
production.log — I, [2017-02-12T11:55:31.455331 #27545] INFO -- : Started GET "/api/v1/register_panelist.json?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=10022&state=45700ðnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for 75.100.38.224 at 2017-02-12 11:55:31 +0000
production.log — I, [2017-02-12T11:55:31.606946 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.607886 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.607948 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.608020 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.608052 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.608086 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.617812 #27545] INFO -- : Processing by StaticPagesController#not_found_404 as HTML
production.log — I, [2017-02-12T11:55:31.617925 #27545] INFO -- : Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"10022", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511", "path"=>"api/v1/register_panelist.json"}
production.log — I, [2017-02-12T11:55:31.636463 #27545] INFO -- : Rendered text template (0.2ms)
production.log — I, [2017-02-12T11:55:31.636883 #27545] INFO -- : Completed 404 Not Found in 19ms (Views: 11.4ms | ActiveRecord: 0.0ms)
Development:
Started GET "/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=53593&state=45700ðnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for ::1 at 2017-02-12 05:56:46 -0600
Prod? false
subdomain:
protocol: http://
Processing by Api::V1::AppusersController#register_panelist as JSON
Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"53593", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511"}
User Load (18.7ms) SELECT "users".* FROM "users" WHERE "users"."api_key" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["api_key", "ff4a6fa1c975693bedc2122e6943946b"]]
rake routes
output from development:
api_v1_register_panelist GET /api/v1/register_panelist(.:format) api/v1/appusers#register_panelist {:format=>"json"}
api_v1_get_surveys GET /api/v1/get_surveys(.:format) api/v1/appusers#get_surveys {:format=>"json"}
and production server:
api_v1_register_panelist GET /api/v1/register_panelist(.:format) api/v1/appusers#register_panelist {:format=>"json"}
api_v1_get_surveys GET /api/v1/get_surveys(.:format) api/v1/appusers#get_surveys {:format=>"json"}
Upvotes: 0
Views: 1612
Reputation: 6080
I'm not positive why this works, but it seems I had to update my routes.rb
file to call out that line twice - once within the constraints to find the routes in my development environment, and once without to be found in production:
constraints(ApiConstraint) do
namespace :api, defaults: {format: 'json'} do
namespace :v1, defaults: {format: 'json'} do
#find in development
match "register_panelist", to: "appusers#register_panelist", via: 'get'
match "get_surveys", to: "appusers#get_surveys", via: 'get'
end
end
match "api/v1/*path", to: "api/v1/misc#api_not_found_404", via: :all, format: 'json'
end
#find in production
match "api/v1/register_panelist", to: "api/v1/appusers#register_panelist", via: 'get'
match "api/v1/get_surveys", to: "api/v1/appusers#get_surveys", via: 'get'
Upvotes: 1
Reputation: 5281
I would guess that there is something in the web server configuration that is usurping the request before it gets passed to Rails (mod rewrite or the like), or that the mount point setting of the Rails execution environment within your web server (e.g. Unicorn in Nginx, Passenger in Apache) , or maybe a piece of middleware that is handled differently by WEBrick and your live server.
Whatever the case, I would start by checking the routes on the server:
rake routes
and make sure they are what you expect. Next, inspect the rails logs and verify that the request is actually being handled by Rails and not being intercepted by the web server or some middleware. If you crank log level up to debug it should give details about exactly how it is handling it. This should at least give you the next step for where to look.
Upvotes: 1