Programmatic API
In many cases it's useful to start Platformatic Service using an API instead of command line, e.g. in tests we want to start and stop our server.
The buildServer
function allows that:
import { buildServer } from '@platformatic/service'
const app = await buildServer('path/to/platformatic.service.json')
server: {
hostname: '127.0.0.1',
port: 0
}
})
await app.start()
const res = await fetch(app.url)
console.log(await res.json())
// do something
await app.close()
It is also possible to customize the configuration:
import { buildServer } from '@platformatic/service'
const app = await buildServer({
server: {
hostname: '127.0.0.1',
port: 0
}
})
await app.start()
const res = await fetch(app.url)
console.log(await res.json())
// do something
await app.close()
Creating a reusable application on top of Platformatic Service
Platformatic DB is built on top of Platformatic Serivce. If you want to build a similar kind of tool, follow this example:
import { buildServer, ConfigManager, platformaticService } from '@platformatic/service'
class MyConfigManager {
_transformConfig () {
// Edit this.current at will, it's the current configuration
console.log(this.current)
}
}
async function myPlugin (app, opts) {
// app.platformatic.configManager contains an instance of the ConfigManager
console.log(app.platformatic.configManager.current)
await platformaticService(app, opts)
}
// break Fastify encapsulation
myPlugin[Symbol.for('skip-override')] = true
const service = await buildServer('path/to/config.json', myPlugin, MyConfigManager)
await service.start()
const res = await fetch(service.url)
console.log(await res.json())
// do something
await service.close()