Skip to content

Commit 86716cd

Browse files
authored
chore(status): migrate egg-status into monorepo (#6070)
`egg-status` lives in a separate repository and uses the old middleware registration API. This change moves it into `plugins/status` as `@eggjs/status` for maintenance with the other plugins. The package supports `egg@4` and uses ESM and the plugin factory API. It registers the middleware in `configWillLoad`, immediately after `meta`. The existing health-check paths, file checks, configuration keys, and response format stay the same. The package includes TypeScript types, usage instructions, and the original license and changelog. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added a status-check plugin with a default `/egg.status` endpoint that reports whether the application is available based on configured status files. - Supports custom health-check paths, response keywords, HTTP status codes, and multiple status files. - **Documentation** - Added setup, configuration, deployment, and migration guidance for the status plugin. - **Tests** - Added coverage for default and custom health-check behavior, including available and unavailable responses. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent bc5bd00 commit 86716cd

19 files changed

Lines changed: 462 additions & 0 deletions

File tree

‎plugins/status/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## 1.0.0 (2023-04-21)
4+
5+
6+
### Features
7+
8+
* init first version ([#1](https://github.com/eggjs/egg-status/issues/1)) ([6d531ce](https://github.com/eggjs/egg-status/commit/6d531ce227aa7399e492fd77e4f8b569b376ce7c))

‎plugins/status/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-present Alibaba Group Holding Limited and other contributors.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎plugins/status/README.md‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# @eggjs/status
2+
3+
Health check middleware for Egg applications, migrated from [egg-status](https://github.com/eggjs/egg-status).
4+
5+
This package supports Egg 4 and Node.js >= 22.18.0. Applications on Egg 3 can continue to use `egg-status`.
6+
7+
## Install
8+
9+
```bash
10+
npm i @eggjs/status
11+
```
12+
13+
## Usage
14+
15+
Enable the plugin in `config/plugin.ts`:
16+
17+
```ts
18+
import statusPlugin from '@eggjs/status';
19+
20+
export default {
21+
...statusPlugin(),
22+
};
23+
```
24+
25+
The package declaration is also supported:
26+
27+
```ts
28+
export default {
29+
status: {
30+
enable: true,
31+
package: '@eggjs/status',
32+
},
33+
};
34+
```
35+
36+
The middleware runs immediately after `meta`. Requests to `/egg.status` return HTTP `200` with an `online` keyword when `${baseDir}/egg.status` exists. Otherwise, they return HTTP `404` with an `offline` keyword. The response also includes the application name, middleware start time, and current time. Other paths continue to the application's middleware and routes.
37+
38+
## Configuration
39+
40+
The defaults are:
41+
42+
```ts
43+
// config/config.default.ts
44+
import path from 'node:path';
45+
import type { EggAppInfo, PartialEggConfig } from 'egg';
46+
import '@eggjs/status';
47+
48+
export default (appInfo: EggAppInfo): PartialEggConfig => ({
49+
status: {
50+
availableResponseKeyword: 'online',
51+
unavailableResponseKeyword: 'offline',
52+
unavailableResponseStatus: 404,
53+
checkStatusURLs: ['/egg.status'],
54+
checkStatusFiles: [path.join(appInfo.baseDir, 'egg.status')],
55+
},
56+
});
57+
```
58+
59+
`checkStatusURLs` matches exact request paths; query strings do not affect matching. The application is available when **any** configured status file is accessible. File contents are ignored. If no file is accessible, including when the file list is empty, the middleware returns the configured unavailable response. Files are checked on each request, so creating or removing a file changes the response without a restart.
60+
61+
The package exports `StatusConfig` for TypeScript consumers.
62+
63+
## Deployment
64+
65+
Use [@eggjs/scripts](../../tools/scripts/README.md) to start and stop the application:
66+
67+
```bash
68+
npm i @eggjs/scripts
69+
```
70+
71+
On a POSIX system, create `egg.status` after startup and remove it before shutdown:
72+
73+
```json
74+
{
75+
"scripts": {
76+
"start": "egg-scripts start --daemon && touch egg.status",
77+
"stop": "rm -f egg.status && sleep 15 && egg-scripts stop"
78+
}
79+
}
80+
```
81+
82+
Configure the load balancer to check `/egg.status` and accept HTTP `200`. Adjust the shutdown delay to allow the load balancer to stop sending traffic before the application exits.
83+
84+
## Migration from egg-status
85+
86+
Replace the `egg-status` dependency with `@eggjs/status` and update `config/plugin.ts` as shown above. The `status` configuration keys, default health path, status file, and response format are unchanged.
87+
88+
## Contributing
89+
90+
See the [contributing guide](../../CONTRIBUTING.md). Report bugs and suggestions in the [issue tracker](https://github.com/eggjs/egg/issues).
91+
92+
## License
93+
94+
[MIT](LICENSE)

‎plugins/status/package.json‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@eggjs/status",
3+
"version": "4.1.2",
4+
"description": "Health check middleware for Egg applications",
5+
"keywords": [
6+
"egg",
7+
"egg-plugin",
8+
"health-check",
9+
"status"
10+
],
11+
"homepage": "https://github.com/eggjs/egg/tree/main/plugins/status",
12+
"bugs": {
13+
"url": "https://github.com/eggjs/egg/issues"
14+
},
15+
"license": "MIT",
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/eggjs/egg.git",
19+
"directory": "plugins/status"
20+
},
21+
"files": [
22+
"dist"
23+
],
24+
"type": "module",
25+
"types": "./dist/index.d.ts",
26+
"exports": {
27+
".": "./src/index.ts",
28+
"./app": "./src/app.ts",
29+
"./app/middleware/status": "./src/app/middleware/status.ts",
30+
"./config/config.default": "./src/config/config.default.ts",
31+
"./types": "./src/types.ts",
32+
"./package.json": "./package.json"
33+
},
34+
"publishConfig": {
35+
"exports": {
36+
".": "./dist/index.js",
37+
"./app": "./dist/app.js",
38+
"./app/middleware/status": "./dist/app/middleware/status.js",
39+
"./config/config.default": "./dist/config/config.default.js",
40+
"./types": "./dist/types.js",
41+
"./package.json": "./package.json"
42+
},
43+
"access": "public"
44+
},
45+
"scripts": {
46+
"typecheck": "tsc --noEmit"
47+
},
48+
"devDependencies": {
49+
"@eggjs/mock": "workspace:*",
50+
"@types/node": "catalog:",
51+
"egg": "workspace:*",
52+
"typescript": "catalog:"
53+
},
54+
"peerDependencies": {
55+
"egg": "workspace:*"
56+
},
57+
"engines": {
58+
"node": ">=22.18.0"
59+
}
60+
}

‎plugins/status/src/app.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from 'node:assert/strict';
2+
3+
import type { Application, ILifecycleBoot } from 'egg';
4+
5+
export default class AppBoot implements ILifecycleBoot {
6+
private readonly app;
7+
8+
constructor(app: Application) {
9+
this.app = app;
10+
}
11+
12+
configWillLoad(): void {
13+
const coreMiddleware = this.app.config.coreMiddleware;
14+
const index = coreMiddleware.indexOf('meta');
15+
assert(index >= 0, 'meta middleware not exists');
16+
coreMiddleware.splice(index + 1, 0, 'status');
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs/promises';
2+
3+
import type { Application, MiddlewareFunc } from 'egg';
4+
5+
import type { StatusConfig } from '../../config/config.default.ts';
6+
7+
async function existsFile(filepath: string): Promise<boolean> {
8+
try {
9+
await fs.access(filepath);
10+
return true;
11+
} catch {
12+
return false;
13+
}
14+
}
15+
16+
export default (options: StatusConfig, app: Application): MiddlewareFunc => {
17+
const {
18+
availableResponseKeyword,
19+
unavailableResponseKeyword,
20+
unavailableResponseStatus,
21+
checkStatusURLs,
22+
checkStatusFiles,
23+
} = options;
24+
const starttime = Date();
25+
26+
return async function serverStatus(ctx, next) {
27+
if (!checkStatusURLs.includes(ctx.path)) {
28+
return next();
29+
}
30+
31+
let exists = false;
32+
for (const statusFile of checkStatusFiles) {
33+
if (await existsFile(statusFile)) {
34+
exists = true;
35+
break;
36+
}
37+
}
38+
const keyword = exists ? availableResponseKeyword : unavailableResponseKeyword;
39+
ctx.status = exists ? 200 : unavailableResponseStatus;
40+
ctx.body = `${app.name} (${starttime} ~ ${Date()}), status: ${keyword}`;
41+
};
42+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path';
2+
3+
import type { EggAppInfo } from 'egg';
4+
5+
export interface StatusConfig {
6+
/** Available response keyword. Defaults to `online`. */
7+
availableResponseKeyword: string;
8+
/** Unavailable response keyword. Defaults to `offline`. */
9+
unavailableResponseKeyword: string;
10+
/** Unavailable HTTP status code. Defaults to `404`. */
11+
unavailableResponseStatus: number;
12+
/** Exact request paths to check. Defaults to `['/egg.status']`. */
13+
checkStatusURLs: string[];
14+
/** Available when any file exists. Defaults to `['${baseDir}/egg.status']`. */
15+
checkStatusFiles: string[];
16+
}
17+
18+
export default (appInfo: EggAppInfo): { status: StatusConfig } => ({
19+
status: {
20+
availableResponseKeyword: 'online',
21+
unavailableResponseKeyword: 'offline',
22+
unavailableResponseStatus: 404,
23+
checkStatusURLs: ['/egg.status'],
24+
checkStatusFiles: [path.join(appInfo.baseDir, 'egg.status')],
25+
},
26+
});

‎plugins/status/src/index.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { definePluginFactory, type EggPluginFactory } from 'egg';
2+
3+
import './types.ts';
4+
5+
export type { StatusConfig } from './config/config.default.ts';
6+
7+
/**
8+
* Usage:
9+
* ```ts
10+
* // config/plugin.ts
11+
* import statusPlugin from '@eggjs/status';
12+
*
13+
* export default {
14+
* ...statusPlugin(),
15+
* };
16+
* ```
17+
*/
18+
export default definePluginFactory({
19+
name: 'status',
20+
enable: true,
21+
path: import.meta.dirname,
22+
}) as EggPluginFactory;

‎plugins/status/src/types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { StatusConfig } from './config/config.default.ts';
2+
3+
declare module 'egg' {
4+
interface EggAppConfig {
5+
/** Health check middleware options. */
6+
status?: StatusConfig;
7+
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import path from 'node:path';
2+
3+
import type { EggAppInfo, PartialEggConfig } from 'egg';
4+
import '@eggjs/status';
5+
6+
export default (appInfo: EggAppInfo): PartialEggConfig => ({
7+
keys: 'status-test',
8+
status: {
9+
availableResponseKeyword: 'ready',
10+
unavailableResponseKeyword: 'unavailable',
11+
unavailableResponseStatus: 503,
12+
checkStatusURLs: ['/health', '/ready'],
13+
checkStatusFiles: [path.join(appInfo.baseDir, 'first.status'), path.join(appInfo.baseDir, 'second.status')],
14+
},
15+
});

0 commit comments

Comments
 (0)