apigee-127 / swagger-hapi

Other
3 stars 2 forks source link

How to enable CORS? #3

Closed vitorbaptista closed 8 years ago

vitorbaptista commented 8 years ago

I'm running a server like:

// https://github.com/opentrials/api/blob/master/server.js
const config = require('./config');
const SwaggerHapi = require('swagger-hapi');
const Hapi = require('hapi');
const server = new Hapi.Server();

SwaggerHapi.create(config.swaggerHapi, (err, swaggerHapi) => {
  const port = config.port;
  const plugins = [swaggerHapi.plugin,
                   ...config.hapi.plugins];
  if (err) { throw err; }

  server.connection({
    host: config.host,
    port,
    routes: {
        cors: true,  // Enabling CORS
    },
  });
  server.address = () => ({ port });

  server.register(plugins, (_err) => {
    if (_err) { throw _err; }
    server.start(() => {
      console.info('Server started at', server.info.uri); // eslint-disable-line no-console
    });
  });
});

module.exports = server; // for testing

Some routes have CORS enabled, but not others (like /v1/swagger.yaml, maybe because it's using x-swagger-pipe: swagger-raw). Adding the routes: { cors: true } to the server.connection() call seems to have no effect at all. Even if I remove it, there're still some routes with CORS enabled.

There seems to be some CORS configurations on swagger-node-runner, but I was unable to figure out how it should work. Could you give me some pointers?

theganyo commented 8 years ago

Hey @vitorbaptista ,

So the cors support included in swagger-node-runner is nothing more than including the "cors" module (https://www.npmjs.com/package/cors) in the pipe that you define in your swagger config.

So, for example, you can see here: https://github.com/theganyo/swagger-node-runner/blob/master/test/assets/project/config/default.yaml that on line 29, it includes "cors" as part of the pipe. Thus, all routes handled as a swagger controller will include cors support. If you don't want cors, just remove that fitting from your pipe.

Does that help?

theganyo commented 8 years ago

Also, I should have mentioned, if you want it on the swagger-raw pipe, you should be able to just add it there as well.

vitorbaptista commented 8 years ago

Thanks for the hint, @theganyo. That really helped! I was able to sort of enable CORS on the swagger_raw pipe, but it feels as if I'm doing it the wrong way.

My swagger configuration file looks like:

swagger:
  bagpipes:
    swagger_raw:
      name: swagger_raw

    swagger_raw_cors:
        - cors
        - swagger_raw

  # ...

I tried adding cors to the actual swagger_raw, but couldn't figure out how. The only way I found was creating a new bagpipe which pipes cors and then the original swagger_raw. Is there an easier way of doing this?

theganyo commented 8 years ago

If I'm not mistaken, I believe you could just drop the first definition (swagger_raw).

vitorbaptista commented 8 years ago

Thanks a lot for your help, @theganyo. This change worked: https://github.com/opentrials/api/pull/24/commits/0cc27e931e809db00022da6ac04d91f6dec26462

theganyo commented 8 years ago

Awesome!