@nitra/fastify 1.4.3 → 1.5.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/package.json +1 -1
- package/src/index.js +12 -6
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import fastify from 'fastify'
|
|
2
|
-
// import { isDev } from '@nitra/isenv'
|
|
3
2
|
import fastifySensible from '@fastify/sensible'
|
|
4
3
|
import getLogger from '@nitra/bunyan/trace'
|
|
4
|
+
import { exit, env } from 'node:process'
|
|
5
5
|
|
|
6
|
-
const port = Number(
|
|
6
|
+
const port = Number(env.PORT) || 8080
|
|
7
7
|
|
|
8
8
|
export const app = fastify({
|
|
9
9
|
// logger: isDev,
|
|
10
|
-
http2: !!
|
|
10
|
+
http2: !!env.K_SERVICE // Запускаємо з http2 якщо в Cloud Run
|
|
11
11
|
})
|
|
12
12
|
|
|
13
13
|
app.register(fastifySensible) // для reply.badRequest(`Not found url: ${req.url} ...`)
|
|
@@ -35,12 +35,18 @@ app.addHook('preHandler', (req, reply, done) => {
|
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
// Запускаємо сервер
|
|
38
|
-
export
|
|
39
|
-
|
|
38
|
+
export function listen() {
|
|
39
|
+
app
|
|
40
|
+
.listen({ port, host: '0.0.0.0' })
|
|
41
|
+
.then(address => console.log(`🚀 Server ready at ${address}`))
|
|
42
|
+
.catch(err => {
|
|
43
|
+
console.error('Error starting server:', err)
|
|
44
|
+
exit(1)
|
|
45
|
+
})
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
function setHeaders(req, reply) {
|
|
43
|
-
let host =
|
|
49
|
+
let host = env.ORIGIN || req.headers.origin || req.headers.referer
|
|
44
50
|
if (!host) {
|
|
45
51
|
host = 'localhost'
|
|
46
52
|
}
|