Skip to content

Commit

Permalink
refactor: add more fields to region configs (#752)
Browse files Browse the repository at this point in the history
* refactor: add more fields to region configs

* enforce filename style

* add censorship config

* add config hash
  • Loading branch information
hyrious authored and aderan committed Aug 17, 2023
1 parent 93abd3f commit 801183f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions config/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ agora:
secret:

whiteboard:
app_id:
access_key:
secret_access_key:
convert_region:
Expand Down
1 change: 1 addition & 0 deletions config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ agora:
secret:

whiteboard:
app_id: "test/flat-server"
access_key: "test"
secret_access_key: "test"
convert_region: "cn-hz"
Expand Down
1 change: 1 addition & 0 deletions src/constants/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const JWT = {
};

export const Whiteboard = {
appId: config.whiteboard.app_id,
accessKey: config.whiteboard.access_key,
secretAccessKey: config.whiteboard.secret_access_key,
convertRegion: config.whiteboard.convert_region,
Expand Down
4 changes: 4 additions & 0 deletions src/utils/ParseConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yaml from "js-yaml";
import fs from "fs";
import path from "path";
import crypto from "crypto";

const configDirPath =
process.env.IS_TEST === "yes"
Expand All @@ -24,6 +25,8 @@ const configPath = (() => {

const yamlContent = fs.readFileSync(configPath, "utf8");

export const configHash = crypto.createHash("md5").update(yamlContent).digest("hex");

export const config = yaml.load(yamlContent) as Config;

type Config = {
Expand Down Expand Up @@ -195,6 +198,7 @@ type Config = {
};
};
whiteboard: {
app_id: string;
access_key: string;
secret_access_key: string;
convert_region: "cn-hz" | "us-sv" | "sg" | "in-mum" | "gb-lon";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import test from "ava";
import { HelperAPI } from "../../../__tests__/helpers/api";
import { regionConfigs, regionConfigsRouters } from "../regionConfigs";
import { regionConfigs, regionConfigsRouters } from "../region-configs";
import { initializeDataSource } from "../../../__tests__/helpers/db/test-hooks";

const namespace = "v2.controllers.region.configs";
initializeDataSource(test, namespace);

test(`${namespace} - fetch region configs`, async ava => {

const helperAPI = new HelperAPI();
await helperAPI.import(regionConfigsRouters, regionConfigs);

Expand All @@ -16,8 +15,8 @@ test(`${namespace} - fetch region configs`, async ava => {
method: "GET",
url: "/v2/region/configs",
});
const s = resp.payload
console.log(s)
const s = resp.payload;
console.log(s);
ava.is(resp.statusCode, 200);
const data = (await resp.json()).data;
ava.true(data.login.wechatWeb);
Expand All @@ -31,5 +30,4 @@ test(`${namespace} - fetch region configs`, async ava => {
ava.is(data.server.region, "CN");
ava.is(data.server.regionCode, 1);
}

});
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import { Server } from "../../../utils/registryRoutersV2";
import { Type } from "@sinclair/typebox";
import { successJSON } from "../internal/utils/response-json";

import { configHash } from "../../../utils/ParseConfig";
import {
Server as ServerConfig, WeChat, Github, Google, Apple, AgoraLogin,
PhoneSMS, Whiteboard, Agora, CloudStorage
Server as ServerConfig,
WeChat,
Github,
Google,
Apple,
AgoraLogin,
PhoneSMS,
Whiteboard,
Agora,
CloudStorage,
StorageService,
Censorship,
} from "../../../constants/Config";

type regionConfigsResponseSchema = {
hash: string;
login: {
wechatWeb: boolean;
wechatMobile: boolean;
Expand All @@ -19,29 +31,49 @@ type regionConfigsResponseSchema = {
agora: boolean;
sms: boolean;
smsForce: boolean;
},
};
server: {
region: string;
regionCode: number;
env: string;
},
};
whiteboard: {
appId: string;
convertRegion: string;
},
};
agora: {
clientId: string;
appId: string;
screenshot: boolean;
messageNotification: boolean;
},
};
github: {
clientId: string;
};
wechat: {
webAppId: string;
mobileAppId: string;
};
google: {
clientId: string;
};
cloudStorage: {
singleFileSize: number;
totleSize: number;
totalSize: number;
allowFileSuffix: Array<String>;
accessKey: string;
};
censorship: {
video: boolean;
voice: boolean;
text: boolean;
};
};

// export for unit test
export const regionConfigs = async (): Promise<ResponseSuccess<regionConfigsResponseSchema>> => {
return successJSON({
hash: configHash,
login: {
wechatWeb: WeChat.web.enable,
wechatMobile: WeChat.mobile.enable,
Expand All @@ -58,17 +90,36 @@ export const regionConfigs = async (): Promise<ResponseSuccess<regionConfigsResp
env: ServerConfig.env,
},
whiteboard: {
convertRegion: Whiteboard.convertRegion
appId: Whiteboard.appId,
convertRegion: Whiteboard.convertRegion,
},
agora: {
clientId: AgoraLogin.clientId,
appId: Agora.appId,
screenshot: Agora.screenshot.enable,
messageNotification: Agora.messageNotification.enable,
},
github: {
clientId: Github.clientId,
},
wechat: {
webAppId: WeChat.web.appId,
mobileAppId: WeChat.mobile.appId,
},
google: {
clientId: Google.clientId,
},
cloudStorage: {
singleFileSize: CloudStorage.singleFileSize,
totleSize: CloudStorage.totalSize,
totalSize: CloudStorage.totalSize,
allowFileSuffix: CloudStorage.allowFileSuffix,
}
accessKey: StorageService.oss.accessKey,
},
censorship: {
video: Censorship.video.enable,
voice: Censorship.voice.enable,
text: Censorship.text.enable,
},
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/v2/controllers/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { applicationRouters } from "./application/routes";
import { roomRouters } from "./room/routes";
import { oauthRouters } from "./auth2/routes";
import { tempPhotoRouters } from "./temp-photo/routes";
import { regionConfigsRouters } from "./configs/regionConfigs";
import { regionConfigsRouters } from "./configs/region-configs";
import { registerRouters } from "./register/routes";
import { loginRouters } from "./login/routes";
import { resetRouters } from "./reset/routes";
Expand Down

0 comments on commit 801183f

Please sign in to comment.