Skip to content

Commit

Permalink
refactor: move cors plugin to @artusx/core
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Jan 5, 2024
1 parent ff87966 commit 5821947
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 51 deletions.
12 changes: 8 additions & 4 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions packages/apps/artusx-api/src/app/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import checkAuth from '../middleware/checkAuth';
dotenv.config();

export default {
port: 7001,
middlewares: [checkAuth],
koa: {
port: 7001
// middlewares: []
},

artusx: {
middlewares: [checkAuth]
},

redis: {
db: process.env.REDIS_DATABASE || 0,
Expand Down
1 change: 1 addition & 0 deletions packages/apps/artusx-api/src/app/middleware/checkAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Context, Next } from '@artus/pipeline';

export default async function checkAuth(ctx: Context, next: Next): Promise<void> {
const { data } = ctx.output;
data.authed = false;
console.log('middleware - checkAuth', data);
await next();
}
11 changes: 7 additions & 4 deletions packages/libs/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
"author": "Suyi <[email protected]>",
"license": "MIT",
"dependencies": {
"@artusx/plugin-application-http": "workspace:*"
"@artus/core": "^2.x",
"@artus/pipeline": "^0.2",
"@artusx/plugin-application-http": "workspace:*",
"@koa/cors": "~5.0.0"
},
"peerDependencies": {
"@artus/core": "^2.x",
"@artus/pipeline": "^0.2"
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@artus/tsconfig": "^1.0.1",
"@types/node": "^18.11.17",
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"@types/koa__cors": "~5.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
34 changes: 33 additions & 1 deletion packages/libs/core/src/app.ts
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
export default {};
import {
Inject,
ArtusInjectEnum,
ArtusApplication,
ApplicationLifecycle,
LifecycleHook,
LifecycleHookUnit
} from '@artus/core';

import cors from '@koa/cors';
import KoaApplication from '@artusx/plugin-application-http/lib/koa/application';

@LifecycleHookUnit()
export default class ArtusXCoreLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
private readonly app: ArtusApplication;

get koa(): KoaApplication {
return this.app.container.get(KoaApplication);
}

@LifecycleHook()
async didLoad() {
this.koa.use(
cors({
credentials: true,
origin(ctx) {
return ctx.get('Origin') || 'localhost';
}
})
);
}
}
5 changes: 3 additions & 2 deletions packages/libs/core/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
port: 7001,
middlewares: []
koa: {
port: 7001
}
};
11 changes: 11 additions & 0 deletions packages/libs/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum ArtusXInjectEnum {
// application-http
Koa = 'ARTUSX_KOA',
Pipeline = 'ARTUSX_PIPELINE',

// redis
Redis = 'ARTUSX_REDIS',

// sequelize
Sequelize = 'ARTUSX_SEQUELIZE'
}
2 changes: 0 additions & 2 deletions packages/plugins/application-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"author": "Suyi <[email protected]>",
"license": "MIT",
"dependencies": {
"@koa/cors": "^5.0.0",
"@koa/router": "^12.0.1",
"koa": "^2.15.0",
"@artus/core": "^2.x",
Expand All @@ -30,7 +29,6 @@
"devDependencies": {
"@artus/tsconfig": "^1.0.1",
"@types/koa": "^2.13.12",
"@types/koa__cors": "^5.0.0",
"@types/koa__router": "^12.0.4",
"@types/node": "^18.11.17",
"tslib": "^2.5.0",
Expand Down
10 changes: 8 additions & 2 deletions packages/plugins/application-http/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default {
port: 7001,
middlewares: []
artusx: {
middlewares: []
},

koa: {
port: 7001,
middlewares: []
}
};
5 changes: 5 additions & 0 deletions packages/plugins/application-http/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum ArtusXInjectEnum {
Koa = 'ARTUSX_KOA',
KoaRouter = 'ARTUSX_KOA_ROUTER',
Pipeline = 'ARTUSX_PIPELINE'
}
9 changes: 9 additions & 0 deletions packages/plugins/application-http/src/koa/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Koa from 'koa';
import { Injectable, ScopeEnum } from '@artus/core';
import { ArtusXInjectEnum } from '../constants';

@Injectable({
id: ArtusXInjectEnum.Koa,
scope: ScopeEnum.SINGLETON
})
export default class KoaApplication extends Koa {}
9 changes: 9 additions & 0 deletions packages/plugins/application-http/src/koa/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Router from '@koa/router';
import { Injectable, ScopeEnum } from '@artus/core';
import { ArtusXInjectEnum } from '../constants';

@Injectable({
id: ArtusXInjectEnum.KoaRouter,
scope: ScopeEnum.SINGLETON
})
export default class KoaRouter extends Router {}
57 changes: 34 additions & 23 deletions packages/plugins/application-http/src/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from 'path';
import Koa from 'koa';
import cors from '@koa/cors';
import Router from '@koa/router';
import { Server } from 'http';

import {
Inject,
Expand All @@ -16,7 +14,7 @@ import { Input, Context } from '@artus/pipeline';

import { CONTROLLER_METADATA, MIDDLEWARE_METADATA, ROUTER_METADATA, WEB_CONTROLLER_TAG } from './decorator';

import HttpPipeline from './pipeline';
import Pipeline from './pipeline';

import type {
ControllerMetadata,
Expand All @@ -26,15 +24,20 @@ import type {
RouteMetadata
} from './types';

import KoaRouter from './koa/router';
import KoaApplication from './koa/application';

import type { Middleware as KoaMiddleware } from 'koa';

export let server: Server;

@LifecycleHookUnit()
export default class ApplicationHttpLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
private readonly app: ArtusApplication;

@Inject()
pipeline: HttpPipeline;

private router = new Router();
pipeline: Pipeline;

get container() {
return this.app.container;
Expand All @@ -44,6 +47,14 @@ export default class ApplicationHttpLifecycle implements ApplicationLifecycle {
return this.app.logger;
}

get router(): KoaRouter {
return this.app.container.get(KoaRouter);
}

get koa(): KoaApplication {
return this.app.container.get(KoaApplication);
}

private registerRoute(
controllerMetadata: ControllerMetadata,
routeMetadataList: RouteMetadata[],
Expand Down Expand Up @@ -104,29 +115,29 @@ export default class ApplicationHttpLifecycle implements ApplicationLifecycle {

@LifecycleHook()
async didLoad() {
const middlewares = this.app.config.middlewares || [];
this.pipeline.use(middlewares);
const ArtusXMiddlewares = this.app.config.artusx.middlewares || [];
this.pipeline.use(ArtusXMiddlewares);

const koaMiddlewares: KoaMiddleware[] = this.app.config.koa.middlewares || [];
koaMiddlewares.map((middleware) => {
this.koa.use(middleware);
});
}

@LifecycleHook()
public async willReady() {
this.loadController();
const port = this.app.config.koa.port || 7001;

const app = new Koa();
const port = this.app.config.port || 7001;

app.use(
cors({
credentials: true,
origin(ctx) {
return ctx.get('Origin') || 'localhost';
}
})
);

app.use(this.router.routes());
app.listen(port, () => {
this.koa.use(this.router.routes());
server = this.koa.listen(port, () => {
this.logger.info(`Server listening on: http://localhost:${port}`);
});
}

@LifecycleHook()
beforeClose() {
this.logger.info('Server closing...');
server?.close();
}
}
8 changes: 6 additions & 2 deletions packages/plugins/application-http/src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Pipeline, Context, Next } from '@artus/pipeline';
import { Injectable, ScopeEnum } from '@artus/core';
import { ArtusXInjectEnum } from './constants';

@Injectable({ scope: ScopeEnum.SINGLETON })
export default class HttpPipeline extends Pipeline {
@Injectable({
id: ArtusXInjectEnum.Pipeline,
scope: ScopeEnum.SINGLETON
})
export default class ArtusPipeline extends Pipeline {
constructor() {
super();

Expand Down
7 changes: 2 additions & 5 deletions packages/plugins/redis/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { Server } from 'http';
import { LifecycleHookUnit, LifecycleHook } from '@artus/core';
import { ApplicationLifecycle } from '@artus/core';
import { ArtusApplication, Inject, ArtusInjectEnum } from '@artus/core';

import { ArtusXInjectEnum } from './constants';
import Redis, { RedisConfig } from './client';

export let server: Server;

@LifecycleHookUnit()
export default class RedisLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

@LifecycleHook()
async willReady() {
const redis = this.app.container.get('ARTUS_REDIS') as Redis;
const redis = this.app.container.get(ArtusXInjectEnum.Redis) as Redis;
await redis.init(this.app.config.redis as RedisConfig);
}
}
3 changes: 2 additions & 1 deletion packages/plugins/redis/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable, ScopeEnum } from '@artus/core';
import { Redis, RedisOptions } from 'ioredis';
import { ArtusXInjectEnum } from './constants';

export interface RedisConfig extends RedisOptions {
host: string;
Expand All @@ -10,7 +11,7 @@ export interface RedisConfig extends RedisOptions {
}

@Injectable({
id: 'ARTUS_REDIS',
id: ArtusXInjectEnum.Redis,
scope: ScopeEnum.SINGLETON
})
export default class Client {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugins/redis/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum ArtusXInjectEnum {
Redis = 'ARTUSX_REDIS'
}
4 changes: 2 additions & 2 deletions packages/plugins/sequelize/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LifecycleHookUnit, LifecycleHook } from '@artus/core';
import { ApplicationLifecycle } from '@artus/core';
import { ArtusApplication, Inject, ArtusInjectEnum } from '@artus/core';

import { ArtusXInjectEnum } from './constants';
import Sequelize, { SequelizeConfig } from './client';

@LifecycleHookUnit()
Expand All @@ -11,7 +11,7 @@ export default class SequelizeLifecycle implements ApplicationLifecycle {

@LifecycleHook()
async willReady() {
const sequelize = this.app.container.get('ARTUS_SEQUELIZE') as Sequelize;
const sequelize = this.app.container.get(ArtusXInjectEnum.Sequelize) as Sequelize;
await sequelize.init(this.app.config.sequelize as SequelizeConfig);
}
}
Loading

0 comments on commit 5821947

Please sign in to comment.