Skip to content

Commit

Permalink
feat(artusx-koa): import @artusx/otl
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed May 22, 2024
1 parent 72c36d4 commit 5ebe5ea
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/apps/artusx-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"@artusx/core": "workspace:*",
"@artusx/otl": "workspace:*",
"@artusx/plugin-ejs": "workspace:*",
"@artusx/plugin-redis": "workspace:*",
"@artusx/plugin-sequelize": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/artusx-koa/src/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -34,7 +34,7 @@ export default () => {
const artusx: ArtusXConfig = {
keys: 'artusx-koa',
port: 7001,
middlewares: [LimitRate, checkAuth],
middlewares: [limitRate, checkAuth],
static: {
dirs: [
{
Expand Down
8 changes: 6 additions & 2 deletions packages/apps/artusx-koa/src/controller/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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!' });
}

Expand All @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/artusx-koa/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 });
3 changes: 2 additions & 1 deletion packages/apps/artusx-koa/src/middleware/checkAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default class CheckAuthMiddleware {

async use(ctx: ArtusXContext, next: ArtusXNext): Promise<void> {
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();
}
}
7 changes: 0 additions & 7 deletions packages/apps/artusx-koa/src/middleware/traceTime.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/apps/artusx-koa/src/middleware/tracing.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const traceId = getTraceId();
console.log('tracing', {
traceId,
path: _ctx.path,
});

console.time('tracing');
await next();
console.timeEnd('tracing');
}
6 changes: 6 additions & 0 deletions packages/apps/artusx-koa/src/public/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.nav {
padding: 2em 4em;
display: flex;
gap: 1em;
}

.container {
margin: 2em 4em;
}
2 changes: 1 addition & 1 deletion packages/apps/artusx-koa/src/schedule/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PluginInjectEnum } from '@artusx/utils';
import { EjsClient } from '@artusx/plugin-ejs';

@Schedule({
enable: true,
enable: false,
cron: '30 * * * * *',
runOnInit: true,
})
Expand Down
1 change: 1 addition & 0 deletions packages/apps/artusx-koa/src/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="/public/style.css" />
</head>
<body>
{% include "nav.html" ignore missing %}
<div class="container">
<h1>{{title}}</h1>
<p>{{message}}</p>
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/artusx-koa/src/view/nav.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="nav">
<a href="/">Home</a>
<a href="/html">HTML</a>
</div>

0 comments on commit 5ebe5ea

Please sign in to comment.