diff --git a/packages/apps/artusx-koa/package.json b/packages/apps/artusx-koa/package.json index b95cb9bd..f3fc6ade 100644 --- a/packages/apps/artusx-koa/package.json +++ b/packages/apps/artusx-koa/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@artusx/core": "workspace:*", + "@artusx/otl": "workspace:*", "@artusx/plugin-ejs": "workspace:*", "@artusx/plugin-redis": "workspace:*", "@artusx/plugin-sequelize": "workspace:*", diff --git a/packages/apps/artusx-koa/src/config/config.default.ts b/packages/apps/artusx-koa/src/config/config.default.ts index 4003a076..7bf9ccf0 100644 --- a/packages/apps/artusx-koa/src/config/config.default.ts +++ b/packages/apps/artusx-koa/src/config/config.default.ts @@ -19,7 +19,7 @@ import type { SequelizeConfig } from '@artusx/plugin-sequelize'; import { getEnv } from '@artusx/utils'; -import LimitRate from '../middleware/LimitRate'; +import limitRate from '../middleware/limitRate'; import checkAuth from '../middleware/checkAuth'; const tmpDir = os.tmpdir(); @@ -34,7 +34,7 @@ export default () => { const artusx: ArtusXConfig = { keys: 'artusx-koa', port: 7001, - middlewares: [LimitRate, checkAuth], + middlewares: [limitRate, checkAuth], static: { dirs: [ { diff --git a/packages/apps/artusx-koa/src/controller/home.ts b/packages/apps/artusx-koa/src/controller/home.ts index e2f28eb9..98fd11b6 100644 --- a/packages/apps/artusx-koa/src/controller/home.ts +++ b/packages/apps/artusx-koa/src/controller/home.ts @@ -11,8 +11,9 @@ import { StatusCode, } from '@artusx/core'; +import { addEvent } from '@artusx/otl'; import type { ArtusXContext, Log4jsClient, NunjucksClient } from '@artusx/core'; -import traceTime from '../middleware/traceTime'; +import tracing from '../middleware/tracing'; @Controller() export default class HomeController { @@ -25,11 +26,13 @@ export default class HomeController { @Inject(ArtusXInjectEnum.Nunjucks) nunjucks: NunjucksClient; - @MW([traceTime]) + @MW([tracing]) @GET('/') async home(ctx: ArtusXContext) { const infoLogger = this.log4js.getLogger('default'); infoLogger.info(`path: /, method: GET`); + + addEvent('home', { key: 'home', value: 'home' }); ctx.body = this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Hello ArtusX!' }); } @@ -39,6 +42,7 @@ export default class HomeController { return this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Post method' }); } + @MW([tracing]) @GET('/html') @Headers({ 'x-handler': 'home-controller-html: html', diff --git a/packages/apps/artusx-koa/src/index.ts b/packages/apps/artusx-koa/src/index.ts index fff51f1b..b289c654 100644 --- a/packages/apps/artusx-koa/src/index.ts +++ b/packages/apps/artusx-koa/src/index.ts @@ -1,6 +1,9 @@ import path from 'path'; +import { setupTracing } from '@artusx/otl'; import { bootstrap } from '@artusx/utils'; const ROOT_DIR = path.resolve(__dirname); +setupTracing('artusx-koa'); + bootstrap({ root: ROOT_DIR }); diff --git a/packages/apps/artusx-koa/src/middleware/checkAuth.ts b/packages/apps/artusx-koa/src/middleware/checkAuth.ts index 5289f1ac..b1367cf1 100644 --- a/packages/apps/artusx-koa/src/middleware/checkAuth.ts +++ b/packages/apps/artusx-koa/src/middleware/checkAuth.ts @@ -9,8 +9,9 @@ export default class CheckAuthMiddleware { async use(ctx: ArtusXContext, next: ArtusXNext): Promise { const { data } = ctx.context.output; + ctx.context.input.params.authed = false; data.authed = false; - console.log('[middleware] - checkAuth', ctx.context); + // console.log('[middleware] - checkAuth', ctx.context); await next(); } } diff --git a/packages/apps/artusx-koa/src/middleware/LimitRate.ts b/packages/apps/artusx-koa/src/middleware/limitRate.ts similarity index 100% rename from packages/apps/artusx-koa/src/middleware/LimitRate.ts rename to packages/apps/artusx-koa/src/middleware/limitRate.ts diff --git a/packages/apps/artusx-koa/src/middleware/traceTime.ts b/packages/apps/artusx-koa/src/middleware/traceTime.ts deleted file mode 100644 index 848503c2..00000000 --- a/packages/apps/artusx-koa/src/middleware/traceTime.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ArtusXContext, ArtusXNext } from '@artusx/core'; - -export default async function traceTime(_ctx: ArtusXContext, next: ArtusXNext): Promise { - console.time('TraceTime'); - await next(); - console.timeEnd('TraceTime'); -} diff --git a/packages/apps/artusx-koa/src/middleware/tracing.ts b/packages/apps/artusx-koa/src/middleware/tracing.ts new file mode 100644 index 00000000..119c46ae --- /dev/null +++ b/packages/apps/artusx-koa/src/middleware/tracing.ts @@ -0,0 +1,14 @@ +import { ArtusXContext, ArtusXNext } from '@artusx/core'; +import { getTraceId } from '@artusx/otl'; + +export default async function tracing(_ctx: ArtusXContext, next: ArtusXNext): Promise { + const traceId = getTraceId(); + console.log('tracing', { + traceId, + path: _ctx.path, + }); + + console.time('tracing'); + await next(); + console.timeEnd('tracing'); +} diff --git a/packages/apps/artusx-koa/src/public/style.css b/packages/apps/artusx-koa/src/public/style.css index c5ddfeae..8e8dd107 100644 --- a/packages/apps/artusx-koa/src/public/style.css +++ b/packages/apps/artusx-koa/src/public/style.css @@ -1,3 +1,9 @@ +.nav { + padding: 2em 4em; + display: flex; + gap: 1em; +} + .container { margin: 2em 4em; } diff --git a/packages/apps/artusx-koa/src/schedule/notify.ts b/packages/apps/artusx-koa/src/schedule/notify.ts index ff1938c7..2556916a 100644 --- a/packages/apps/artusx-koa/src/schedule/notify.ts +++ b/packages/apps/artusx-koa/src/schedule/notify.ts @@ -5,7 +5,7 @@ import { PluginInjectEnum } from '@artusx/utils'; import { EjsClient } from '@artusx/plugin-ejs'; @Schedule({ - enable: true, + enable: false, cron: '30 * * * * *', runOnInit: true, }) diff --git a/packages/apps/artusx-koa/src/view/index.html b/packages/apps/artusx-koa/src/view/index.html index c764c2dd..35212f4d 100644 --- a/packages/apps/artusx-koa/src/view/index.html +++ b/packages/apps/artusx-koa/src/view/index.html @@ -7,6 +7,7 @@ + {% include "nav.html" ignore missing %}

{{title}}

{{message}}

diff --git a/packages/apps/artusx-koa/src/view/nav.html b/packages/apps/artusx-koa/src/view/nav.html new file mode 100644 index 00000000..a49af3f7 --- /dev/null +++ b/packages/apps/artusx-koa/src/view/nav.html @@ -0,0 +1,4 @@ +