@fastcar/koa 0.1.9
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/LICENSE +20 -0
- package/README.md +110 -0
- package/annotation.d.ts +46 -0
- package/index.d.ts +63 -0
- package/package.json +123 -0
- package/src/KoaApplication.ts +139 -0
- package/src/annotation/EnableKoa.ts +9 -0
- package/src/annotation/KoaMiddleware.ts +19 -0
- package/src/annotation/router/AddMapping.ts +31 -0
- package/src/annotation/router/AllMapping.ts +12 -0
- package/src/annotation/router/DeleteMapping.ts +12 -0
- package/src/annotation/router/GetMapping.ts +12 -0
- package/src/annotation/router/PatchMapping.ts +12 -0
- package/src/annotation/router/PostMapping.ts +12 -0
- package/src/annotation/router/PutMapping.ts +12 -0
- package/src/annotation/router/RequestMapping.ts +23 -0
- package/src/annotation.ts +43 -0
- package/src/index.ts +11 -0
- package/src/middleware/ExceptionGlobalHandler.ts +28 -0
- package/src/middleware/KoaBody.ts +11 -0
- package/src/middleware/KoaBodyParser.ts +11 -0
- package/src/middleware/KoaCors.ts +38 -0
- package/src/middleware/KoaStatic.ts +42 -0
- package/src/middleware/Swagger.ts +63 -0
- package/src/type/DesignMeta.ts +4 -0
- package/src/type/KoaConfig.ts +13 -0
- package/src/type/MethodType.ts +8 -0
- package/src/type/RouteMethods.ts +8 -0
- package/target/KoaApplication.js +139 -0
- package/target/annotation/EnableKoa.js +11 -0
- package/target/annotation/KoaMiddleware.js +19 -0
- package/target/annotation/router/AddMapping.js +29 -0
- package/target/annotation/router/AllMapping.js +14 -0
- package/target/annotation/router/DeleteMapping.js +14 -0
- package/target/annotation/router/GetMapping.js +14 -0
- package/target/annotation/router/PatchMapping.js +14 -0
- package/target/annotation/router/PostMapping.js +14 -0
- package/target/annotation/router/PutMapping.js +14 -0
- package/target/annotation/router/RequestMapping.js +22 -0
- package/target/annotation.js +38 -0
- package/target/index.js +19 -0
- package/target/middleware/ExceptionGlobalHandler.js +32 -0
- package/target/middleware/KoaBody.js +10 -0
- package/target/middleware/KoaBodyParser.js +10 -0
- package/target/middleware/KoaCors.js +33 -0
- package/target/middleware/KoaStatic.js +38 -0
- package/target/middleware/Swagger.js +53 -0
- package/target/npmlist.json +1 -0
- package/target/type/DesignMeta.js +7 -0
- package/target/type/KoaConfig.js +2 -0
- package/target/type/MethodType.js +2 -0
- package/target/type/RouteMethods.js +12 -0
- package/test/logs/koa.log +0 -0
- package/test/logs/serverlogger.log +3 -0
- package/test/logs/sys.log +70 -0
- package/test/resource/application.yml +18 -0
- package/test/resource/public/api/index.yaml +37 -0
- package/test/resource/public/hello.txt +1 -0
- package/test/simple/app.ts +38 -0
- package/test/simple/controller/HelloController.ts +37 -0
package/target/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DesignMeta = exports.Swagger = exports.KoaStatic = exports.KoaCors = exports.KoaBodyParser = exports.KoaBody = exports.ExceptionGlobalHandler = exports.KoaApplication = void 0;
|
|
4
|
+
const KoaApplication_1 = require("./KoaApplication");
|
|
5
|
+
exports.KoaApplication = KoaApplication_1.default;
|
|
6
|
+
const ExceptionGlobalHandler_1 = require("./middleware/ExceptionGlobalHandler");
|
|
7
|
+
exports.ExceptionGlobalHandler = ExceptionGlobalHandler_1.default;
|
|
8
|
+
const KoaBody_1 = require("./middleware/KoaBody");
|
|
9
|
+
exports.KoaBody = KoaBody_1.default;
|
|
10
|
+
const KoaBodyParser_1 = require("./middleware/KoaBodyParser");
|
|
11
|
+
exports.KoaBodyParser = KoaBodyParser_1.default;
|
|
12
|
+
const KoaCors_1 = require("./middleware/KoaCors");
|
|
13
|
+
exports.KoaCors = KoaCors_1.default;
|
|
14
|
+
const KoaStatic_1 = require("./middleware/KoaStatic");
|
|
15
|
+
exports.KoaStatic = KoaStatic_1.default;
|
|
16
|
+
const Swagger_1 = require("./middleware/Swagger");
|
|
17
|
+
exports.Swagger = Swagger_1.default;
|
|
18
|
+
const DesignMeta_1 = require("./type/DesignMeta");
|
|
19
|
+
Object.defineProperty(exports, "DesignMeta", { enumerable: true, get: function () { return DesignMeta_1.DesignMeta; } });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@fastcar/core");
|
|
4
|
+
//默认错误捕捉
|
|
5
|
+
function ExceptionGlobalHandler(app) {
|
|
6
|
+
let logger = app.getSysLogger();
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
try {
|
|
9
|
+
await next();
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
//新增如果是校验的错误则进行输出
|
|
13
|
+
if (e instanceof core_1.ValidError) {
|
|
14
|
+
ctx.body = {
|
|
15
|
+
code: 400,
|
|
16
|
+
msg: e.message || "parameter error",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
logger.error(`${ctx.url} is error`);
|
|
21
|
+
if (e) {
|
|
22
|
+
logger.error(e);
|
|
23
|
+
}
|
|
24
|
+
ctx.body = {
|
|
25
|
+
code: 500,
|
|
26
|
+
msg: "Service internal error",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.default = ExceptionGlobalHandler;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const koaBody = require("koa-body");
|
|
4
|
+
//对于文件上传做限定
|
|
5
|
+
function KoaBody(app) {
|
|
6
|
+
let koaConfig = app.getSetting("koa");
|
|
7
|
+
let bodyConfig = koaConfig?.koaBodyOptions;
|
|
8
|
+
return koaBody(bodyConfig);
|
|
9
|
+
}
|
|
10
|
+
exports.default = KoaBody;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bodyParser = require("koa-bodyparser");
|
|
4
|
+
//对文件内容做解析
|
|
5
|
+
function KoaBodyParser(app) {
|
|
6
|
+
let koaConfig = app.getSetting("koa");
|
|
7
|
+
let bodyConfig = koaConfig?.koaBodyParser;
|
|
8
|
+
return bodyParser(bodyConfig);
|
|
9
|
+
}
|
|
10
|
+
exports.default = KoaBodyParser;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const koa2Cors = require("koa2-cors");
|
|
4
|
+
function KoaCors(app) {
|
|
5
|
+
let koaConfig = app.getSetting("koa");
|
|
6
|
+
if (koaConfig?.extra) {
|
|
7
|
+
let corsConfig = Reflect.get(koaConfig.extra, "cors");
|
|
8
|
+
if (!!corsConfig) {
|
|
9
|
+
//兼容支持多个跨域
|
|
10
|
+
if (typeof corsConfig.origin == "string") {
|
|
11
|
+
let origins = corsConfig.origin.split(";");
|
|
12
|
+
Reflect.set(corsConfig, "origin", (ctx) => {
|
|
13
|
+
let orign = ctx?.request?.header?.origin;
|
|
14
|
+
if (!orign) {
|
|
15
|
+
if (!origins.includes("*")) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return "*";
|
|
19
|
+
}
|
|
20
|
+
for (let o of origins) {
|
|
21
|
+
if (orign.startsWith(o) || o == "*") {
|
|
22
|
+
return o;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return koa2Cors(corsConfig);
|
|
30
|
+
}
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
exports.default = KoaCors;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const koaStatic = require("koa-static");
|
|
4
|
+
const KoaRange = require("koa-range");
|
|
5
|
+
const KoaMount = require("koa-mount");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
//支持静态文件访问
|
|
9
|
+
function KoaStatic(app) {
|
|
10
|
+
let mlist = [];
|
|
11
|
+
//采用koa-range使文件可以流式传播
|
|
12
|
+
mlist.push(KoaRange);
|
|
13
|
+
let koaConfig = app.getSetting("koa");
|
|
14
|
+
if (!!koaConfig?.koaStatic) {
|
|
15
|
+
let keys = Object.keys(koaConfig?.koaStatic);
|
|
16
|
+
if (keys.length > 0) {
|
|
17
|
+
for (let key of keys) {
|
|
18
|
+
let fp = koaConfig.koaStatic[key];
|
|
19
|
+
let rp = path.join(app.getResourcePath(), fp);
|
|
20
|
+
if (!fs.existsSync(fp)) {
|
|
21
|
+
if (!fs.existsSync(rp)) {
|
|
22
|
+
console.error(`${fp} is not found`);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
fp = rp;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!key.startsWith("/")) {
|
|
30
|
+
key = `/${key}`;
|
|
31
|
+
}
|
|
32
|
+
mlist.push(KoaMount(key, koaStatic(fp)));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return mlist;
|
|
37
|
+
}
|
|
38
|
+
exports.default = KoaStatic;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const koaStatic = require("koa-static");
|
|
6
|
+
const KoaMount = require("koa-mount");
|
|
7
|
+
const swaggerDefalutUrl = "https://petstore.swagger.io/v2/swagger.json";
|
|
8
|
+
//api显示和管理
|
|
9
|
+
function Swagger(app) {
|
|
10
|
+
let mlist = [];
|
|
11
|
+
let koaConfig = app.getSetting("koa");
|
|
12
|
+
if (koaConfig.swagger && koaConfig.swagger.enable) {
|
|
13
|
+
let apiMap = new Map();
|
|
14
|
+
let fileMap = new Map();
|
|
15
|
+
let apis = koaConfig.swagger.api;
|
|
16
|
+
if (apis) {
|
|
17
|
+
Object.keys(apis).forEach((key) => {
|
|
18
|
+
let value = apis[key];
|
|
19
|
+
if (!key.startsWith("/")) {
|
|
20
|
+
key = `/${key}`;
|
|
21
|
+
}
|
|
22
|
+
let realValue = value.startsWith("/") ? value : "/" + value;
|
|
23
|
+
apiMap.set(key, realValue);
|
|
24
|
+
fileMap.set(realValue, path.join(app.getResourcePath(), value));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//进行设置静态访问路径
|
|
28
|
+
const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath();
|
|
29
|
+
mlist.push(KoaMount("/swagger-ui", koaStatic(swaggerUiAssetPath)));
|
|
30
|
+
const swaggerTemplate = fs.readFileSync(path.join(swaggerUiAssetPath, "index.html"), "utf-8");
|
|
31
|
+
const fn = async (ctx, next) => {
|
|
32
|
+
let url = ctx.url;
|
|
33
|
+
let item = apiMap.get(url);
|
|
34
|
+
if (!!item) {
|
|
35
|
+
//输出路径
|
|
36
|
+
ctx.type = "text/html";
|
|
37
|
+
ctx.body = swaggerTemplate.replace(swaggerDefalutUrl, item).replace(/\.\//g, "./swagger-ui/");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let fp = fileMap.get(url);
|
|
41
|
+
if (fp) {
|
|
42
|
+
if (fs.existsSync(fp)) {
|
|
43
|
+
ctx.body = fs.readFileSync(fp, "utf-8");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
await next();
|
|
48
|
+
};
|
|
49
|
+
mlist.unshift(fn);
|
|
50
|
+
}
|
|
51
|
+
return mlist;
|
|
52
|
+
}
|
|
53
|
+
exports.default = Swagger;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.1.5","name":"fastcar-koa","dependencies":{"koa-body":{"version":"4.2.0"},"koa-bodyparser":{"version":"4.3.0"},"koa-mount":{"version":"4.0.0"},"koa-range":{"version":"0.3.0"},"koa-router":{"version":"10.1.1"},"koa-static":{"version":"5.0.0"},"koa":{"version":"2.13.4"},"koa2-cors":{"version":"2.0.6"},"swagger-ui-dist":{"version":"4.10.0"}}}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RouteMethods = void 0;
|
|
4
|
+
var RouteMethods;
|
|
5
|
+
(function (RouteMethods) {
|
|
6
|
+
RouteMethods["GetMapping"] = "get";
|
|
7
|
+
RouteMethods["PostMapping"] = "post";
|
|
8
|
+
RouteMethods["PutMapping"] = "put";
|
|
9
|
+
RouteMethods["DeleteMapping"] = "delete";
|
|
10
|
+
RouteMethods["PatchMapping"] = "patch";
|
|
11
|
+
RouteMethods["AllMapping"] = "all";
|
|
12
|
+
})(RouteMethods = exports.RouteMethods || (exports.RouteMethods = {}));
|
|
File without changes
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
{"timestamp":"2022-04-10 15:12:31.105","level":"INFO","label":"serverlogger","message":"http server is running in 1234"}
|
|
2
|
+
{"timestamp":"2022-04-10 16:06:15.720","level":"INFO","label":"serverlogger","message":"http server is running in 1234"}
|
|
3
|
+
{"timestamp":"2022-04-10 16:09:07.975","level":"INFO","label":"serverlogger","message":"http server is running in 1234"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{"timestamp":"2022-04-10 15:12:30.147","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
2
|
+
{"timestamp":"2022-04-10 15:12:30.553","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
3
|
+
{"timestamp":"2022-04-10 15:12:30.554","level":"INFO","label":"sys","message":"Start component injection"}
|
|
4
|
+
{"timestamp":"2022-04-10 15:12:30.557","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
5
|
+
{"timestamp":"2022-04-10 15:12:30.558","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
6
|
+
{"timestamp":"2022-04-10 15:12:31.106","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
7
|
+
{"timestamp":"2022-04-10 15:12:31.107","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
8
|
+
{"timestamp":"2022-04-10 16:06:14.433","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
9
|
+
{"timestamp":"2022-04-10 16:06:15.178","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
10
|
+
{"timestamp":"2022-04-10 16:06:15.179","level":"INFO","label":"sys","message":"Start component injection"}
|
|
11
|
+
{"timestamp":"2022-04-10 16:06:15.182","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
12
|
+
{"timestamp":"2022-04-10 16:06:15.183","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
13
|
+
{"timestamp":"2022-04-10 16:06:15.722","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
14
|
+
{"timestamp":"2022-04-10 16:06:15.723","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
15
|
+
{"timestamp":"2022-04-10 16:09:07.207","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
16
|
+
{"timestamp":"2022-04-10 16:09:07.592","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
17
|
+
{"timestamp":"2022-04-10 16:09:07.593","level":"INFO","label":"sys","message":"Start component injection"}
|
|
18
|
+
{"timestamp":"2022-04-10 16:09:07.595","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
19
|
+
{"timestamp":"2022-04-10 16:09:07.596","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
20
|
+
{"timestamp":"2022-04-10 16:09:07.976","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
21
|
+
{"timestamp":"2022-04-10 16:09:07.977","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
22
|
+
{"timestamp":"2022-04-10 16:09:36.884","level":"INFO","label":"sys","message":"exit reason","splat":"[\"sigint exit\"]"}
|
|
23
|
+
{"timestamp":"2022-04-11 18:02:08.195","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
24
|
+
{"timestamp":"2022-04-11 18:02:08.682","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
25
|
+
{"timestamp":"2022-04-11 18:02:08.682","level":"INFO","label":"sys","message":"Start component injection"}
|
|
26
|
+
{"timestamp":"2022-04-11 18:02:08.684","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
27
|
+
{"timestamp":"2022-04-11 18:02:08.684","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
28
|
+
{"timestamp":"2022-04-11 18:02:09.01","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
29
|
+
{"timestamp":"2022-04-11 18:02:09.02","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
30
|
+
{"timestamp":"2022-04-11 18:02:09.03","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
31
|
+
{"timestamp":"2022-04-11 18:03:57.591","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
32
|
+
{"timestamp":"2022-04-11 18:03:57.852","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
33
|
+
{"timestamp":"2022-04-11 18:03:57.853","level":"INFO","label":"sys","message":"Start component injection"}
|
|
34
|
+
{"timestamp":"2022-04-11 18:03:57.854","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
35
|
+
{"timestamp":"2022-04-11 18:03:57.855","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
36
|
+
{"timestamp":"2022-04-11 18:03:58.84","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
37
|
+
{"timestamp":"2022-04-11 18:03:58.85","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
38
|
+
{"timestamp":"2022-04-11 18:03:58.86","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
39
|
+
{"timestamp":"2022-04-11 18:04:22.624","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
40
|
+
{"timestamp":"2022-04-11 18:04:22.871","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
41
|
+
{"timestamp":"2022-04-11 18:04:22.872","level":"INFO","label":"sys","message":"Start component injection"}
|
|
42
|
+
{"timestamp":"2022-04-11 18:04:22.873","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
43
|
+
{"timestamp":"2022-04-11 18:04:22.874","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
44
|
+
{"timestamp":"2022-04-11 18:04:23.104","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
45
|
+
{"timestamp":"2022-04-11 18:04:23.105","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
46
|
+
{"timestamp":"2022-04-11 18:04:23.106","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
47
|
+
{"timestamp":"2022-04-22 21:16:31.916","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
48
|
+
{"timestamp":"2022-04-22 21:16:32.795","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
49
|
+
{"timestamp":"2022-04-22 21:16:32.797","level":"INFO","label":"sys","message":"Start component injection"}
|
|
50
|
+
{"timestamp":"2022-04-22 21:16:32.799","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
51
|
+
{"timestamp":"2022-04-22 21:16:32.799","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
52
|
+
{"timestamp":"2022-04-22 21:16:33.297","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
53
|
+
{"timestamp":"2022-04-22 21:16:33.298","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
54
|
+
{"timestamp":"2022-04-22 21:16:33.298","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
55
|
+
{"timestamp":"2022-04-23 09:53:25.585","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
56
|
+
{"timestamp":"2022-04-23 09:53:26.372","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
57
|
+
{"timestamp":"2022-04-23 09:53:26.372","level":"INFO","label":"sys","message":"Start component injection"}
|
|
58
|
+
{"timestamp":"2022-04-23 09:53:26.374","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
59
|
+
{"timestamp":"2022-04-23 09:53:26.374","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
60
|
+
{"timestamp":"2022-04-23 09:53:26.862","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
61
|
+
{"timestamp":"2022-04-23 09:53:26.863","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
62
|
+
{"timestamp":"2022-04-23 09:53:26.864","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
63
|
+
{"timestamp":"2022-04-23 10:22:03.459","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
64
|
+
{"timestamp":"2022-04-23 10:22:03.909","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
65
|
+
{"timestamp":"2022-04-23 10:22:03.910","level":"INFO","label":"sys","message":"Start component injection"}
|
|
66
|
+
{"timestamp":"2022-04-23 10:22:03.913","level":"INFO","label":"sys","message":"Complete component injection"}
|
|
67
|
+
{"timestamp":"2022-04-23 10:22:03.914","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
68
|
+
{"timestamp":"2022-04-23 10:22:04.326","level":"INFO","label":"sys","message":"http server is running in 12234"}
|
|
69
|
+
{"timestamp":"2022-04-23 10:22:04.327","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
70
|
+
{"timestamp":"2022-04-23 10:22:04.328","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
application:
|
|
2
|
+
name: "koaSimple"
|
|
3
|
+
settings:
|
|
4
|
+
koa:
|
|
5
|
+
server: { port: 1234 } #服务器配置 支持 http http2和https
|
|
6
|
+
# koaStatic: #静态资源访问
|
|
7
|
+
# { "public": "public" }
|
|
8
|
+
swagger:
|
|
9
|
+
enable: true
|
|
10
|
+
api: { "index": "/public/api/index.yaml" }
|
|
11
|
+
extra: #额外配置支持
|
|
12
|
+
cors: #跨域支持示范
|
|
13
|
+
origin: "https://localhost:1234"
|
|
14
|
+
maxAge: 60000 #指定本次预检请求的有效期,单位为秒。
|
|
15
|
+
credentials: true #是否允许发送Cookie
|
|
16
|
+
allowMethods: ["GET", "POST"] #设置所允许的HTTP请求方法
|
|
17
|
+
allowHeaders: ["Content-Type", "Authorization", "Accept"] #设置服务器支持的所有头信息字段
|
|
18
|
+
exposeHeaders: ["WWW-Authenticate", "Server-Authorization"] #设置获取其他自定义字段
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
openapi: "3.0.0"
|
|
2
|
+
info:
|
|
3
|
+
title: "api 测试"
|
|
4
|
+
description: "测试"
|
|
5
|
+
version: "1.0.0"
|
|
6
|
+
servers:
|
|
7
|
+
- url: http://localhost:1234
|
|
8
|
+
description: 默认版本
|
|
9
|
+
paths:
|
|
10
|
+
/getHello:
|
|
11
|
+
get:
|
|
12
|
+
summary: hello 请求
|
|
13
|
+
tags:
|
|
14
|
+
- "hello"
|
|
15
|
+
description: ""
|
|
16
|
+
parameters:
|
|
17
|
+
- in: query
|
|
18
|
+
name: id
|
|
19
|
+
description: 传一个id过去
|
|
20
|
+
required: true
|
|
21
|
+
schema:
|
|
22
|
+
type: integer
|
|
23
|
+
responses:
|
|
24
|
+
200:
|
|
25
|
+
description: 成功
|
|
26
|
+
content:
|
|
27
|
+
application/json:
|
|
28
|
+
schema:
|
|
29
|
+
$ref: "#/components/schemas/ApiResponse"
|
|
30
|
+
components:
|
|
31
|
+
schemas:
|
|
32
|
+
ApiResponse:
|
|
33
|
+
type: object
|
|
34
|
+
properties:
|
|
35
|
+
code: { type: integer, default: 200, description: "响应码" }
|
|
36
|
+
msg: { type: string, default: "success", description: "响应描述" }
|
|
37
|
+
data: { type: object, description: "自定义数据" }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hello world
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
2
|
+
import { Application } from "@fastcar/core/annotation";
|
|
3
|
+
import EnableKoa from "../../src/annotation/EnableKoa";
|
|
4
|
+
import KoaMiddleware from "../../src/annotation/KoaMiddleware";
|
|
5
|
+
import KoaStatic from "../../src/middleware/KoaStatic";
|
|
6
|
+
import KoaBodyParser from "../../src/middleware/KoaBodyParser";
|
|
7
|
+
import * as Koa from "koa";
|
|
8
|
+
import ExceptionGlobalHandler from "../../src/middleware/ExceptionGlobalHandler";
|
|
9
|
+
import KoaCors from "../../src/middleware/koaCors";
|
|
10
|
+
import Swagger from "../../src/middleware/Swagger";
|
|
11
|
+
|
|
12
|
+
const m1 = () => {
|
|
13
|
+
return async (ctx: any, next: Function) => {
|
|
14
|
+
console.log("m1--- in");
|
|
15
|
+
await next();
|
|
16
|
+
console.log("m1--- out");
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const m2 = (): Koa.Middleware => {
|
|
21
|
+
return async (ctx: any, next: Function) => {
|
|
22
|
+
console.log("m2--- in");
|
|
23
|
+
await next();
|
|
24
|
+
console.log("m2--- out");
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
@Application
|
|
29
|
+
@EnableKoa //开启koa
|
|
30
|
+
@KoaMiddleware(ExceptionGlobalHandler, KoaStatic, KoaBodyParser)
|
|
31
|
+
@KoaMiddleware(KoaCors)
|
|
32
|
+
@KoaMiddleware(Swagger)
|
|
33
|
+
@KoaMiddleware(m1, m2)
|
|
34
|
+
class APP {
|
|
35
|
+
app!: FastCarApplication;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const app = new APP();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Controller, Rule, ValidForm } from "@fastcar/core/annotation";
|
|
2
|
+
import GetMapping from "../../../src/annotation/router/GetMapping";
|
|
3
|
+
import PostMapping from "../../../src/annotation/router/PostMapping";
|
|
4
|
+
import RequestMapping from "../../../src/annotation/router/RequestMapping";
|
|
5
|
+
import { GET } from "../../../src/annotation";
|
|
6
|
+
|
|
7
|
+
@Controller
|
|
8
|
+
// @RequestMapping("/test") //允许头部追加url
|
|
9
|
+
export default class HelloController {
|
|
10
|
+
hello: string = "hello";
|
|
11
|
+
|
|
12
|
+
@GET("/")
|
|
13
|
+
home() {
|
|
14
|
+
return "hello world";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//支持多种http的请求方式
|
|
18
|
+
@GetMapping("/getHello")
|
|
19
|
+
@PostMapping("/getHello")
|
|
20
|
+
getHello(data: any) {
|
|
21
|
+
return {
|
|
22
|
+
code: 200,
|
|
23
|
+
data: this.hello,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@GetMapping("/getError")
|
|
28
|
+
getError() {
|
|
29
|
+
throw new Error("test error");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@GetMapping("/checkParam")
|
|
33
|
+
@ValidForm
|
|
34
|
+
async checkParam(@Rule({ hello: { required: true, maxSize: 10, type: "string" } }) { hello }: any) {
|
|
35
|
+
return hello;
|
|
36
|
+
}
|
|
37
|
+
}
|