PostgREST / postgrest

REST API for any Postgres database
https://postgrest.org
MIT License
23.37k stars 1.03k forks source link

Failed to Fetch error on Swagger UI on Windows #1580

Open jmaldon1 opened 4 years ago

jmaldon1 commented 4 years ago

Environment

Description of issue

docker-compose.yaml

version: '3'
services:
  server:
    env_file:
      - .env
    image: postgrest/postgrest
    ports:
      - "3000:3000"
    environment:
      PGRST_DB_URI: postgres://${PGUSER}:${PGPASS}@${HOSTNAME}:5432/postgres?sslmode=require
      PGRST_DB_SCHEMA: api
      PGRST_DB_ANON_ROLE: app_user
      # PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3000/"
  swagger:
    image: swaggerapi/swagger-ui
    ports:
      - "8080:8080"
    expose:
      - "8080"
    environment:
      API_URL: http://localhost:3000/

I am getting various failed to fetch errors when trying to use the swagger UI.

When running on Chrome browser: I am getting a TypeError: Failed to fetch error when using the swagger UI. When looking at the browser dev console i get net::ERR_ADDRESS_INVALID.

When running on Firefox browser: I am getting a TypeError: NetworkError when attempting to fetch resource. error when using the swagger UI. When looking at the browser dev console I get Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://0.0.0.0:3000/outfits. (Reason: CORS request did not succeed)..

Everything works fine when hitting the postgrest server from any other means besides browser such as Postman or Curl.

The strange part is that I have a macbook that I ran this same docker-compose up on and the swagger UI works over there (same versions of everything).

UPDATE: When i put the address http://0.0.0.0:3000 in my browser I get This site can’t be reached. When i put the address http://localhost:3000 in my browser it works fine.

jmaldon1 commented 4 years ago

SOLUTION:

I had to add PGRST_OPENAPI_SERVER_PROXY_URI:

    environment:
      PGRST_DB_URI: postgres://${PGUSER}:${PGPASS}@${HOSTNAME}:5432/postgres?sslmode=require
      PGRST_DB_SCHEMA: api
      PGRST_DB_ANON_ROLE: app_user
      PGRST_OPENAPI_SERVER_PROXY_URI: "http://127.0.0.1:3000/"

I had the wrong syntax earlier, and I guess for some reason on mac it can access http://0.0.0.0, but on windows it can't? Not really sure whats up with that.

I guess we can close.

steve-chavez commented 4 years ago

@jmaldon1 Glad you could solve it.

Seeing the windows problem, perhaps we should default to 127.0.0.1 instead of 0.0.0.0 for the OpenAPI host?

We add 0.0.0.0 in these lines:

https://github.com/PostgREST/postgrest/blob/7af54c5813c0fdfdd2b6ba36e6d04af084b96f77/src/PostgREST/OpenAPI.hs#L276-L282

jmaldon1 commented 4 years ago

@steve-chavez That seems like a good solution assuming you were able to recreate the issue and its a common windows thing and not just my machine being dumb haha.

wolfgangwalther commented 4 years ago

I think we should just default to not setting schemes and host in the OpenAPI schema at all, if not specified via configuration. This is because we can never guess it correctly when behind a proxy anyway, right?

https://swagger.io/docs/specification/2-0/api-host-and-base-path/

If schemes are not specified, the scheme used to serve the API specification will be used for API calls. [...] If host is not specified, it is assumed to be the same host where the API documentation is being served. [...] Omitting host and scheme host and schemes can be omitted for a more dynamic association. In this case, the host and scheme used to serve the API documentation will be used for API calls.

This should then work for any OS?