feat: expose mcfly logger to server framework; log api URL
This commit is contained in:
parent
7c0949c518
commit
b285e0a73b
4 changed files with 23 additions and 6 deletions
5
demo/src/api/index.js
Normal file
5
demo/src/api/index.js
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
export default async (fastify) => {
|
||||||
|
fastify.get('/', async function (request, reply) {
|
||||||
|
return 'This is the API Index'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ export default [
|
||||||
eslintPluginPrettierRecommended,
|
eslintPluginPrettierRecommended,
|
||||||
includeIgnoreFile(gitignorePath),
|
includeIgnoreFile(gitignorePath),
|
||||||
{
|
{
|
||||||
ignores: ['site/*', 'templates/*', '**/public/*'],
|
ignores: ['site/*', 'templates/*', '**/public/*', 'demo/*'],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
rules: {
|
rules: {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import { resolve } from 'pathe'
|
||||||
// import { fileURLToPath } from 'node:url'
|
// import { fileURLToPath } from 'node:url'
|
||||||
// import { dirname } from 'pathe'
|
// import { dirname } from 'pathe'
|
||||||
import { getMcFlyConfig } from '../get-config.js'
|
import { getMcFlyConfig } from '../get-config.js'
|
||||||
|
import type { McFlyConfig } from '../../../config/dist/define-config.js'
|
||||||
|
|
||||||
// const __filename = fileURLToPath(import.meta.url)
|
// const __filename = fileURLToPath(import.meta.url)
|
||||||
// const __dirname = dirname(__filename)
|
// const __dirname = dirname(__filename)
|
||||||
|
|
@ -37,7 +38,13 @@ async function serve(args: ParsedArgs) {
|
||||||
* - autoLoad config
|
* - autoLoad config
|
||||||
* - fastify 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) {
|
} catch (e) {
|
||||||
consola.error(e)
|
consola.error(e)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@ import Fastify from 'fastify'
|
||||||
import AutoLoad from '@fastify/autoload'
|
import AutoLoad from '@fastify/autoload'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
export default ({ rootDir, apiDir }) => {
|
export default ({ rootDir, apiDir, logger, port }) => {
|
||||||
const server = Fastify({ logger: true })
|
const server = Fastify()
|
||||||
|
const portNumber = port ?? 3000
|
||||||
|
|
||||||
server.register(AutoLoad, {
|
server.register(AutoLoad, {
|
||||||
dir: path.join(rootDir, apiDir),
|
dir: path.join(rootDir, apiDir),
|
||||||
|
|
@ -12,6 +13,10 @@ export default ({ rootDir, apiDir }) => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('[INFO]: Watching for file changes in', rootDir)
|
server
|
||||||
server.listen({ port: 3000 })
|
.listen({ port: portNumber })
|
||||||
|
.then(() => {
|
||||||
|
logger.log(`API now serving at http://localhost:${portNumber}${apiDir}`)
|
||||||
|
})
|
||||||
|
.catch((err) => logger.error(err))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue