Skip to content

Commit

Permalink
Merge pull request #43 from donghquinn/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
donghquinn authored Feb 6, 2024
2 parents 688fa08 + 49202ae commit 796e5e0
Show file tree
Hide file tree
Showing 37 changed files with 543 additions and 385 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions package-deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"schema": "prisma generate"
},
"dependencies": {
"@nestjs/common": "^10.3.0",
"@nestjs/core": "^10.3.0",
"@nestjs/common": "^10.3.1",
"@nestjs/core": "^10.3.1",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.0",
"@prisma/client": "^5.8.1",
"@nestjs/platform-express": "^10.3.1",
"@prisma/client": "^5.9.1",
"cookie-parser": "^1.4.6",
"date-fns": "^2.30.0",
"express": "^4.18.2",
Expand All @@ -24,7 +24,7 @@
"node-fetch": "^3.3.2",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"prisma": "^5.8.1",
"prisma": "^5.9.1",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.8.1",
"winston": "^3.11.0",
Expand All @@ -34,7 +34,7 @@
"devDependencies": {
"@types/cookie-parser": "^1",
"@types/express": "^4.17.21",
"@types/node": "^20.11.2",
"@types/node": "^20.11.16",
"@types/passport": "^0",
"@types/passport-jwt": "^4",
"@typescript-eslint/eslint-plugin": "^6.19.0",
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"db:update": "prisma db push"
},
"dependencies": {
"@nestjs/common": "^10.3.0",
"@nestjs/core": "^10.3.0",
"@nestjs/common": "^10.3.1",
"@nestjs/core": "^10.3.1",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.0",
"@prisma/client": "^5.8.1",
"@nestjs/platform-express": "^10.3.1",
"@prisma/client": "^5.9.1",
"cookie-parser": "^1.4.6",
"date-fns": "^2.30.0",
"express": "^4.18.2",
Expand All @@ -29,7 +29,7 @@
"node-fetch": "^3.3.2",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"prisma": "^5.8.1",
"prisma": "^5.9.1",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.8.1",
"winston": "^3.11.0",
Expand All @@ -39,7 +39,7 @@
"devDependencies": {
"@types/cookie-parser": "^1",
"@types/express": "^4.17.21",
"@types/node": "^20.11.2",
"@types/node": "^20.11.16",
"@types/passport": "^0",
"@types/passport-jwt": "^4",
"@typescript-eslint/eslint-plugin": "^6.19.0",
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/geek/geek.ctl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { Body, Controller, Get, Headers, Post, Query } from '@nestjs/common';
import { hadaNewsStarValidator, hadaNewsValidator } from '@validators/hada.validator';
import { SetErrorResponse, SetResponse } from 'dto/response.dto';
import { GeekProvider } from 'providers/news/geek/geek.pvd';
Expand All @@ -23,11 +23,11 @@ export class GeekController {
}

@Post('/star')
async giveStarNews(@Body() request: StarRequest) {
async giveStarNews(@Body() request: StarRequest, @Headers('Authorization') clientUuid: string) {
try {
const { uuid } = await hadaNewsStarValidator(request);
const { uuid: postUuid } = await hadaNewsStarValidator(request);

const result = await this.geek.giveStar(uuid);
const result = await this.geek.giveStar(postUuid, clientUuid);

return new SetResponse(200, { result });
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/hacker/hacker.ctl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { Body, Controller, Get, Post, Headers, Query } from '@nestjs/common';
import { hackerNewsStarValidator, hackerNewsValidator } from '@validators/hacker.validator';
import { SetErrorResponse, SetResponse } from 'dto/response.dto';
import { HackersNewsProvider } from 'providers/news/hacker/hacker.pvd';
Expand Down Expand Up @@ -34,11 +34,11 @@ export class HackerController {
}

@Post('/star')
async giveStarNews(@Body() request: StarRequest) {
async giveStarNews(@Body() request: StarRequest, @Headers('Authorization') clientUuid: string) {
try {
const { uuid } = await hackerNewsStarValidator(request);
const { uuid: PostUuid } = await hackerNewsStarValidator(request);

const result = await this.hacker.giveStar(uuid);
const result = await this.hacker.giveStar(PostUuid, clientUuid);

return new SetResponse(200, { result });
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/machine/machine.ctl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MachineLearningProvider } from 'providers/news/ml/machine.pvd';
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { Body, Controller, Get, Post, Headers, Query } from '@nestjs/common';
import { machineLearningValidator, mlNewsStarValidator } from '@validators/ml.validator';
import { SetErrorResponse, SetResponse } from 'dto/response.dto';
import { StarRequest } from 'types/request.type';
Expand All @@ -23,11 +23,11 @@ export class MachineLearningController {
}

@Post('/star')
async giveStarNews(@Body() request: StarRequest) {
async giveStarNews(@Body() request: StarRequest, @Headers('Authorization') clientUuid: string) {
try {
const { uuid } = await mlNewsStarValidator(request);
const { uuid: postUuid } = await mlNewsStarValidator(request);

const result = await this.mlNews.giveStar(uuid);
const result = await this.mlNews.giveStar(postUuid, clientUuid);

return new SetResponse(200, { result });
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/errors/manager.error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export class StarError extends Error {
export class AccountError extends Error {
type: string;

constructor(type: string, message: string, cause?: Error) {
super(message);

this.type = type;

this.name = '[Star Error]';
this.name = '[Account Error]';

this.cause = cause;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/client/account.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { AccountManager } from 'providers/client/account-manager.pvd';
import { AccountManager } from 'providers/auth/account-manager.pvd';

@Module({
providers: [AccountManager],
Expand Down
3 changes: 2 additions & 1 deletion src/modules/geek/geek.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GeekController } from '@controllers/geek/geek.ctl';
import { AccountManagerModule } from '@modules/client/account.module';
import { Module } from '@nestjs/common';
import { GeekProvider } from 'providers/news/geek/geek.pvd';
import { GeekPrismaModule } from './geek-prisma.module';

@Module({
controllers: [GeekController],
imports: [GeekPrismaModule],
imports: [GeekPrismaModule, AccountManagerModule],
providers: [GeekProvider],
})
export class GeekModule {}
3 changes: 2 additions & 1 deletion src/modules/hacker/hacker.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { HackerController } from '@controllers/hacker/hacker.ctl';
import { AccountManagerModule } from '@modules/client/account.module';
import { Module } from '@nestjs/common';
import { HackersNewsProvider } from 'providers/news/hacker/hacker.pvd';
import { HackerPrismaModule } from './hacker-prisma.module';

@Module({
controllers: [HackerController],
imports: [HackerPrismaModule],
imports: [HackerPrismaModule, AccountManagerModule],
providers: [HackersNewsProvider],
})
export class HackerModule {}
3 changes: 2 additions & 1 deletion src/modules/ml/machine.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MachineLearningController } from '@controllers/machine/machine.ctl';
import { AccountManagerModule } from '@modules/client/account.module';
import { Module } from '@nestjs/common';
import { MachineLearningProvider } from 'providers/news/ml/machine.pvd';
import { MlPrismaModule } from './ml-prisma.module';

@Module({
controllers: [MachineLearningController],
imports: [MlPrismaModule],
imports: [MlPrismaModule, AccountManagerModule],
providers: [MachineLearningProvider],
})
export class MachineLearningNewsModule {}
157 changes: 157 additions & 0 deletions src/providers/auth/account-manager.pvd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { Injectable } from '@nestjs/common';
import { ClientLogger, ManagerLogger } from '@utils/logger.util';
import { ClientLoginMapItem, ClientLoginMapKey } from 'types/client.type';

@Injectable()
export class AccountManager {
private userMap: WeakMap<ClientLoginMapKey, ClientLoginMapItem>;

private userKeys: Array<ClientLoginMapKey>;

constructor() {
this.userKeys = [];
this.userMap = new WeakMap();
}

public setLoginUser(clientUuid: string, email: string, password?: string) {
const isLogined = this.getItem(clientUuid);

ManagerLogger.debug('[ACCOUNT] Searching User Info: %o', {
isLogined,
map: this.userMap,
clientUuid,
});

if (!isLogined) {
const clientKey = { uuid: clientUuid };

this.userKeys.push(clientKey);
this.setItem(email, clientKey, password);

if (this.userKeys.length >= 5000) {
ClientLogger.debug('[AccountManager] keyList maximum reached. shift()');
this.userKeys.shift();
}

ClientLogger.info('[ACCOUNT] Set Finished');

return clientUuid;
}

ManagerLogger.debug('[ACCOUNT] Found User Key: %o', {
clientUuid,
map: this.userMap,
});

const interval = 1000 * 60 * 10;

const timer = setInterval(() => {
const isExsit = this.getItem(clientUuid);

if (!isExsit) {
ManagerLogger.info('[ACCOUNT] It is not existing user. Clear Interval.');

ManagerLogger.debug('[ACCOUNT] Client Map Inspection: %o', {
isExsit,
clientUuid,
map: this.userMap,
});

clearInterval(timer);
} else {
ManagerLogger.info('[ACCOUNT] Expiration time. Delete user.');

ManagerLogger.debug('[ACCOUNT] Client Map Inspection: %o', {
clientUuid,
map: this.userMap,
});

this.deleteItem(clientUuid);
}
}, interval);

return clientUuid;
}

public deleteLogoutUser(clientUuid: string) {
ManagerLogger.debug('[ACCOUNT] Client Data: %o', {
clientUuid,
map: this.userMap,
});

const isExist = this.getItem(clientUuid);

if (!isExist) {
ManagerLogger.info('[ACCOUNT] Not Matchin Data found. Ignore.');

return false;
}

this.deleteItem(clientUuid);

ManagerLogger.debug('[ACCOUNT] Client Map Delete Inspection: %o', {
clientUuid,
map: this.userMap,
});

return true;
}

public deleteItem(clientUuid: string) {
const index = this.userKeys.findIndex((item) => item.uuid === clientUuid);

if (index > -1) this.userKeys.splice(index, 1);

ClientLogger.debug('[ACCOUNT] Delete Finished: %o', {
clientUuid,
index,
keyList: this.userKeys,
map: this.userMap,
});
}

public setItem(email: string, clientKey: ClientLoginMapKey, password?: string) {
return this.userMap.set(clientKey, { email, password });
}

public getItem(clientUuid: string) {
const key = this.userKeys.find((item) => item.uuid === clientUuid);

if (!key) return null;

return this.userMap.get(key) as ClientLoginMapItem;
}

// public stop() {
// if (this.isListening) {
// this.isListening = false;

// // Account lock 이 전부 풀릴 때까지 대기
// for (;;) {
// // 등록된 주소와 mutex 조회
// const items = this.keyList
// .map((item) => ({ address: item.address, ...this.getItem(item.address) }))
// .filter((item) => item.mutex !== null);

// // 등록된 주소가 없을 경우 안전하게 끝낼 수 있음
// if (!items.length) break;

// // 그렇지 않을 경우 락 여부를 확인해야 함
// for (let i = 0; i < items.length; i += 1) {
// const item = items[i];

// if (item?.mutex?.isLocked()) {
// Logger.info('[AccountManager] Waiting for address in use(locked): %o', item.address);

// // 1초 대기
// // SIGTERM 핸들러는 비동기 코드 resolve 가 되지 않으므로 then() 으로 기다림
// setTimeout(1000).then(() =>
// Logger.info ('[AccountManager] Waited 1 second for address: %o', item.address ));
// }
// }
// }

// TxLogger.info('[AccountManager] Address lock cleared, safe to shutdown.');
// }
// }
}
Loading

0 comments on commit 796e5e0

Please sign in to comment.