Skip to content

Commit

Permalink
feat: support website custom domain (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jun 18, 2022
1 parent b7ca2f7 commit e2c7196
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function initOssSubDomainRoute() {
},
plugins: {
'proxy-rewrite': {
regex_uri: ["/", "/index.html"]
regex_uri: ["/$", "/index.html"]
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions packages/gateway-controller/src/support/apisix-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export class ApiSixGateway implements GatewayInterface {
public async create(route: IRouteData) {
if (route.type === RouteType.APP) {
return await createAppRoute(this.baseUrl, route)
} else if (route.type === RouteType.WEBSITE_CUSTOM) {
return await createWebsiteCustomRoute(this.baseUrl, route)
}
}

public async delete(route: IRouteData) {
if (route.type === RouteType.APP) {
return await deleteAppRoute(this.baseUrl, route)
} else if (route.type === RouteType.WEBSITE_CUSTOM) {
return await deleteWebsiteCustomRoute(this.baseUrl, route)
}
}

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

async function deleteAppRoute(url: string, route: IRouteData) {
return await ApiSixHttpUtils.delete(url, route.appid)
}


async function createWebsiteCustomRoute(url: string, route: IRouteData) {
if (route.domain.length !== 2) {
return false
}
let hosts = null, node = null
if (Config.SERVICE_DRIVER == 'docker') {
hosts = route.domain[0]
node = 'oss:9000'
}

let data = {
name: route.name,
uri: '/*',
hosts: hosts,
priority: 9, // 设置优先级较高点
upstream: {
pass_host: 'rewrite',
upstream_host: route.domain[1] + '.' + Config.DEPLOY_OSS_DOMAIN,
type: 'roundrobin',
nodes: {
[node]: 1
}
},
timeout: {
connect: 600,
send: 600,
read: 600,
},
plugins: {
'proxy-rewrite': {
regex_uri: ["/$", "/index.html"]
}
}
}
return await ApiSixHttpUtils.put(url, route.website_id, data)
}

async function deleteWebsiteCustomRoute(url: string, route: IRouteData) {
return await ApiSixHttpUtils.delete(url, route.website_id)
}
5 changes: 3 additions & 2 deletions packages/gateway-controller/src/support/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export enum RouteStatus {

export enum RouteType {
APP = 'app',
OSS_CUSTOM = 'oss_custom'
WEBSITE_CUSTOM = 'website_custom'
}


Expand All @@ -23,6 +23,7 @@ export interface IRouteData {
appid: string
type: RouteType
website_id: string
domain: string[]
status: RouteStatus
created_by: ObjectId
created_at?: Date
Expand Down Expand Up @@ -63,4 +64,4 @@ export async function updateRouteStatus(appid: string, from: RouteStatus, to: Ro
})

return r.modifiedCount
}
}
8 changes: 7 additions & 1 deletion packages/system-server/src/handler/website/domain-bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkPermission } from "../../support/permission"
import { IApplicationData } from "../../support/application"
import { DatabaseAgent } from "../../db"
import { WebsiteActionDef } from "../../actions"
import {createWebsiteCustomRoute} from "../../support/route";


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

return res.send({ data: r.modifiedCount })
}
}
62 changes: 44 additions & 18 deletions packages/system-server/src/support/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export enum RouteStatus {

export enum RouteType {
APP = 'app',
OSS_CUSTOM = 'oss_custom'
WEBSITE_CUSTOM = 'website_custom'
}


Expand All @@ -24,6 +24,7 @@ export interface IRouteData {
appid: string
type: RouteType
website_id: string
domain: string[]
status: RouteStatus
created_by: ObjectId
created_at?: Date
Expand All @@ -37,6 +38,7 @@ export async function createApplicationRoute(name: string, appid: string, uid: a
appid: appid,
type: RouteType.APP,
website_id: null,
domain: [],
status: RouteStatus.PREPARED_CREATE,
created_by: new ObjectId(uid),
created_at: now,
Expand Down Expand Up @@ -68,28 +70,52 @@ export async function deleteApplicationRoute(appid: string) {
}


export async function createOssCustomRoute(name: string, appid: string, websiteId: string, uid: any): Promise<Boolean> {
const now = new Date()
let data: IRouteData = {
name: name,
export async function createWebsiteCustomRoute(name: string, appid: string, websiteId: string, domain: string[], uid: any): Promise<Boolean> {
const route = await DatabaseAgent.db.collection(CN_ROUTES).findOne({
appid: appid,
type: RouteType.OSS_CUSTOM,
website_id: websiteId,
status: RouteStatus.PREPARED_CREATE,
created_by: new ObjectId(uid),
created_at: now,
updated_at: now,
}
const ret = await DatabaseAgent.db.collection(CN_ROUTES)
.insertOne(data as any)
if (!ret.insertedId) {
logger.error('create route task successful: {}', appid)
return false
websiteId: websiteId
})
const now = new Date()
if (route) {
const ret = await DatabaseAgent.db.collection(CN_ROUTES).updateOne({
appid: appid,
websiteId: websiteId
}, {
$set: {
domain: domain,
status: RouteStatus.PREPARED_CREATE,
created_at: now,
updated_at: now,
}
})
if (!ret.modifiedCount) {
logger.error('update route task failed: {}', appid)
return false
}
} else {
let data: IRouteData = {
name: name,
appid: appid,
type: RouteType.WEBSITE_CUSTOM,
website_id: websiteId,
domain: domain,
status: RouteStatus.PREPARED_CREATE,
created_by: new ObjectId(uid),
created_at: now,
updated_at: now,
}
const ret = await DatabaseAgent.db.collection(CN_ROUTES)
.insertOne(data as any)
if (!ret.insertedId) {
logger.error('create route task failed: {}', appid)
return false
}
}

return true
}

export async function deleteOssCustomRoute(appid: string, websiteId: string) {
export async function deleteWebsiteCustomRoute(appid: string, websiteId: string) {
const ret = await DatabaseAgent.db.collection<IRouteData>(CN_ROUTES)
.updateOne({
appid: appid,
Expand Down

0 comments on commit e2c7196

Please sign in to comment.