@tramvai/module-server 1.39.0 → 1.44.4
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 +60 -58
- package/lib/server.es.js +1 -1
- package/lib/server.js +1 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# Module Server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Core `tramvai` module, responsible for processing the users requests.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
You need to install `@tramvai/module-server`
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm i --save @tramvai/module-server
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
And connect to the project
|
|
14
14
|
|
|
15
15
|
```tsx
|
|
16
16
|
import { createApp } from '@tramvai/core';
|
|
@@ -24,28 +24,28 @@ createApp({
|
|
|
24
24
|
|
|
25
25
|
## Explanation
|
|
26
26
|
|
|
27
|
-
###
|
|
27
|
+
### Processing the users requests
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
`ServerModule` creates [express.js](https://expressjs.com/) application, handles user requests, runs [commandLineRunner](concepts/command-line-runner.md), and sends responses to users with data, headers and status from `RESPONSE_MANAGER_TOKEN` token.
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### Request proxying
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
`ServerModule` allows you to configure the proxying of urls to the application using the library [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware)
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
To enable proxying, create a file `proxy.conf.js` or `proxy.conf.json` in the root of the project to export the request mapping object, or you can use the `PROXY_CONFIG_TOKEN` token.
|
|
36
36
|
|
|
37
|
-
####
|
|
37
|
+
#### Proxy config format
|
|
38
38
|
|
|
39
|
-
#####
|
|
39
|
+
##### Key-value object
|
|
40
40
|
|
|
41
41
|
```javascript
|
|
42
42
|
const testStand = 'https://example.org';
|
|
43
43
|
|
|
44
44
|
module.exports = {
|
|
45
|
-
//
|
|
46
|
-
//
|
|
45
|
+
// The key is the path pattern for the `express` to be passed to `app.use`
|
|
46
|
+
// value can be a string, in order to proxy all urls starting with `/login/`
|
|
47
47
|
'/login/': testStand,
|
|
48
|
-
//
|
|
48
|
+
// or can be a config object for [http-proxy](https://github.com/chimurai/http-proxy-middleware#http-proxy-options)
|
|
49
49
|
'/test/': {
|
|
50
50
|
target: testStand,
|
|
51
51
|
auth: true,
|
|
@@ -55,19 +55,19 @@ module.exports = {
|
|
|
55
55
|
};
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
#####
|
|
58
|
+
##### Object with context and target properties
|
|
59
59
|
|
|
60
60
|
```javascript
|
|
61
61
|
module.exports = {
|
|
62
|
-
// context -
|
|
62
|
+
// context - is similar to the option for [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware#context-matching)
|
|
63
63
|
context: ['/login/', '/registration/', '/auth/papi/'],
|
|
64
64
|
target: 'https://example.org',
|
|
65
|
-
//
|
|
65
|
+
// other `http-proxy-middleware` options
|
|
66
66
|
changeOrigin: true,
|
|
67
67
|
};
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
#####
|
|
70
|
+
##### Array with context and target properties
|
|
71
71
|
|
|
72
72
|
```json
|
|
73
73
|
[
|
|
@@ -78,7 +78,7 @@ module.exports = {
|
|
|
78
78
|
]
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
#####
|
|
81
|
+
##### Implementation of the PROXY_CONFIG_TOKEN token
|
|
82
82
|
|
|
83
83
|
```tsx
|
|
84
84
|
import { Scope, provide } from '@tramvai/core';
|
|
@@ -97,30 +97,30 @@ import { PROXY_CONFIG_TOKEN } from '@tramvai/tokens-server';
|
|
|
97
97
|
];
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
-
###
|
|
100
|
+
### Serving static files
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
The `ServerModule` has a built-in static server that allows you to distribute static files to users.
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
To serve files, you need to create a directory `public` in the root of the project in which to place the necessary files.
|
|
105
|
+
After that, all files will be available for request by browsers.
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
Эта функция доступна так-же и на продакшене. Для этого необходимо в докер контейнер скопировать папку `public`
|
|
107
|
+
_For example, we want to distribute sw.js file from the project's root:_ for this we create a folder `public` in which we put the file `sw.js`. Now on the client side, we will be able to request data from the url http://localhost:3000/sw.js. Also, we will most likely need some modifications on the CI/CD side to copy the public folder to the stands.
|
|
109
108
|
|
|
109
|
+
This function is also available in production. For this purpose, copy the folder `public` into the docker container
|
|
110
110
|
### PAPI
|
|
111
111
|
|
|
112
|
-
Papi - API
|
|
112
|
+
Papi - API routes for the `tramvai` application. More information is available in [Papi](features/papi/introduction.md)
|
|
113
113
|
|
|
114
|
-
###
|
|
114
|
+
### Emulation of network/backends problems in the application
|
|
115
115
|
|
|
116
|
-
(
|
|
116
|
+
(functionality is only available in dev mode)
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
The server has the ability to increase the response time of all requests.
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
To do this you must:
|
|
121
121
|
|
|
122
|
-
-
|
|
123
|
-
-
|
|
122
|
+
- start the application
|
|
123
|
+
- send a POST request to `/private/papi/debug-http-request` with a delay for the request:
|
|
124
124
|
|
|
125
125
|
```shell script
|
|
126
126
|
curl --location --request POST 'http://localhost:3000/tincoin/private/papi/debug-http-request' \
|
|
@@ -128,52 +128,54 @@ curl --location --request POST 'http://localhost:3000/tincoin/private/papi/debug
|
|
|
128
128
|
--data-urlencode 'delay=2000'
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
-
|
|
132
|
-
-
|
|
131
|
+
- check if the application works. Note: after each restart of the server the settings are reset, so after each rebuild it is necessary to access papi again.
|
|
132
|
+
- you can disable the timeout by accessing the same papi using the DELETE method
|
|
133
133
|
|
|
134
134
|
```shell script
|
|
135
135
|
curl --location --request DELETE 'http://localhost:3000/tincoin/private/papi/debug-http-request'
|
|
136
136
|
```
|
|
137
137
|
|
|
138
|
-
###
|
|
138
|
+
### Logging requests sent to the server
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
In dev mode, all requests sent through the standard `http` and `https` libraries for nodejs are logged under a special `server:node-debug:request` key. This allows you to see all requests that have been sent to the server, even if no logging has been defined for the requests explicitly.
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
To enable such logging, simply add the `server:node-debug:request` key to the `LOG_ENABLE` environment variable
|
|
143
143
|
|
|
144
144
|
### Health checks
|
|
145
145
|
|
|
146
|
-
- _`/healthz`_ -
|
|
147
|
-
- _`/readyz`_ -
|
|
146
|
+
- _`/healthz`_ - always replies `OK` after starting the application
|
|
147
|
+
- _`/readyz`_ - always replies `OK` after starting the application
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
Metrics
|
|
150
150
|
|
|
151
|
-
|
|
151
|
+
The metrics module is automatically connected into the server module.
|
|
152
|
+
For more information on metrics, you can read [in the metrics documentation](references/modules/metrics.md)
|
|
152
153
|
|
|
153
|
-
###
|
|
154
|
+
### Warming application caches
|
|
154
155
|
|
|
155
|
-
|
|
156
|
+
The cache-warmup module is automatically plugged into the server module.
|
|
157
|
+
Detailed information on cache warmup can be found [in cache-warmup documentation](references/modules/cache-warmup.md)
|
|
156
158
|
|
|
157
|
-
###
|
|
159
|
+
### Custom headers
|
|
158
160
|
|
|
159
|
-
####
|
|
161
|
+
#### Building and Deployment Information
|
|
160
162
|
|
|
161
|
-
|
|
163
|
+
There are special headers in the module, which help to determine the exact information about the version of the built application, commit, branch, etc:
|
|
162
164
|
|
|
163
|
-
- _x-app-id_ -
|
|
164
|
-
- _x-host_ -
|
|
165
|
-
- _x-app-version_ -
|
|
166
|
-
- _x-deploy-branch_ -
|
|
167
|
-
- _x-deploy-commit_ - sha
|
|
168
|
-
- _x-deploy-version_ -
|
|
169
|
-
- _x-deploy-repository_ -
|
|
165
|
+
- _x-app-id_ - The name of the application specified in `createApp`. Specified in the application code.
|
|
166
|
+
- _x-host_ - Hostname of the server where the current application is running. Computed in runtime.
|
|
167
|
+
- _x-app-version_ - version of the running application. Transmitted through the environment variable `APP_VERSION`.
|
|
168
|
+
- _x-deploy-branch_ - branch from which the current application image was built. Passed through environment variable `DEPLOY_BRANCH`.
|
|
169
|
+
- _x-deploy-commit_ - sha commit from which current application image was built. Passed through environment variable `DEPLOY_COMMIT`.
|
|
170
|
+
- _x-deploy-version_ - deploy revision number in k8s. Passed through environment variable `DEPLOY_VERSION`.
|
|
171
|
+
- _x-deploy-repository_ - application repository link. Passed through environment variable `DEPLOY_REPOSITORY`.
|
|
170
172
|
|
|
171
|
-
|
|
173
|
+
For all of the headers above which are passed via environment variables to be available, you need the external infrastructure to pass them when building and deprovisioning the application image (inside tinkoff this is done automatically).
|
|
172
174
|
|
|
173
|
-
##
|
|
175
|
+
## Debugging
|
|
174
176
|
|
|
175
|
-
|
|
177
|
+
Module uses loggers with identifiers: `server`, `server:static`, `server:webapp`, `server:node-debug:request`
|
|
176
178
|
|
|
177
|
-
##
|
|
179
|
+
## Exportable tokens
|
|
178
180
|
|
|
179
|
-
[
|
|
181
|
+
[Link](references/tokens/server-tokens.md)
|
package/lib/server.es.js
CHANGED
|
@@ -93,7 +93,7 @@ const webAppInitCommand = ({ app, logger, commandLineRunner, beforeInit, init, a
|
|
|
93
93
|
return async function webAppInit() {
|
|
94
94
|
await runHandlers(beforeInit);
|
|
95
95
|
app.use(bodyParser.urlencoded({
|
|
96
|
-
limit: '
|
|
96
|
+
limit: '2mb',
|
|
97
97
|
extended: false,
|
|
98
98
|
}), cookieParser());
|
|
99
99
|
await runHandlers(init);
|
package/lib/server.js
CHANGED
|
@@ -115,7 +115,7 @@ const webAppInitCommand = ({ app, logger, commandLineRunner, beforeInit, init, a
|
|
|
115
115
|
return async function webAppInit() {
|
|
116
116
|
await runHandlers(beforeInit);
|
|
117
117
|
app.use(bodyParser__default["default"].urlencoded({
|
|
118
|
-
limit: '
|
|
118
|
+
limit: '2mb',
|
|
119
119
|
extended: false,
|
|
120
120
|
}), cookieParser__default["default"]());
|
|
121
121
|
await runHandlers(init);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.44.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"@tinkoff/express-terminus": "0.1.12",
|
|
24
24
|
"@tinkoff/monkeypatch": "1.3.3",
|
|
25
25
|
"@tinkoff/url": "0.7.37",
|
|
26
|
-
"@tramvai/module-cache-warmup": "1.
|
|
27
|
-
"@tramvai/module-metrics": "1.
|
|
28
|
-
"@tramvai/papi": "1.
|
|
29
|
-
"@tramvai/tokens-server": "1.
|
|
26
|
+
"@tramvai/module-cache-warmup": "1.44.4",
|
|
27
|
+
"@tramvai/module-metrics": "1.44.4",
|
|
28
|
+
"@tramvai/papi": "1.44.4",
|
|
29
|
+
"@tramvai/tokens-server": "1.44.4",
|
|
30
30
|
"body-parser": "^1.19.0",
|
|
31
31
|
"compression": "^1.7.4",
|
|
32
32
|
"cookie-parser": "^1.4.3",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"@tinkoff/utils": "^2.1.2",
|
|
39
|
-
"@tramvai/cli": "1.
|
|
40
|
-
"@tramvai/core": "1.
|
|
41
|
-
"@tramvai/module-common": "1.
|
|
42
|
-
"@tramvai/module-environment": "1.
|
|
43
|
-
"@tramvai/tokens-common": "1.
|
|
39
|
+
"@tramvai/cli": "1.44.4",
|
|
40
|
+
"@tramvai/core": "1.44.4",
|
|
41
|
+
"@tramvai/module-common": "1.44.4",
|
|
42
|
+
"@tramvai/module-environment": "1.44.4",
|
|
43
|
+
"@tramvai/tokens-common": "1.44.4",
|
|
44
44
|
"@tinkoff/dippy": "0.7.35",
|
|
45
45
|
"tslib": "^2.0.3"
|
|
46
46
|
},
|