fastify / help

Need help with Fastify? File an Issue here.
https://www.fastify.io/
65 stars 8 forks source link

Unable to get fastify/swagger working with fastify/aws-lambda #762

Closed arnabk closed 2 years ago

arnabk commented 2 years ago

You have already researched for similar issues?

Yes, searched keywords swagger+lambda/serverless does not return anything

What are you trying to achieve, or the steps to reproduce?

I am trying to enable swagger using serverlessJs. It works if I run a standalone server, but the same code does not generate swagger docs and routes when used with fastify/aws-lambda

index.js

const awsLambdaFastify = require("@fastify/aws-lambda");
const { app } = require("./App");

const proxy = awsLambdaFastify(app, {
  callbackWaitsForEmptyEventLoop: false,
  decorateRequest: false,
});
app.ready().then(() => {
  app.swagger();
});

export const handler = proxy;

App.js

const { fastify } = require('fastify');

export const app = fastify({ logger: true });

app.register(require('@fastify/swagger'), {
  routePrefix: '/documentation',
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    },
    consumes: ['application/json'],
    produces: ['application/json'],
  },
  uiConfig: {
    docExpansion: 'full',
    deepLinking: false
  },
  exposeRoute: true
});

app.get('/rest', async (request, reply) => {
  return { hello: 'world' }
});

export default app;

serverless.yml (although might not be needed here)

service: sls-test
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
frameworkVersion: '3'

# Add the serverless-webpack plugin
plugins:
  - serverless-esbuild
  - serverless-offline
provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-north-1

functions:
  rest:
    handler: index.handler
    events:
      - http:
          method: ANY
          path: /{any+}
          cors: true

Standalone version of the code that works

const { fastify: fastifyApp } = require('fastify')
const fastify = fastifyApp({ logger: true });

(async () => {

  await fastify.register(require('@fastify/swagger'), {
    routePrefix: '/documentation',
    swagger: {
      info: {
        title: 'Test swagger',
        description: 'Testing the Fastify swagger API',
        version: '0.1.0'
      },
      consumes: ['application/json'],
      produces: ['application/json'],
    },
    uiConfig: {
      docExpansion: 'full',
      deepLinking: false
    },
    exposeRoute: true
  })

  // Declare a route
  fastify.get('/', async (request, reply) => {
    return { hello: 'world' }
  })

  try {
    await fastify.listen({ port: 4000 });
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }

})();

What was the result you received?

When I open http://localhost:3000/dev/documentation, it redirects to http://localhost:3000/dev/documentation/static/index.html with following content

{
"message": "Route GET:/documentation/static/index.html not found",
"error": "Not Found",
"statusCode": 404
}

What did you expect?

I was expecting it would show swagger default index.html page

Context

Eomm commented 2 years ago

Maybe @fastify/aws-lambda could help?

adrai commented 2 years ago

does the /rest route work?

adrai commented 2 years ago

Have you tried to set routePrefix: '/dev/documentation'?

adrai commented 2 years ago

It may be the problem could be also somewhere in the getRedirectPathForTheRootRoute function maybe for api-gw+lambda environments the staticPrefix is not correct.

arnabk commented 2 years ago

@Eomm Should I post this in @fastify/aws-lambda. I spent several hours figuring out where could be the problem. I could only think that aws-lambda could be messing with it since it works as standalone server.

@adrai Yes /dev/rest works. I tried with both routePrefix: '/dev/documentation', and routePrefix: '/documentation',, but it does not work.

Where is fastify-static serving the swagger content from?

arnabk commented 2 years ago

I am going to close this and reopen here https://github.com/fastify/aws-lambda-fastify. Sounds like aws lambda fastify issue