Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
yuki-takei committed Jan 15, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 708c45a commit a02046f
Showing 5 changed files with 17 additions and 42 deletions.
6 changes: 3 additions & 3 deletions apps/app/src/features/opentelemetry/server/node-sdk.ts
Original file line number Diff line number Diff line change
@@ -82,13 +82,13 @@ export const initServiceInstanceId = async(): Promise<void> => {
const instrumentationEnabled = configManager.getConfig('otel:enabled', ConfigSource.env);
if (instrumentationEnabled) {
const { generateNodeSDKConfiguration } = await import('./node-sdk-configuration');
const { getInstance: getGrowiInfoService } = await import('~/server/service/growi-info');
const { growiInfoService } = await import('~/server/service/growi-info');

// get GrowiInfo with additional info
const growiInfo = await getGrowiInfoService().getGrowiInfo();
const growiInfo = await growiInfoService.getGrowiInfo();

const serviceInstanceId = configManager.getConfig('otel:serviceInstanceId')
?? growiInfo.appSiteUrlHashed;
?? growiInfo.serviceInstanceId;

const updatedResource = generateNodeSDKConfiguration(serviceInstanceId);

Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import type Crowi from '~/server/crowi';
import { accessTokenParser } from '~/server/middlewares/access-token-parser';
import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
import { configManager } from '~/server/service/config-manager';
import { getInstance as getGrowiInfoService } from '~/server/service/growi-info';
import { growiInfoService } from '~/server/service/growi-info';
import axios from '~/utils/axios';
import loggerFactory from '~/utils/logger';

@@ -18,7 +18,7 @@ import { StatusType } from '../../../interfaces/questionnaire-answer-status';
import ProactiveQuestionnaireAnswer from '../../models/proactive-questionnaire-answer';
import QuestionnaireAnswer from '../../models/questionnaire-answer';
import QuestionnaireAnswerStatus from '../../models/questionnaire-answer-status';
import { convertToLegacyFormat } from '../../util/convert-to-legacy-format';
import { convertToLegacyFormat, getSiteUrlHashed } from '../../util/convert-to-legacy-format';


const logger = loggerFactory('growi:routes:apiv3:questionnaire');
@@ -62,8 +62,8 @@ module.exports = (crowi: Crowi): Router => {
};

router.get('/orders', accessTokenParser, loginRequired, async(req: AuthorizedRequest, res: ApiV3Response) => {
const growiInfo = await getGrowiInfoService().getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(req.user ?? null, growiInfo.appSiteUrlHashed);
const growiInfo = await growiInfoService.getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(req.user ?? null, getSiteUrlHashed(growiInfo.appSiteUrl));

try {
const questionnaireOrders = await crowi.questionnaireService!.getQuestionnaireOrdersToShow(userInfo, growiInfo, req.user?._id ?? null);
@@ -85,8 +85,8 @@ module.exports = (crowi: Crowi): Router => {
const sendQuestionnaireAnswer = async() => {
const questionnaireServerOrigin = configManager.getConfig('app:questionnaireServerOrigin');
const isAppSiteUrlHashed = configManager.getConfig('questionnaire:isAppSiteUrlHashed');
const growiInfo = await getGrowiInfoService().getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(req.user ?? null, growiInfo.appSiteUrlHashed);
const growiInfo = await growiInfoService.getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(req.user ?? null, getSiteUrlHashed(growiInfo.appSiteUrl));

const proactiveQuestionnaireAnswer: IProactiveQuestionnaireAnswer = {
satisfaction: req.body.satisfaction,
@@ -134,8 +134,8 @@ module.exports = (crowi: Crowi): Router => {
const sendQuestionnaireAnswer = async(user: IUserHasId, answers: IAnswer[]) => {
const questionnaireServerOrigin = crowi.configManager.getConfig('app:questionnaireServerOrigin');
const isAppSiteUrlHashed = configManager.getConfig('questionnaire:isAppSiteUrlHashed');
const growiInfo = await getGrowiInfoService().getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(user, growiInfo.appSiteUrlHashed);
const growiInfo = await growiInfoService.getGrowiInfo(true);
const userInfo = crowi.questionnaireService!.getUserInfo(user, getSiteUrlHashed(growiInfo.appSiteUrl));

const questionnaireAnswer: IQuestionnaireAnswer = {
growiInfo,
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ function isLegacy<T extends { growiInfo: any }>(data: T): data is IHasGrowiAppIn
return !('additionalInfo' in data.growiInfo);
}

function getSiteUrlHashed(_siteUrl: string | undefined): string {
export function getSiteUrlHashed(_siteUrl: string | undefined): string {
const siteUrl = _siteUrl ?? '[The site URL is not set. Please set it!]';
const hasher = crypto.createHash('sha256');
hasher.update(siteUrl);
34 changes: 5 additions & 29 deletions apps/app/src/server/service/growi-info/growi-info.integ.ts
Original file line number Diff line number Diff line change
@@ -8,13 +8,11 @@ import { configManager } from '~/server/service/config-manager';

import type Crowi from '../../crowi';

import type { GrowiInfoService } from './growi-info';
import { serviceFactory } from './growi-info';
import { growiInfoService } from './growi-info';

describe('GrowiInfoService', () => {
const appVersion = pkg.version;

let growiInfoService: GrowiInfoService;
let User;

beforeAll(async() => {
@@ -43,13 +41,8 @@ describe('GrowiInfoService', () => {
});
}
}),
appService: {
getSiteUrl: () => 'http://growi.test.jp',
},
});

growiInfoService = serviceFactory(crowiMock);

const userModelFactory = (await import('~/server/models/user')).default;
User = userModelFactory(crowiMock);

@@ -63,20 +56,18 @@ describe('GrowiInfoService', () => {

assert(growiInfo != null);

expect(growiInfo.appSiteUrlHashed).toBeTruthy();
expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
expect(growiInfo.osInfo?.type).toBeTruthy();
expect(growiInfo.osInfo?.platform).toBeTruthy();
expect(growiInfo.osInfo?.arch).toBeTruthy();
expect(growiInfo.osInfo?.totalmem).toBeTruthy();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (growiInfo as any).appSiteUrlHashed;
delete growiInfo.osInfo;
delete (growiInfo as any).osInfo;

expect(growiInfo).toEqual({
version: appVersion,
appSiteUrl: 'http://growi.test.jp',
serviceInstanceId: '',
type: 'on-premise',
wikiType: 'closed',
deploymentType: 'growi-docker-compose',
@@ -96,20 +87,18 @@ describe('GrowiInfoService', () => {
// assert
assert(growiInfo != null);

expect(growiInfo.appSiteUrlHashed).toBeTruthy();
expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
expect(growiInfo.osInfo?.type).toBeTruthy();
expect(growiInfo.osInfo?.platform).toBeTruthy();
expect(growiInfo.osInfo?.arch).toBeTruthy();
expect(growiInfo.osInfo?.totalmem).toBeTruthy();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (growiInfo as any).appSiteUrlHashed;
delete growiInfo.osInfo;
delete (growiInfo as any).osInfo;

expect(growiInfo).toEqual({
version: appVersion,
appSiteUrl: 'http://growi.test.jp',
serviceInstanceId: '',
type: 'on-premise',
wikiType: 'closed',
deploymentType: 'growi-docker-compose',
@@ -124,18 +113,5 @@ describe('GrowiInfoService', () => {
});
});

describe('When url hash settings is on', () => {
beforeEach(async() => {
process.env.QUESTIONNAIRE_IS_APP_SITE_URL_HASHED = 'true';
await configManager.loadConfigs();
});

test('Should return app url string', async() => {
const growiInfo = await growiInfoService.getGrowiInfo();
expect(growiInfo.appSiteUrl).toBeUndefined();
expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
expect(growiInfo.appSiteUrlHashed).toBeTruthy();
});
});
});
});
1 change: 0 additions & 1 deletion apps/app/src/server/service/growi-info/growi-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import crypto from 'crypto';
import * as os from 'node:os';

import type { IGrowiInfo } from '@growi/core';

0 comments on commit a02046f

Please sign in to comment.