feat: use fastify as server #2

Merged
ayo merged 10 commits from fastify-rewrite into main 2026-06-04 11:03:46 +00:00
4 changed files with 23 additions and 6 deletions
Showing only changes of commit b285e0a73b - Show all commits

5
demo/src/api/index.js Normal file
View file

@ -0,0 +1,5 @@
export default async (fastify) => {
fastify.get('/', async function (request, reply) {
return 'This is the API Index'
})
}

View file

@ -16,7 +16,7 @@ export default [
eslintPluginPrettierRecommended,
includeIgnoreFile(gitignorePath),
{
ignores: ['site/*', 'templates/*', '**/public/*'],
ignores: ['site/*', 'templates/*', '**/public/*', 'demo/*'],
},
{
rules: {

View file

@ -8,6 +8,7 @@ import { resolve } from 'pathe'
// import { fileURLToPath } from 'node:url'
// import { dirname } from 'pathe'
import { getMcFlyConfig } from '../get-config.js'
import type { McFlyConfig } from '../../../config/dist/define-config.js'
// const __filename = fileURLToPath(import.meta.url)
// const __dirname = dirname(__filename)
@ -37,7 +38,13 @@ async function serve(args: ParsedArgs) {
* - autoLoad config
* - fastify config
*/
mcflyConfig.server.serve({ rootDir: rootDir + '/src', apiDir: '/api' })
if (mcflyConfig.server)
mcflyConfig.server.serve({
rootDir: rootDir + '/src',
apiDir: '/api',
logger: consola,
})
else consola.error('[McFly]: `server` configuration required')
} catch (e) {
consola.error(e)
}

View file

@ -2,8 +2,9 @@ import Fastify from 'fastify'
import AutoLoad from '@fastify/autoload'
import path from 'node:path'
export default ({ rootDir, apiDir }) => {
const server = Fastify({ logger: true })
export default ({ rootDir, apiDir, logger, port }) => {
const server = Fastify()
const portNumber = port ?? 3000
server.register(AutoLoad, {
dir: path.join(rootDir, apiDir),
@ -12,6 +13,10 @@ export default ({ rootDir, apiDir }) => {
},
})
console.log('[INFO]: Watching for file changes in', rootDir)
server.listen({ port: 3000 })
server
.listen({ port: portNumber })
.then(() => {
logger.log(`API now serving at http://localhost:${portNumber}${apiDir}`)
})
.catch((err) => logger.error(err))
}