@platformatic/runtime 3.0.0-alpha.4 → 3.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,15 @@
1
- 'use strict'
2
-
3
- const { platform, tmpdir } = require('node:os')
4
- const { join } = require('node:path')
5
- const { createDirectory, safeRemove } = require('@platformatic/foundation')
6
-
7
- const fastify = require('fastify')
8
- const ws = require('ws')
1
+ import fastifyAccepts from '@fastify/accepts'
2
+ import fastifyWebsocket from '@fastify/websocket'
3
+ import { createDirectory, safeRemove } from '@platformatic/foundation'
4
+ import fastify from 'fastify'
5
+ import { platform, tmpdir } from 'node:os'
6
+ import { join } from 'node:path'
7
+ import { createWebSocketStream } from 'ws'
9
8
 
10
9
  const PLATFORMATIC_TMP_DIR = join(tmpdir(), 'platformatic', 'runtimes')
11
10
 
12
- async function managementApiPlugin (app, opts) {
13
- app.register(require('@fastify/accepts'))
11
+ export async function managementApiPlugin (app, opts) {
12
+ app.register(fastifyAccepts)
14
13
 
15
14
  const runtime = opts.runtime
16
15
 
@@ -32,62 +31,62 @@ async function managementApiPlugin (app, opts) {
32
31
  })
33
32
 
34
33
  app.post('/stop', async () => {
35
- app.log.debug('stop services')
34
+ app.log.debug('stop applications')
36
35
  await runtime.close()
37
36
  })
38
37
 
39
38
  app.post('/restart', async () => {
40
- app.log.debug('restart services')
39
+ app.log.debug('restart applications')
41
40
  await runtime.restart()
42
41
  })
43
42
 
44
- app.get('/services', async () => {
45
- return runtime.getServices()
43
+ app.get('/applications', async () => {
44
+ return runtime.getApplications()
46
45
  })
47
46
 
48
- app.get('/services/:id', async request => {
47
+ app.get('/applications/:id', async request => {
49
48
  const { id } = request.params
50
- app.log.debug('get service details', { id })
51
- return runtime.getServiceDetails(id)
49
+ app.log.debug('get application details', { id })
50
+ return runtime.getApplicationDetails(id)
52
51
  })
53
52
 
54
- app.get('/services/:id/config', async request => {
53
+ app.get('/applications/:id/config', async request => {
55
54
  const { id } = request.params
56
- app.log.debug('get service config', { id })
57
- return runtime.getServiceConfig(id)
55
+ app.log.debug('get application config', { id })
56
+ return runtime.getApplicationConfig(id)
58
57
  })
59
58
 
60
- app.get('/services/:id/env', async request => {
59
+ app.get('/applications/:id/env', async request => {
61
60
  const { id } = request.params
62
- app.log.debug('get service config', { id })
63
- return runtime.getServiceEnv(id)
61
+ app.log.debug('get application config', { id })
62
+ return runtime.getApplicationEnv(id)
64
63
  })
65
64
 
66
- app.get('/services/:id/openapi-schema', async request => {
65
+ app.get('/applications/:id/openapi-schema', async request => {
67
66
  const { id } = request.params
68
67
  app.log.debug('get openapi-schema', { id })
69
- return runtime.getServiceOpenapiSchema(id)
68
+ return runtime.getApplicationOpenapiSchema(id)
70
69
  })
71
70
 
72
- app.get('/services/:id/graphql-schema', async request => {
71
+ app.get('/applications/:id/graphql-schema', async request => {
73
72
  const { id } = request.params
74
73
  app.log.debug('get graphql-schema', { id })
75
- return runtime.getServiceGraphqlSchema(id)
74
+ return runtime.getApplicationGraphqlSchema(id)
76
75
  })
77
76
 
78
- app.post('/services/:id/start', async request => {
77
+ app.post('/applications/:id/start', async request => {
79
78
  const { id } = request.params
80
- app.log.debug('start service', { id })
81
- await runtime.startService(id)
79
+ app.log.debug('start application', { id })
80
+ await runtime.startApplication(id)
82
81
  })
83
82
 
84
- app.post('/services/:id/stop', async request => {
83
+ app.post('/applications/:id/stop', async request => {
85
84
  const { id } = request.params
86
- app.log.debug('stop service', { id })
87
- await runtime.stopService(id)
85
+ app.log.debug('stop application', { id })
86
+ await runtime.stopApplication(id)
88
87
  })
89
88
 
90
- app.all('/services/:id/proxy/*', async (request, reply) => {
89
+ app.all('/applications/:id/proxy/*', async (request, reply) => {
91
90
  const { id, '*': requestUrl } = request.params
92
91
  app.log.debug('proxy request', { id, requestUrl })
93
92
 
@@ -149,11 +148,11 @@ async function managementApiPlugin (app, opts) {
149
148
  })
150
149
 
151
150
  app.get('/logs/live', { websocket: true }, async (socket, req) => {
152
- runtime.addLoggerDestination(ws.createWebSocketStream(socket))
151
+ runtime.addLoggerDestination(createWebSocketStream(socket))
153
152
  })
154
153
  }
155
154
 
156
- async function startManagementApi (runtime, root) {
155
+ export async function startManagementApi (runtime, root) {
157
156
  const runtimePID = process.pid
158
157
 
159
158
  try {
@@ -170,7 +169,7 @@ async function startManagementApi (runtime, root) {
170
169
  }
171
170
 
172
171
  const managementApi = fastify()
173
- managementApi.register(require('@fastify/websocket'))
172
+ managementApi.register(fastifyWebsocket)
174
173
  managementApi.register(managementApiPlugin, { runtime, prefix: '/api/v1' })
175
174
 
176
175
  managementApi.addHook('onClose', async () => {
@@ -190,5 +189,3 @@ async function startManagementApi (runtime, root) {
190
189
  process.exit(1)
191
190
  }
192
191
  }
193
-
194
- module.exports = { startManagementApi, managementApiPlugin }
@@ -1,6 +1,6 @@
1
- 'use strict'
2
-
3
- const fastify = require('fastify')
1
+ import fastifyAccepts from '@fastify/accepts'
2
+ import fastifyBasicAuth from '@fastify/basic-auth'
3
+ import fastify from 'fastify'
4
4
 
5
5
  const DEFAULT_HOSTNAME = '0.0.0.0'
6
6
  const DEFAULT_PORT = 9090
@@ -67,7 +67,7 @@ async function checkLiveness (runtime) {
67
67
  return { response, status }
68
68
  }
69
69
 
70
- async function startPrometheusServer (runtime, opts) {
70
+ export async function startPrometheusServer (runtime, opts) {
71
71
  if (opts.enabled === false) {
72
72
  return
73
73
  }
@@ -77,19 +77,19 @@ async function startPrometheusServer (runtime, opts) {
77
77
  const auth = opts.auth ?? null
78
78
 
79
79
  const promServer = fastify({ name: 'Prometheus server' })
80
- promServer.register(require('@fastify/accepts'))
80
+ promServer.register(fastifyAccepts)
81
81
 
82
82
  let onRequestHook
83
83
  if (auth) {
84
84
  const { username, password } = auth
85
85
 
86
- await promServer.register(require('@fastify/basic-auth'), {
86
+ await promServer.register(fastifyBasicAuth, {
87
87
  validate: function (user, pass, req, reply, done) {
88
88
  if (username !== user || password !== pass) {
89
89
  return reply.code(401).send({ message: 'Unauthorized' })
90
90
  }
91
91
  return done()
92
- },
92
+ }
93
93
  })
94
94
  onRequestHook = promServer.basicAuth
95
95
  }
@@ -129,7 +129,7 @@ async function startPrometheusServer (runtime, opts) {
129
129
  reply.type('text/plain')
130
130
  }
131
131
  return (await runtime.getMetrics(reqType)).metrics
132
- },
132
+ }
133
133
  })
134
134
 
135
135
  if (opts.readiness !== false) {
@@ -167,7 +167,7 @@ async function startPrometheusServer (runtime, opts) {
167
167
  reply.status(failStatusCode).send(failBody)
168
168
  }
169
169
  }
170
- },
170
+ }
171
171
  })
172
172
  }
173
173
 
@@ -206,14 +206,10 @@ async function startPrometheusServer (runtime, opts) {
206
206
  reply.status(failStatusCode).send(readiness?.body || failBody)
207
207
  }
208
208
  }
209
- },
209
+ }
210
210
  })
211
211
  }
212
212
 
213
213
  await promServer.listen({ port, host })
214
214
  return promServer
215
215
  }
216
-
217
- module.exports = {
218
- startPrometheusServer,
219
- }