├── README.md ├── platformatic ├── .env.sample ├── modules │ ├── catalogue │ │ ├── index.js │ │ └── routes │ │ │ └── products.js │ └── inventory │ │ ├── routes │ │ └── product.js │ │ └── index.js ├── .gitignore ├── platformatic.service.json ├── app.js └── README.md ├── runtime ├── services │ ├── inventory │ │ ├── .env.sample │ │ ├── platformatic.service.json │ │ ├── routes │ │ │ └── product.mjs │ │ └── README.md │ ├── composer │ │ ├── .env.sample │ │ ├── platformatic.composer.json │ │ └── README.md │ └── catalogue │ │ ├── inventory │ │ ├── package.json │ │ ├── inventory.cjs │ │ ├── inventory.openapi.json │ │ └── inventory.d.ts │ │ ├── .env.sample │ │ ├── routes │ │ └── products.mjs │ │ ├── platformatic.service.json │ │ └── README.md ├── package.json ├── platformatic.runtime.json ├── .gitignore └── README.md ├── modular ├── modules │ ├── catalogue │ │ ├── index.js │ │ └── routes │ │ │ └── products.js │ └── inventory │ │ ├── routes │ │ └── product.js │ │ └── index.js ├── app.js └── server.js ├── simple ├── plugins │ └── inventory.js ├── routes │ ├── catalogue │ │ └── products.js │ └── inventory │ │ └── product.js ├── server.js └── app.js ├── package.json ├── LICENSE └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # modular_monolith 2 | Example of the "Building a Modular Monolith with Fastify" talk 3 | -------------------------------------------------------------------------------- /platformatic/.env.sample: -------------------------------------------------------------------------------- 1 | PLT_SERVER_HOSTNAME=127.0.0.1 2 | PORT=3042 3 | PLT_SERVER_LOGGER_LEVEL=info 4 | -------------------------------------------------------------------------------- /runtime/services/inventory/.env.sample: -------------------------------------------------------------------------------- 1 | PLT_SERVER_HOSTNAME=127.0.0.1 2 | PORT=3002 3 | PLT_SERVER_LOGGER_LEVEL=info 4 | -------------------------------------------------------------------------------- /runtime/services/composer/.env.sample: -------------------------------------------------------------------------------- 1 | PLT_SERVER_HOSTNAME=127.0.0.1 2 | PORT=3000 3 | PLT_SERVER_LOGGER_LEVEL=info 4 | PLT_EXAMPLE_ORIGIN= 5 | -------------------------------------------------------------------------------- /runtime/services/catalogue/inventory/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inventory", 3 | "main": "./inventory.cjs", 4 | "types": "./inventory.d.ts" 5 | } -------------------------------------------------------------------------------- /runtime/services/catalogue/.env.sample: -------------------------------------------------------------------------------- 1 | PLT_SERVER_HOSTNAME=127.0.0.1 2 | PORT=3001 3 | PLT_SERVER_LOGGER_LEVEL=info 4 | 5 | PLT_INVENTORY_URL=http://127.0.0.1:3002/ 6 | -------------------------------------------------------------------------------- /runtime/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "start": "platformatic runtime start" 4 | }, 5 | "devDependencies": { 6 | "fastify": "^4.17.0" 7 | }, 8 | "dependencies": { 9 | "platformatic": "^0.26.1" 10 | }, 11 | "engines": { 12 | "node": "^18.8.0 || >=19" 13 | } 14 | } -------------------------------------------------------------------------------- /runtime/platformatic.runtime.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://platformatic.dev/schemas/v0.26.1/runtime", 3 | "entrypoint": "composer", 4 | "allowCycles": false, 5 | "hotReload": true, 6 | "autoload": { 7 | "path": "services", 8 | "exclude": [ 9 | "docs" 10 | ] 11 | } 12 | } -------------------------------------------------------------------------------- /modular/modules/catalogue/index.js: -------------------------------------------------------------------------------- 1 | import autoload from '@fastify/autoload' 2 | import { join } from 'desm' 3 | 4 | export default async function catalogue (app, opts) { 5 | app.register(autoload, { 6 | dir: join(import.meta.url, 'routes'), 7 | options: { 8 | prefix: opts.prefix 9 | } 10 | }) 11 | } 12 | -------------------------------------------------------------------------------- /platformatic/modules/catalogue/index.js: -------------------------------------------------------------------------------- 1 | import autoload from '@fastify/autoload' 2 | import { join } from 'desm' 3 | 4 | export default async function catalogue (app, opts) { 5 | app.register(autoload, { 6 | dir: join(import.meta.url, 'routes'), 7 | options: { 8 | prefix: opts.prefix 9 | } 10 | }) 11 | } 12 | -------------------------------------------------------------------------------- /simple/plugins/inventory.js: -------------------------------------------------------------------------------- 1 | import fp from 'fastify-plugin' 2 | 3 | class Inventory { 4 | async howManyInStore(sku) { 5 | if (sku === 42) { 6 | return 2 7 | } else { 8 | return 0 9 | } 10 | } 11 | } 12 | 13 | async function inventory (fastify) { 14 | fastify.decorate('inventory', new Inventory()) 15 | } 16 | 17 | export default fp(inventory) 18 | -------------------------------------------------------------------------------- /runtime/.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | .DS_Store 3 | 4 | # dotenv environment variable files 5 | .env 6 | 7 | # database files 8 | *.sqlite 9 | *.sqlite3 10 | 11 | # Logs 12 | logs 13 | *.log 14 | npm-debug.log* 15 | yarn-debug.log* 16 | yarn-error.log* 17 | lerna-debug.log* 18 | .pnpm-debug.log* 19 | 20 | # Dependency directories 21 | node_modules/ 22 | 23 | # ctags 24 | tags 25 | 26 | # clinicjs 27 | .clinic/ 28 | -------------------------------------------------------------------------------- /platformatic/.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | .DS_Store 3 | 4 | # dotenv environment variable files 5 | .env 6 | 7 | # database files 8 | *.sqlite 9 | *.sqlite3 10 | 11 | # Logs 12 | logs 13 | *.log 14 | npm-debug.log* 15 | yarn-debug.log* 16 | yarn-error.log* 17 | lerna-debug.log* 18 | .pnpm-debug.log* 19 | 20 | # Dependency directories 21 | node_modules/ 22 | 23 | # ctags 24 | tags 25 | 26 | # clinicjs 27 | .clinic/ 28 | -------------------------------------------------------------------------------- /platformatic/platformatic.service.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://platformatic.dev/schemas/v0.19.4/service", 3 | "server": { 4 | "hostname": "{PLT_SERVER_HOSTNAME}", 5 | "port": "{PORT}", 6 | "logger": { 7 | "level": "{PLT_SERVER_LOGGER_LEVEL}" 8 | } 9 | }, 10 | "service": { 11 | "openapi": true 12 | }, 13 | "plugins": { 14 | "paths": [ 15 | "./app.js" 16 | ] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /runtime/services/inventory/platformatic.service.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://platformatic.dev/schemas/v0.26.1/service", 3 | "server": { 4 | "hostname": "{PLT_SERVER_HOSTNAME}", 5 | "port": "{PORT}", 6 | "logger": { 7 | "level": "{PLT_SERVER_LOGGER_LEVEL}" 8 | } 9 | }, 10 | "service": { 11 | "openapi": true 12 | }, 13 | "plugins": { 14 | "paths": [ 15 | "./routes" 16 | ] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /simple/routes/catalogue/products.js: -------------------------------------------------------------------------------- 1 | export default async function (fastify, opts) { 2 | fastify.get('/products', async (request, reply) => { 3 | const data = [{ sku: 42, name: 'foo' }, { sku: 43, name: 'bar' }] 4 | // Currentlyn slow and inefficient, but ok for the demo 5 | for (const product of data) { 6 | product.inStore = await fastify.inventory.howManyInStore(product.sku) 7 | } 8 | return data 9 | }) 10 | } 11 | -------------------------------------------------------------------------------- /modular/modules/catalogue/routes/products.js: -------------------------------------------------------------------------------- 1 | export default async function (fastify, opts) { 2 | fastify.get('/products', async (request, reply) => { 3 | const data = [{ sku: 42, name: 'foo' }, { sku: 43, name: 'bar' }] 4 | // Currently slow and inefficient, but ok for the demo 5 | for (const product of data) { 6 | product.inStore = await fastify.inventory.howManyInStore(product.sku) 7 | } 8 | return data 9 | }) 10 | } 11 | -------------------------------------------------------------------------------- /platformatic/modules/catalogue/routes/products.js: -------------------------------------------------------------------------------- 1 | export default async function (fastify, opts) { 2 | fastify.get('/products', async (request, reply) => { 3 | const data = [{ sku: 42, name: 'foo' }, { sku: 43, name: 'bar' }] 4 | // Currentlyn slow and inefficient, but ok for the demo 5 | for (const product of data) { 6 | product.inStore = await fastify.inventory.howManyInStore(product.sku) 7 | } 8 | return data 9 | }) 10 | } 11 | -------------------------------------------------------------------------------- /simple/routes/inventory/product.js: -------------------------------------------------------------------------------- 1 | 2 | export default async function (fastify, opts) { 3 | fastify.get('/product/:sku', { 4 | schema: { 5 | params: { 6 | type: 'object', 7 | properties: { 8 | sku: { type: 'number' } 9 | } 10 | } 11 | } 12 | }, async (request, reply) => { 13 | const sku = request.params.sku 14 | return { sku, inStore: await fastify.inventory.howManyInStore(sku) } 15 | }) 16 | } 17 | -------------------------------------------------------------------------------- /modular/modules/inventory/routes/product.js: -------------------------------------------------------------------------------- 1 | 2 | export default async function (fastify, opts) { 3 | fastify.get('/product/:sku', { 4 | schema: { 5 | params: { 6 | type: 'object', 7 | properties: { 8 | sku: { type: 'number' } 9 | } 10 | } 11 | } 12 | }, async (request, reply) => { 13 | const sku = request.params.sku 14 | return { sku, inStore: await fastify.inventory.howManyInStore(sku) } 15 | }) 16 | } 17 | -------------------------------------------------------------------------------- /runtime/services/catalogue/routes/products.mjs: -------------------------------------------------------------------------------- 1 | export default async function (fastify, opts) { 2 | fastify.get('/products', async (request, reply) => { 3 | const data = [{ sku: 42, name: 'foo' }, { sku: 43, name: 'bar' }] 4 | // Currentlyn slow and inefficient, but ok for the demo 5 | for (const product of data) { 6 | product.inStore = (await fastify.inventory.getProductSku({ sku: product.sku })).inStore 7 | } 8 | return data 9 | }) 10 | } 11 | -------------------------------------------------------------------------------- /platformatic/modules/inventory/routes/product.js: -------------------------------------------------------------------------------- 1 | 2 | export default async function (fastify, opts) { 3 | fastify.get('/product/:sku', { 4 | schema: { 5 | params: { 6 | type: 'object', 7 | properties: { 8 | sku: { type: 'number' } 9 | } 10 | } 11 | } 12 | }, async (request, reply) => { 13 | const sku = request.params.sku 14 | return { sku, inStore: await fastify.inventory.howManyInStore(sku) } 15 | }) 16 | } 17 | -------------------------------------------------------------------------------- /runtime/services/catalogue/platformatic.service.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://platformatic.dev/schemas/v0.26.1/service", 3 | "server": { 4 | "hostname": "{PLT_SERVER_HOSTNAME}", 5 | "port": "{PORT}", 6 | "logger": { 7 | "level": "{PLT_SERVER_LOGGER_LEVEL}" 8 | } 9 | }, 10 | "service": { 11 | "openapi": true 12 | }, 13 | "plugins": { 14 | "paths": [ 15 | "./routes" 16 | ] 17 | }, 18 | "clients": [ 19 | { 20 | "path": "./inventory", 21 | "url": "{PLT_INVENTORY_URL}" 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /runtime/services/inventory/routes/product.mjs: -------------------------------------------------------------------------------- 1 | 2 | class Inventory { 3 | async howManyInStore(sku) { 4 | if (sku === 42) { 5 | return 2 6 | } else { 7 | return 0 8 | } 9 | } 10 | } 11 | 12 | export default async function (fastify, opts) { 13 | const inventory = new Inventory() 14 | 15 | fastify.get('/product/:sku', { 16 | schema: { 17 | params: { 18 | type: 'object', 19 | properties: { 20 | sku: { type: 'number' } 21 | } 22 | } 23 | } 24 | }, async (request, reply) => { 25 | const sku = request.params.sku 26 | return { sku, inStore: await inventory.howManyInStore(sku) } 27 | }) 28 | } 29 | -------------------------------------------------------------------------------- /runtime/services/composer/platformatic.composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://platformatic.dev/schemas/v0.26.1/composer", 3 | "server": { 4 | "hostname": "{PLT_SERVER_HOSTNAME}", 5 | "port": "{PORT}", 6 | "logger": { 7 | "level": "{PLT_SERVER_LOGGER_LEVEL}" 8 | } 9 | }, 10 | "composer": { 11 | "services": [ 12 | { 13 | "id": "catalogue", 14 | "openapi": { 15 | "url": "/documentation/json" 16 | } 17 | }, 18 | { 19 | "id": "inventory", 20 | "openapi": { 21 | "url": "/documentation/json" 22 | } 23 | } 24 | 25 | ], 26 | "refreshTimeout": 1000 27 | }, 28 | "watch": true 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "modular_monolith", 3 | "version": "1.0.0", 4 | "description": "Example of the \"Building a Modular Monolith with Fastify\" talk", 5 | "main": "index.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "Matteo Collina ", 12 | "license": "MIT", 13 | "dependencies": { 14 | "@fastify/autoload": "^5.7.1", 15 | "close-with-grace": "^1.2.0", 16 | "desm": "^1.3.0", 17 | "dotenv": "^16.0.3", 18 | "fastify": "^4.15.0", 19 | "fastify-plugin": "^4.5.0", 20 | "pino-pretty": "^10.0.0", 21 | "platformatic": "^0.19.4" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /runtime/services/catalogue/inventory/inventory.cjs: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const pltClient = require('@platformatic/client') 4 | const { join } = require('path') 5 | 6 | async function generateInventoryClientPlugin (app, opts) { 7 | app.register(pltClient, { 8 | type: 'openapi', 9 | name: 'inventory', 10 | path: join(__dirname, 'inventory.openapi.json'), 11 | url: opts.url, 12 | fullResponse: false 13 | }) 14 | } 15 | 16 | generateInventoryClientPlugin[Symbol.for('plugin-meta')] = { 17 | name: 'inventory OpenAPI Client' 18 | } 19 | generateInventoryClientPlugin[Symbol.for('skip-override')] = true 20 | 21 | module.exports = generateInventoryClientPlugin 22 | module.exports.default = generateInventoryClientPlugin 23 | -------------------------------------------------------------------------------- /runtime/README.md: -------------------------------------------------------------------------------- 1 | # Platformatic Runtime API 2 | 3 | This is a generated [Platformatic Runtime](https://oss.platformatic.dev/docs/reference/runtime/introduction) application. 4 | 5 | ## Requirements 6 | 7 | Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended). 8 | You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 9 | 10 | ## Setup 11 | 12 | 1. Install dependencies: 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ## Usage 19 | 20 | Run the API with: 21 | 22 | ```bash 23 | npm start 24 | ``` 25 | 26 | ## Adding a Service 27 | 28 | Adding a new service to this project is as simple as running `create-platformatic` again, like so: 29 | 30 | ``` 31 | npx create-platformatic 32 | ``` 33 | -------------------------------------------------------------------------------- /runtime/services/catalogue/inventory/inventory.openapi.json: -------------------------------------------------------------------------------- 1 | { 2 | "openapi": "3.0.3", 3 | "info": { 4 | "title": "Platformatic", 5 | "description": "This is a service built on top of Platformatic", 6 | "version": "1.0.0" 7 | }, 8 | "components": { 9 | "schemas": {} 10 | }, 11 | "paths": { 12 | "/product/{sku}": { 13 | "get": { 14 | "parameters": [ 15 | { 16 | "schema": { 17 | "type": "number" 18 | }, 19 | "in": "path", 20 | "name": "sku", 21 | "required": true 22 | } 23 | ], 24 | "responses": { 25 | "200": { 26 | "description": "Default Response" 27 | } 28 | } 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /runtime/services/composer/README.md: -------------------------------------------------------------------------------- 1 | # Platformatic Composer API 2 | 3 | This is a generated [Platformatic Composer](https://oss.platformatic.dev/docs/reference/composer/introduction) application. 4 | 5 | ## Requirements 6 | 7 | Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended). 8 | You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 9 | 10 | ## Setup 11 | 12 | 1. Install dependencies: 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ## Usage 19 | 20 | Run the API with: 21 | 22 | ```bash 23 | npm start 24 | ``` 25 | 26 | ### Explore 27 | - ⚡ The Platformatic Composer server is running at http://localhost:3042/ 28 | - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/ 29 | 30 | 31 | -------------------------------------------------------------------------------- /modular/modules/inventory/index.js: -------------------------------------------------------------------------------- 1 | import fp from 'fastify-plugin' 2 | import autoload from '@fastify/autoload' 3 | import { join } from 'desm' 4 | 5 | class Inventory { 6 | async howManyInStore(sku) { 7 | if (sku === 42) { 8 | return 2 9 | } else { 10 | return 0 11 | } 12 | } 13 | } 14 | 15 | async function inventory (fastify, opts) { 16 | // This will be published to the root fastify instance 17 | // it could also be extracted to a separate plugin 18 | fastify.decorate('inventory', new Inventory()) 19 | 20 | // These routes would be created in their own child instances 21 | fastify.register(autoload, { 22 | dir: join(import.meta.url, 'routes'), 23 | options: { 24 | prefix: opts.prefix 25 | } 26 | }) 27 | } 28 | 29 | export default fp(inventory) 30 | -------------------------------------------------------------------------------- /platformatic/modules/inventory/index.js: -------------------------------------------------------------------------------- 1 | import fp from 'fastify-plugin' 2 | import autoload from '@fastify/autoload' 3 | import { join } from 'desm' 4 | 5 | class Inventory { 6 | async howManyInStore(sku) { 7 | if (sku === 42) { 8 | return 2 9 | } else { 10 | return 0 11 | } 12 | } 13 | } 14 | 15 | async function inventory (fastify, opts) { 16 | // This will be published to the root fastify instance 17 | // it could also be extracted to a separate plugin 18 | fastify.decorate('inventory', new Inventory()) 19 | 20 | // These routes would be created in their own child instances 21 | fastify.register(autoload, { 22 | dir: join(import.meta.url, 'routes'), 23 | options: { 24 | prefix: opts.prefix 25 | } 26 | }) 27 | } 28 | 29 | export default fp(inventory) 30 | -------------------------------------------------------------------------------- /platformatic/app.js: -------------------------------------------------------------------------------- 1 | import fastify from 'fastify' 2 | import autoload from '@fastify/autoload' 3 | import { join } from 'desm' 4 | 5 | export default async function (app, opts) { 6 | app.register(autoload, { 7 | dir: join(import.meta.url, 'modules'), 8 | encapsulate: false, 9 | maxDepth: 1 10 | }) 11 | 12 | app.setErrorHandler(async (err, request, reply) => { 13 | if (err.validation) { 14 | reply.code(403) 15 | return err.message 16 | } 17 | request.log.error({ err }) 18 | reply.code(err.statusCode || 500) 19 | 20 | return "I'm sorry, there was an error processing your request." 21 | }) 22 | 23 | app.setNotFoundHandler(async (request, reply) => { 24 | reply.code(404) 25 | return "I'm sorry, I couldn't find what you were looking for." 26 | }) 27 | 28 | return app 29 | } 30 | -------------------------------------------------------------------------------- /runtime/services/catalogue/README.md: -------------------------------------------------------------------------------- 1 | # Platformatic Service API 2 | 3 | This is a generated [Platformatic DB](https://oss.platformatic.dev/docs/reference/service/introduction) application. 4 | 5 | ## Requirements 6 | 7 | Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended). 8 | You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 9 | 10 | ## Setup 11 | 12 | Install dependencies: 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ## Usage 19 | 20 | Run the API with: 21 | 22 | ```bash 23 | npm start 24 | ``` 25 | 26 | ### Explore 27 | - ⚡ The Platformatic DB server is running at http://localhost:3042/ 28 | - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/ 29 | - 🔍 Try out the GraphiQL web UI at http://localhost:3042/graphiql 30 | 31 | 32 | -------------------------------------------------------------------------------- /runtime/services/inventory/README.md: -------------------------------------------------------------------------------- 1 | # Platformatic Service API 2 | 3 | This is a generated [Platformatic DB](https://oss.platformatic.dev/docs/reference/service/introduction) application. 4 | 5 | ## Requirements 6 | 7 | Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended). 8 | You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 9 | 10 | ## Setup 11 | 12 | Install dependencies: 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ## Usage 19 | 20 | Run the API with: 21 | 22 | ```bash 23 | npm start 24 | ``` 25 | 26 | ### Explore 27 | - ⚡ The Platformatic DB server is running at http://localhost:3042/ 28 | - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/ 29 | - 🔍 Try out the GraphiQL web UI at http://localhost:3042/graphiql 30 | 31 | 32 | -------------------------------------------------------------------------------- /platformatic/README.md: -------------------------------------------------------------------------------- 1 | # Platformatic Service API 2 | 3 | This is a generated [Platformatic DB](https://oss.platformatic.dev/docs/reference/service/introduction) application. 4 | 5 | ## Requirements 6 | 7 | Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended). 8 | You'll need to have [Node.js](https://nodejs.org/) >= v16.17.0 or >= v18.8.0 9 | 10 | ## Setup 11 | 12 | Install dependencies: 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ## Usage 19 | 20 | Run the API with: 21 | 22 | ```bash 23 | npm start 24 | ``` 25 | 26 | ### Explore 27 | - ⚡ The Platformatic DB server is running at http://localhost:3042/ 28 | - 📔 View the REST API's Swagger documentation at http://localhost:3042/documentation/ 29 | - 🔍 Try out the GraphiQL web UI at http://localhost:3042/graphiql 30 | 31 | 32 | -------------------------------------------------------------------------------- /simple/server.js: -------------------------------------------------------------------------------- 1 | import { build } from './app.js' 2 | import closeWithGrace from 'close-with-grace' 3 | import dotenv from 'dotenv' 4 | 5 | dotenv.config() 6 | 7 | const opts = { 8 | logger: { 9 | level: 'info' 10 | } 11 | } 12 | 13 | // We want to use pino-pretty only if there is a human watching this, 14 | // otherwise we log as newline-delimited JSON. 15 | if (process.stdout.isTTY) { 16 | opts.logger.transport = { target: 'pino-pretty' } 17 | } 18 | 19 | const port = process.env.PORT || 3000 20 | const host = process.env.HOST || '127.0.0.1' 21 | 22 | const app = await build(opts) 23 | await app.listen({ port, host }) 24 | 25 | closeWithGrace(async ({ err }) => { 26 | if (err) { 27 | app.log.error({ err }, 'server closing due to error') 28 | } 29 | app.log.info('shutting down gracefully') 30 | await app.close() 31 | }) 32 | -------------------------------------------------------------------------------- /modular/app.js: -------------------------------------------------------------------------------- 1 | import fastify from 'fastify' 2 | import autoload from '@fastify/autoload' 3 | import { join } from 'desm' 4 | 5 | export async function build (opts = {}) { 6 | const app = fastify(opts) 7 | 8 | app.register(autoload, { 9 | dir: join(import.meta.url, 'modules'), 10 | encapsulate: false, 11 | maxDepth: 1 12 | }) 13 | 14 | app.setErrorHandler(async (err, request, reply) => { 15 | if (err.validation) { 16 | reply.code(403) 17 | return err.message 18 | } 19 | request.log.error({ err }) 20 | reply.code(err.statusCode || 500) 21 | 22 | return "I'm sorry, there was an error processing your request." 23 | }) 24 | 25 | app.setNotFoundHandler(async (request, reply) => { 26 | reply.code(404) 27 | return "I'm sorry, I couldn't find what you were looking for." 28 | }) 29 | 30 | return app 31 | } 32 | -------------------------------------------------------------------------------- /modular/server.js: -------------------------------------------------------------------------------- 1 | import { build } from './app.js' 2 | import closeWithGrace from 'close-with-grace' 3 | import dotenv from 'dotenv' 4 | 5 | dotenv.config() 6 | 7 | const opts = { 8 | logger: { 9 | level: 'info' 10 | } 11 | } 12 | 13 | // We want to use pino-pretty only if there is a human watching this, 14 | // otherwise we log as newline-delimited JSON. 15 | if (process.stdout.isTTY) { 16 | opts.logger.transport = { target: 'pino-pretty' } 17 | } 18 | 19 | const port = process.env.PORT || 3000 20 | const host = process.env.HOST || '127.0.0.1' 21 | 22 | const app = await build(opts) 23 | console.log(app.printRoutes()) 24 | await app.listen({ port, host }) 25 | 26 | closeWithGrace(async ({ err }) => { 27 | if (err) { 28 | app.log.error({ err }, 'server closing due to error') 29 | } 30 | app.log.info('shutting down gracefully') 31 | await app.close() 32 | }) 33 | -------------------------------------------------------------------------------- /simple/app.js: -------------------------------------------------------------------------------- 1 | import fastify from 'fastify' 2 | import autoload from '@fastify/autoload' 3 | import { join } from 'desm' 4 | 5 | export async function build (opts = {}) { 6 | const app = fastify(opts) 7 | 8 | app.register(autoload, { 9 | dir: join(import.meta.url, 'plugins') 10 | }) 11 | 12 | app.register(autoload, { 13 | dir: join(import.meta.url, 'routes') 14 | }) 15 | 16 | app.setErrorHandler(async (err, request, reply) => { 17 | if (err.validation) { 18 | reply.code(403) 19 | return err.message 20 | } 21 | request.log.error({ err }) 22 | reply.code(err.statusCode || 500) 23 | 24 | return "I'm sorry, there was an error processing your request." 25 | }) 26 | 27 | app.setNotFoundHandler(async (request, reply) => { 28 | reply.code(404) 29 | return "I'm sorry, I couldn't find what you were looking for." 30 | }) 31 | 32 | return app 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Matteo Collina 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /runtime/services/catalogue/inventory/inventory.d.ts: -------------------------------------------------------------------------------- 1 | import { FastifyPluginAsync } from 'fastify' 2 | 3 | interface GetProductSkuRequest { 4 | 'sku': number; 5 | } 6 | 7 | interface GetProductSkuResponseOK { 8 | } 9 | 10 | interface Inventory { 11 | getProductSku(req: GetProductSkuRequest): Promise; 12 | } 13 | 14 | type InventoryPlugin = FastifyPluginAsync> 15 | 16 | declare module 'fastify' { 17 | interface ConfigureInventory { 18 | async getHeaders(req: FastifyRequest, reply: FastifyReply): Promise>; 19 | } 20 | interface FastifyInstance { 21 | 'inventory': Inventory; 22 | configureInventory(opts: ConfigureInventory): unknown 23 | } 24 | 25 | interface FastifyRequest { 26 | 'inventory': Inventory; 27 | } 28 | } 29 | 30 | declare namespace inventory { 31 | export interface InventoryOptions { 32 | url: string 33 | } 34 | export const inventory: InventoryPlugin; 35 | export { inventory as default }; 36 | } 37 | 38 | declare function inventory(...params: Parameters): ReturnType; 39 | export = inventory; 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | --------------------------------------------------------------------------------