@nitra/fastify 1.0.0
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/README.md +1 -0
- package/package.json +25 -0
- package/src/index.js +35 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @nitra/fastify
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nitra/fastify",
|
|
3
|
+
"description": "Fastify helper",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"nitra",
|
|
12
|
+
"fastify"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/nitra/fastify.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": "https://github.com/nitra/fastify/issues",
|
|
19
|
+
"homepage": "https://github.com/nitra/fastify",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@nitra/isenv": "^2.0.1",
|
|
23
|
+
"fastify": "^4.9.2"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fastify from 'fastify'
|
|
2
|
+
import { isDev } from '@nitra/isenv'
|
|
3
|
+
|
|
4
|
+
const port = Number(process.env.PORT) || 8080
|
|
5
|
+
|
|
6
|
+
export const app = fastify({
|
|
7
|
+
logger: isDev,
|
|
8
|
+
http2: !!process.env.K_SERVICE // Запускаем в http2 если находимся в контейнере
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
// Обрабатываем только OPTIONS
|
|
12
|
+
app.options('/*', async (req, reply) => {
|
|
13
|
+
// Ручний cors, бо WildcardOrigin Not Allowed
|
|
14
|
+
setHeaders(req, reply)
|
|
15
|
+
|
|
16
|
+
reply.header('Access-Control-Max-Age', 86400)
|
|
17
|
+
reply.header('Cache-Control', 'public, max-age=86400')
|
|
18
|
+
reply.header('Vary', 'origin')
|
|
19
|
+
|
|
20
|
+
reply.code(200).send()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
// Запускаем сервер
|
|
24
|
+
app.listen({ port, host: '0.0.0.0' })
|
|
25
|
+
|
|
26
|
+
function setHeaders(req, reply) {
|
|
27
|
+
let host = req.headers.origin || req.headers.referer
|
|
28
|
+
if (!host) {
|
|
29
|
+
host = 'localhost'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
reply.header('Access-Control-Allow-Origin', host)
|
|
33
|
+
reply.header('Access-Control-Allow-Credentials', 'true')
|
|
34
|
+
reply.header('Access-Control-Allow-Headers', 'Content-Type')
|
|
35
|
+
}
|