Skip to content

Commit 1b2b382

Browse files
skyoctmaslow
authored andcommitted
feat: support website custom domain (#138)
1 parent 0e27b70 commit 1b2b382

File tree

5 files changed

+101
-22
lines changed

5 files changed

+101
-22
lines changed

packages/gateway-controller/src/support/apisix-gateway-init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function initOssSubDomainRoute() {
115115
},
116116
plugins: {
117117
'proxy-rewrite': {
118-
regex_uri: ["/", "/index.html"]
118+
regex_uri: ["/$", "/index.html"]
119119
}
120120
}
121121
}

packages/gateway-controller/src/support/apisix-gateway.ts

+46
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ export class ApiSixGateway implements GatewayInterface {
1717
public async create(route: IRouteData) {
1818
if (route.type === RouteType.APP) {
1919
return await createAppRoute(this.baseUrl, route)
20+
} else if (route.type === RouteType.WEBSITE_CUSTOM) {
21+
return await createWebsiteCustomRoute(this.baseUrl, route)
2022
}
2123
}
2224

2325
public async delete(route: IRouteData) {
2426
if (route.type === RouteType.APP) {
2527
return await deleteAppRoute(this.baseUrl, route)
28+
} else if (route.type === RouteType.WEBSITE_CUSTOM) {
29+
return await deleteWebsiteCustomRoute(this.baseUrl, route)
2630
}
2731
}
2832

@@ -58,4 +62,46 @@ async function createAppRoute(url: string, route: IRouteData) {
5862

5963
async function deleteAppRoute(url: string, route: IRouteData) {
6064
return await ApiSixHttpUtils.delete(url, route.appid)
65+
}
66+
67+
68+
async function createWebsiteCustomRoute(url: string, route: IRouteData) {
69+
if (route.domain.length !== 2) {
70+
return false
71+
}
72+
let hosts = null, node = null
73+
if (Config.SERVICE_DRIVER == 'docker') {
74+
hosts = route.domain[0]
75+
node = 'oss:9000'
76+
}
77+
78+
let data = {
79+
name: route.name,
80+
uri: '/*',
81+
hosts: hosts,
82+
priority: 9, // 设置优先级较高点
83+
upstream: {
84+
pass_host: 'rewrite',
85+
upstream_host: route.domain[1] + '.' + Config.DEPLOY_OSS_DOMAIN,
86+
type: 'roundrobin',
87+
nodes: {
88+
[node]: 1
89+
}
90+
},
91+
timeout: {
92+
connect: 600,
93+
send: 600,
94+
read: 600,
95+
},
96+
plugins: {
97+
'proxy-rewrite': {
98+
regex_uri: ["/$", "/index.html"]
99+
}
100+
}
101+
}
102+
return await ApiSixHttpUtils.put(url, route.website_id, data)
103+
}
104+
105+
async function deleteWebsiteCustomRoute(url: string, route: IRouteData) {
106+
return await ApiSixHttpUtils.delete(url, route.website_id)
61107
}

packages/gateway-controller/src/support/route.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum RouteStatus {
1313

1414
export enum RouteType {
1515
APP = 'app',
16-
OSS_CUSTOM = 'oss_custom'
16+
WEBSITE_CUSTOM = 'website_custom'
1717
}
1818

1919

@@ -23,6 +23,7 @@ export interface IRouteData {
2323
appid: string
2424
type: RouteType
2525
website_id: string
26+
domain: string[]
2627
status: RouteStatus
2728
created_by: ObjectId
2829
created_at?: Date
@@ -63,4 +64,4 @@ export async function updateRouteStatus(appid: string, from: RouteStatus, to: Ro
6364
})
6465

6566
return r.modifiedCount
66-
}
67+
}

packages/system-server/src/handler/website/domain-bind.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { checkPermission } from "../../support/permission"
66
import { IApplicationData } from "../../support/application"
77
import { DatabaseAgent } from "../../db"
88
import { WebsiteActionDef } from "../../actions"
9+
import {createWebsiteCustomRoute} from "../../support/route";
910

1011

1112
/**
@@ -70,6 +71,11 @@ export async function handleBindDomain(req: Request, res: Response) {
7071
$push: { domain: domain }
7172
}
7273
)
74+
const domainList = [domain, website.appid + '-' + website.bucket_name]
75+
const rt = await createWebsiteCustomRoute('website-custom', app.appid, website_id, domainList, uid)
76+
if (!rt) {
77+
return res.send({ code: 'ROUTE CREATE FAILED', error: "route create failed" })
78+
}
7379

7480
return res.send({ data: r.modifiedCount })
75-
}
81+
}

packages/system-server/src/support/route.ts

+44-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export enum RouteStatus {
1414

1515
export enum RouteType {
1616
APP = 'app',
17-
OSS_CUSTOM = 'oss_custom'
17+
WEBSITE_CUSTOM = 'website_custom'
1818
}
1919

2020

@@ -24,6 +24,7 @@ export interface IRouteData {
2424
appid: string
2525
type: RouteType
2626
website_id: string
27+
domain: string[]
2728
status: RouteStatus
2829
created_by: ObjectId
2930
created_at?: Date
@@ -37,6 +38,7 @@ export async function createApplicationRoute(name: string, appid: string, uid: a
3738
appid: appid,
3839
type: RouteType.APP,
3940
website_id: null,
41+
domain: [],
4042
status: RouteStatus.PREPARED_CREATE,
4143
created_by: new ObjectId(uid),
4244
created_at: now,
@@ -68,28 +70,52 @@ export async function deleteApplicationRoute(appid: string) {
6870
}
6971

7072

71-
export async function createOssCustomRoute(name: string, appid: string, websiteId: string, uid: any): Promise<Boolean> {
72-
const now = new Date()
73-
let data: IRouteData = {
74-
name: name,
73+
export async function createWebsiteCustomRoute(name: string, appid: string, websiteId: string, domain: string[], uid: any): Promise<Boolean> {
74+
const route = await DatabaseAgent.db.collection(CN_ROUTES).findOne({
7575
appid: appid,
76-
type: RouteType.OSS_CUSTOM,
77-
website_id: websiteId,
78-
status: RouteStatus.PREPARED_CREATE,
79-
created_by: new ObjectId(uid),
80-
created_at: now,
81-
updated_at: now,
82-
}
83-
const ret = await DatabaseAgent.db.collection(CN_ROUTES)
84-
.insertOne(data as any)
85-
if (!ret.insertedId) {
86-
logger.error('create route task successful: {}', appid)
87-
return false
76+
websiteId: websiteId
77+
})
78+
const now = new Date()
79+
if (route) {
80+
const ret = await DatabaseAgent.db.collection(CN_ROUTES).updateOne({
81+
appid: appid,
82+
websiteId: websiteId
83+
}, {
84+
$set: {
85+
domain: domain,
86+
status: RouteStatus.PREPARED_CREATE,
87+
created_at: now,
88+
updated_at: now,
89+
}
90+
})
91+
if (!ret.modifiedCount) {
92+
logger.error('update route task failed: {}', appid)
93+
return false
94+
}
95+
} else {
96+
let data: IRouteData = {
97+
name: name,
98+
appid: appid,
99+
type: RouteType.WEBSITE_CUSTOM,
100+
website_id: websiteId,
101+
domain: domain,
102+
status: RouteStatus.PREPARED_CREATE,
103+
created_by: new ObjectId(uid),
104+
created_at: now,
105+
updated_at: now,
106+
}
107+
const ret = await DatabaseAgent.db.collection(CN_ROUTES)
108+
.insertOne(data as any)
109+
if (!ret.insertedId) {
110+
logger.error('create route task failed: {}', appid)
111+
return false
112+
}
88113
}
114+
89115
return true
90116
}
91117

92-
export async function deleteOssCustomRoute(appid: string, websiteId: string) {
118+
export async function deleteWebsiteCustomRoute(appid: string, websiteId: string) {
93119
const ret = await DatabaseAgent.db.collection<IRouteData>(CN_ROUTES)
94120
.updateOne({
95121
appid: appid,

0 commit comments

Comments
 (0)