Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Spark APIi service to Fastify #Migrate our REST APIs to Fastify #549

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Goddhi
Copy link
Contributor

@Goddhi Goddhi commented Mar 16, 2025

This PR migrates the Spark API server from the existing Node.js's built-in HTTP module to Fastify framework.
Changes Made
Added Fastify as a dependency
Created a parallel implementation that maintains compatibility with existing API contracts
Reorganized route handlers

Files worked on
api/bin/spark-fastify.js - modified this file for Fastify server entry point
api/fastify-app.js - Fastify application setup with middleware and hooks
api/routes/index.js - introduced this file for Route definitions migrated from api/index.js,
api/package.json - Modified this file by adding Fastify dependency and new start script

@juliangruber
Copy link
Member

npm error npm ci can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with npm install before continuing.

@Goddhi CI is failing because package.json and package-lock.json aren't in sync. Could you please run npm install to fix that?

import { createHandler } from '../index.js'
import Fastify from 'fastify'
import { createFastifyApp } from '../fastify-app.js'
// import { createHandler } from '../index.js'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this code comment for?

Comment on lines +12 to +17
logger: {
level: 'info',
transport: {
target: 'pino-pretty'
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger: {
level: 'info',
transport: {
target: 'pino-pretty'
}
}
logger

To allow the caller of createFastifyApp to provide the logger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, noted

client,
// Initialize Fastify app
const fastifyApp = await createFastifyApp({
pgPool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

target: 'pino-pretty'
}
}
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})

// Add shared context to each request
fastify.decorateRequest('pgPool', null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we switch to @fastify/postgres instead of manual handling instead please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on using @fastify/postgres

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pyropy I have have made some commits after your review last week on this PR stats-api-pr, can you kindly help review once again, so I can replicate same @fastify/postgres implementation to this API as well, thanks.

})

// Error handler
fastify.setErrorHandler((error, request, reply) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use fastify's default error handler instead? No need to keep the current implementation, which we only created, because we didn't have any default error handler

})

// Request logging
fastify.addHook('onRequest', (request, reply, done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be removed, as fastify is taking care of request logging now

@@ -9,6 +9,7 @@
"scripts": {
"start": "node bin/spark.js",
"lint": "standard",
"start:fastify": "node bin/spark-fastify.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"start:fastify": "node bin/spark-fastify.js",
"start:fastify": "node bin/spark-fastify.js",

That file doesn't exist

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

measurement.providerId
])

return { id: rows[0].id }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return { id: rows[0].id }
reply.send({ id: rows[0].id })

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the other routes have similar issues. Once CI is fixed, that should show.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to keep all routes in one file, then I prefer to call that file api/routes.js.

Copy link
Contributor

@pyropy pyropy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @Goddhi

Hey just wanted to check in and see how things are going. Is there something we could do to help you out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants