@platformatic/service 0.8.0 → 0.9.1
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.
- package/index.js +14 -11
- package/package.json +4 -4
- package/test/config.test.js +186 -0
package/index.js
CHANGED
|
@@ -132,7 +132,7 @@ async function buildServer (options, app = platformaticService) {
|
|
|
132
132
|
if (!options.configManager) {
|
|
133
133
|
// instantiate a new config manager from current options
|
|
134
134
|
const cm = new ConfigManager({
|
|
135
|
-
source:
|
|
135
|
+
source: options,
|
|
136
136
|
schema
|
|
137
137
|
})
|
|
138
138
|
await cm.parseAndValidate()
|
|
@@ -164,28 +164,31 @@ async function buildServer (options, app = platformaticService) {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
addLoggerToTheConfig(opts)
|
|
167
|
+
const configManager = handler.app.platformatic.configManager
|
|
168
|
+
|
|
169
|
+
if (!opts) {
|
|
170
|
+
opts = configManager.current
|
|
171
|
+
}
|
|
167
172
|
|
|
168
173
|
// Ignore because not tested on Windows
|
|
169
174
|
// TODO: remove the ignore, we shoduld be testing
|
|
170
175
|
// this on Windows
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
opts = createServerConfig(opts)
|
|
178
|
-
opts.app = app
|
|
179
|
-
}
|
|
176
|
+
const fileWatcher = handler.app.platformatic.fileWatcher
|
|
177
|
+
opts.fileWatcher = fileWatcher
|
|
178
|
+
opts.configManager = configManager
|
|
179
|
+
opts = createServerConfig(opts)
|
|
180
|
+
opts.app = app
|
|
181
|
+
|
|
180
182
|
debounce = _restart(opts).then(() => {
|
|
181
183
|
handler.app.log.info('restarted')
|
|
182
184
|
}).finally(() => {
|
|
183
185
|
debounce = null
|
|
184
186
|
})
|
|
185
|
-
/* c8 ignore stop */
|
|
186
187
|
return debounce
|
|
187
188
|
}
|
|
188
189
|
|
|
190
|
+
handler.app.restart = handler.restart
|
|
191
|
+
|
|
189
192
|
return handler
|
|
190
193
|
}
|
|
191
194
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/service",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
@@ -28,15 +28,15 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@fastify/accepts": "^4.0.1",
|
|
30
30
|
"@fastify/autoload": "^5.5.0",
|
|
31
|
-
"@fastify/basic-auth": "^
|
|
31
|
+
"@fastify/basic-auth": "^5.0.0",
|
|
32
32
|
"@fastify/cors": "^8.0.0",
|
|
33
33
|
"@fastify/deepmerge": "^1.1.0",
|
|
34
34
|
"@fastify/restartable": "^1.3.1",
|
|
35
35
|
"@fastify/static": "^6.5.0",
|
|
36
36
|
"@fastify/swagger": "^8.0.0",
|
|
37
37
|
"@fastify/under-pressure": "^8.0.0",
|
|
38
|
-
"@platformatic/config": "0.
|
|
39
|
-
"@platformatic/utils": "0.
|
|
38
|
+
"@platformatic/config": "0.9.1",
|
|
39
|
+
"@platformatic/utils": "0.9.1",
|
|
40
40
|
"close-with-grace": "^1.1.0",
|
|
41
41
|
"commist": "^3.1.2",
|
|
42
42
|
"desm": "^1.2.0",
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
require('./helper')
|
|
4
|
+
const { test } = require('tap')
|
|
5
|
+
const { buildServer } = require('..')
|
|
6
|
+
const { request } = require('undici')
|
|
7
|
+
const { join } = require('path')
|
|
8
|
+
const os = require('os')
|
|
9
|
+
const { writeFile } = require('fs/promises')
|
|
10
|
+
|
|
11
|
+
test('config reloads', async ({ teardown, equal, pass, same }) => {
|
|
12
|
+
const file = join(os.tmpdir(), `${process.pid}-1.js`)
|
|
13
|
+
|
|
14
|
+
await writeFile(file, `
|
|
15
|
+
module.exports = async function (app, options) {
|
|
16
|
+
app.get('/', () => options.message)
|
|
17
|
+
}`)
|
|
18
|
+
|
|
19
|
+
const server = await buildServer({
|
|
20
|
+
server: {
|
|
21
|
+
hostname: '127.0.0.1',
|
|
22
|
+
port: 0
|
|
23
|
+
},
|
|
24
|
+
plugin: {
|
|
25
|
+
path: file,
|
|
26
|
+
options: {
|
|
27
|
+
message: 'hello'
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
metrics: false
|
|
31
|
+
})
|
|
32
|
+
teardown(server.stop)
|
|
33
|
+
await server.listen()
|
|
34
|
+
|
|
35
|
+
{
|
|
36
|
+
const res = await request(`${server.url}/`)
|
|
37
|
+
equal(res.statusCode, 200, 'add status code')
|
|
38
|
+
same(await res.body.text(), 'hello', 'response')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await server.app.platformatic.configManager.update({
|
|
42
|
+
server: {
|
|
43
|
+
hostname: '127.0.0.1',
|
|
44
|
+
port: 0
|
|
45
|
+
},
|
|
46
|
+
plugin: {
|
|
47
|
+
path: file,
|
|
48
|
+
options: {
|
|
49
|
+
message: 'ciao mondo'
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
metrics: false
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
await server.restart()
|
|
56
|
+
|
|
57
|
+
{
|
|
58
|
+
const res = await request(`${server.url}/`)
|
|
59
|
+
equal(res.statusCode, 200, 'add status code')
|
|
60
|
+
same(await res.body.text(), 'ciao mondo', 'response')
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('config reloads from a written file', async ({ teardown, equal, pass, same }) => {
|
|
65
|
+
const config = join(os.tmpdir(), `${process.pid}-2.json`)
|
|
66
|
+
const file = join(os.tmpdir(), `${process.pid}-2.js`)
|
|
67
|
+
|
|
68
|
+
await writeFile(config, JSON.stringify({
|
|
69
|
+
server: {
|
|
70
|
+
hostname: '127.0.0.1',
|
|
71
|
+
port: 0
|
|
72
|
+
},
|
|
73
|
+
plugin: {
|
|
74
|
+
path: file,
|
|
75
|
+
options: {
|
|
76
|
+
message: 'hello'
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
metrics: false
|
|
80
|
+
}))
|
|
81
|
+
|
|
82
|
+
await writeFile(file, `
|
|
83
|
+
module.exports = async function (app, options) {
|
|
84
|
+
app.get('/', () => options.message)
|
|
85
|
+
}`)
|
|
86
|
+
|
|
87
|
+
const server = await buildServer(config)
|
|
88
|
+
teardown(server.stop)
|
|
89
|
+
await server.listen()
|
|
90
|
+
|
|
91
|
+
{
|
|
92
|
+
const res = await request(`${server.url}/`)
|
|
93
|
+
equal(res.statusCode, 200, 'add status code')
|
|
94
|
+
same(await res.body.text(), 'hello', 'response')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
await server.app.platformatic.configManager.update({
|
|
98
|
+
server: {
|
|
99
|
+
hostname: '127.0.0.1',
|
|
100
|
+
port: 0
|
|
101
|
+
},
|
|
102
|
+
plugin: {
|
|
103
|
+
path: file,
|
|
104
|
+
options: {
|
|
105
|
+
message: 'ciao mondo'
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
metrics: false
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
await server.restart()
|
|
112
|
+
|
|
113
|
+
{
|
|
114
|
+
const res = await request(`${server.url}/`)
|
|
115
|
+
equal(res.statusCode, 200, 'add status code')
|
|
116
|
+
same(await res.body.text(), 'ciao mondo', 'response')
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('config reloads from a written file from a route', async ({ teardown, equal, pass, same }) => {
|
|
121
|
+
const config = join(os.tmpdir(), `${process.pid}-3.json`)
|
|
122
|
+
const file = join(os.tmpdir(), `${process.pid}-3.js`)
|
|
123
|
+
|
|
124
|
+
await writeFile(config, JSON.stringify({
|
|
125
|
+
server: {
|
|
126
|
+
hostname: '127.0.0.1',
|
|
127
|
+
logger: { level: 'error' },
|
|
128
|
+
port: 0
|
|
129
|
+
},
|
|
130
|
+
plugin: {
|
|
131
|
+
path: file,
|
|
132
|
+
options: {
|
|
133
|
+
message: 'hello'
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
metrics: false
|
|
137
|
+
}))
|
|
138
|
+
|
|
139
|
+
await writeFile(file, `
|
|
140
|
+
module.exports = async function (app, options) {
|
|
141
|
+
app.get('/', () => options.message)
|
|
142
|
+
|
|
143
|
+
app.post('/restart', async (req, res) => {
|
|
144
|
+
await app.platformatic.configManager.update({
|
|
145
|
+
server: {
|
|
146
|
+
hostname: '127.0.0.1',
|
|
147
|
+
port: 0
|
|
148
|
+
},
|
|
149
|
+
plugin: {
|
|
150
|
+
path: '${file.replace(/\\/g, '\\\\')}',
|
|
151
|
+
options: {
|
|
152
|
+
message: 'ciao mondo'
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
metrics: false
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
await app.restart()
|
|
159
|
+
|
|
160
|
+
return true
|
|
161
|
+
})
|
|
162
|
+
}`)
|
|
163
|
+
|
|
164
|
+
const server = await buildServer(config)
|
|
165
|
+
teardown(server.stop)
|
|
166
|
+
await server.listen()
|
|
167
|
+
|
|
168
|
+
{
|
|
169
|
+
const res = await request(`${server.url}/`)
|
|
170
|
+
equal(res.statusCode, 200, 'add status code')
|
|
171
|
+
same(await res.body.text(), 'hello', 'response')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
{
|
|
175
|
+
const res = await request(`${server.url}/restart`, {
|
|
176
|
+
method: 'POST'
|
|
177
|
+
})
|
|
178
|
+
equal(res.statusCode, 200, 'add status code')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
{
|
|
182
|
+
const res = await request(`${server.url}/`)
|
|
183
|
+
equal(res.statusCode, 200, 'add status code')
|
|
184
|
+
same(await res.body.text(), 'ciao mondo', 'response')
|
|
185
|
+
}
|
|
186
|
+
})
|