diff --git a/api/package.json b/api/package.json index 284cb77..cfe3d34 100644 --- a/api/package.json +++ b/api/package.json @@ -5,6 +5,10 @@ "dependencies": { "@redwoodjs/api": "5.2.3", "@redwoodjs/auth-dbauth-api": "5.2.3", - "@redwoodjs/graphql-server": "5.2.3" + "@redwoodjs/fastify": "5.2.3", + "@redwoodjs/graphql-server": "5.2.3", + "@redwoodjs/project-config": "5.2.3", + "chalk": "4.1.2", + "fastify": "4.17.0" } } diff --git a/api/src/server.ts b/api/src/server.ts new file mode 100644 index 0000000..4a9680a --- /dev/null +++ b/api/src/server.ts @@ -0,0 +1,66 @@ +import path from 'path' + +import chalk from 'chalk' +import { config } from 'dotenv-defaults' +import Fastify from 'fastify' + +import { + coerceRootPath, + redwoodFastifyWeb, + redwoodFastifyAPI, + DEFAULT_REDWOOD_FASTIFY_CONFIG, +} from '@redwoodjs/fastify' +import { getPaths, getConfig } from '@redwoodjs/project-config' + +async function serve() { + // Load .env files + const redwoodProjectPaths = getPaths() + + config({ + path: path.join(redwoodProjectPaths.base, '.env'), + defaults: path.join(redwoodProjectPaths.base, '.env.defaults'), + multiline: true, + }) + + const tsServer = Date.now() + console.log(chalk.italic.dim('Starting API and Web Servers...')) + + // Configure Fastify + const fastify = Fastify({ + ...DEFAULT_REDWOOD_FASTIFY_CONFIG, + }) + + const redwoodConfig = getConfig() + + const apiRootPath = coerceRootPath(redwoodConfig.web.apiUrl) + const port = redwoodConfig.web.port + + await fastify.register(redwoodFastifyWeb) + + await fastify.register(redwoodFastifyAPI, { + redwood: { + apiRootPath, + }, + }) + + // Start + fastify.listen({ port }) + + fastify.ready(() => { + console.log(chalk.italic.dim('Took ' + (Date.now() - tsServer) + ' ms')) + const on = chalk.magenta(`http://localhost:${port}${apiRootPath}`) + const webServer = chalk.green(`http://localhost:${port}`) + const apiServer = chalk.magenta(`http://localhost:${port}`) + console.log(`Web server started on ${webServer}`) + console.log(`API serving from ${apiServer}`) + console.log(`API listening on ${on}`) + const graphqlEnd = chalk.magenta(`${apiRootPath}graphql`) + console.log(`GraphQL endpoint at ${graphqlEnd}`) + }) + + process.on('exit', () => { + fastify.close() + }) +} + +serve() diff --git a/redwood.toml b/redwood.toml index 4a78230..93191d1 100644 --- a/redwood.toml +++ b/redwood.toml @@ -16,3 +16,6 @@ open = true [notifications] versionUpdates = ["latest"] + +[experimental.serverFile] + enabled = true diff --git a/yarn.lock b/yarn.lock index e1aff20..62756ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7931,7 +7931,11 @@ __metadata: dependencies: "@redwoodjs/api": 5.2.3 "@redwoodjs/auth-dbauth-api": 5.2.3 + "@redwoodjs/fastify": 5.2.3 "@redwoodjs/graphql-server": 5.2.3 + "@redwoodjs/project-config": 5.2.3 + chalk: 4.1.2 + fastify: ^4.17.0 languageName: unknown linkType: soft @@ -12869,7 +12873,7 @@ __metadata: languageName: node linkType: hard -"fastify@npm:4.17.0": +"fastify@npm:4.17.0, fastify@npm:^4.17.0": version: 4.17.0 resolution: "fastify@npm:4.17.0" dependencies: