core: 支持绑定自定义域名

This commit is contained in:
undefined committed 2021-01-22 22:48:53 +08:00
1 parent c0efda36cd
commit 62a5bc319b
6 files changed
+37 -10

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.15.30",
"version": "2.15.31",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
+1 -1
View File
@@ -310,7 +310,7 @@ export interface DomainDoc extends Dictionary<any> {
bulletin: string,
pidCounter: number,
join?: any,
hostname?: string,
host?: string,
}
// Message
+11
View File
@@ -53,6 +53,16 @@ export async function get(domainId: string): Promise<DomainDoc | null> {
return result;
}
export async function getByHost(host: string): Promise<DomainDoc | null> {
const query: FilterQuery<DomainDoc> = { host };
await bus.serial('domain/before-get', query);
console.log(query);
const result = await coll.findOne(query);
console.log(result);
await bus.serial('domain/get', result);
return result;
}
export function getMulti(query: FilterQuery<DomainDoc> = {}) {
return coll.find(query);
}
@@ -212,6 +222,7 @@ global.Hydro.model.domain = {
add,
inc,
get,
getByHost,
edit,
getMulti,
getList,
+1
View File
@@ -108,6 +108,7 @@ DomainSetting(
Setting('setting_domain', 'gravatar', null, '', 'text', 'gravatar', 'Will be used as the domain icon.'),
Setting('setting_domain', 'bulletin', null, '', 'markdown', 'Bulletin'),
Setting('setting_storage', 'pidCounter', null, 0, 'number', 'Problem ID Counter', null, FLAG_HIDDEN | FLAG_DISABLED),
Setting('setting_storage', 'host', null, '', 'text', 'Custom host', null, FLAG_HIDDEN | FLAG_DISABLED),
);
DomainUserSetting(
+22 -7
View File
@@ -302,12 +302,20 @@ export class HandlerCommon {
else query[key] = kwargs.query[key].toString();
}
try {
if (this.domainId !== 'system' || args.domainId) {
name += '_with_domainId';
args.domainId = args.domainId || this.domainId;
}
const { anchor } = args;
res = router.url(name, args, { query });
if (!this.domain.host) {
if (this.domainId !== 'system' || args.domainId) {
name += '_with_domainId';
args.domainId = args.domainId || this.domainId;
}
res = router.url(name, args, { query });
} else {
if ((this.request.path || '').startsWith('/d/')) {
name += '_with_domainId';
args.domainId = args.domainId || this.domainId;
}
res = `//${this.domain.host}${router.url(name, args, { query })}`;
}
if (anchor) return `${res}#${anchor}`;
} catch (e) {
logger.warn(e.message);
@@ -431,13 +439,20 @@ export class Handler extends HandlerCommon {
}
async init({ domainId }) {
[this.domain] = await Promise.all([
const [absoluteDomain, inferDomain] = await Promise.all([
domain.get(domainId),
domain.getByHost(this.request.host),
this.limitRate('global', 10, 100),
this.getSession(),
this.getBdoc(),
]);
if (!this.domain) {
if (inferDomain) {
this.domainId = inferDomain._id;
this.args.domainId = inferDomain._id;
this.domain = inferDomain;
domainId = inferDomain._id;
} else if (absoluteDomain) this.domain = absoluteDomain;
else {
this.args.domainId = 'system';
this.user = await user.getById('system', this.session.uid);
if (!this.user) this.user = await user.getById('system', 0);