Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: opt website domain #156

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,11 @@ function initOssSubDomainRoute() {
type: 'roundrobin',
nodes: {'oss:9000': 1}
},
priority: 0,
timeout: {
connect: 600,
send: 600,
read: 600,
},
plugins: {
'proxy-rewrite': {
regex_uri: ["/$", "/index.html"]
}
}
}
ApiSixHttpUtils.put(baseUrl, 'base_oss_sub_domain', data)
Expand Down
32 changes: 21 additions & 11 deletions packages/gateway-controller/src/support/apisix-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {GatewayInterface} from "./gateway-operator";
import {IRouteData, RouteType} from "./route";
import Config from "../config";
import {ApiSixHttpUtils} from "./apisix-gateway-utils";
import {logger} from "./logger";

export class ApiSixGateway implements GatewayInterface {

Expand All @@ -17,16 +18,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)
} else if (route.type === RouteType.WEBSITE) {
return await createWebsiteRoute(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)
} else if (route.type === RouteType.WEBSITE) {
return await deleteWebsiteRoute(this.baseUrl, route)
}
}

Expand Down Expand Up @@ -69,24 +70,33 @@ async function deleteAppRoute(url: string, route: IRouteData) {
}


async function createWebsiteCustomRoute(url: string, route: IRouteData) {
if (route.domain.length !== 2) {
async function createWebsiteRoute(url: string, route: IRouteData) {
if (route.domain.length <= 0) {
logger.warn('website: {} domain list is empty')
return false
}
let hosts = null, node = null
let host = null, node = null
if (Config.SERVICE_DRIVER == 'docker') {
hosts = route.domain[0]
host = route.domain[0]
node = 'oss:9000'
}

let upstream_host = null
if (route.domain.length > 1) {
upstream_host = route.domain[1]
} else {
upstream_host = route.domain[0]

}

let data = {
name: route.name,
uri: '/*',
hosts: hosts,
hosts: [host],
priority: 9, // 设置优先级较高点
upstream: {
pass_host: 'rewrite',
upstream_host: route.domain[1] + '.' + Config.DEPLOY_OSS_DOMAIN,
upstream_host: upstream_host,
type: 'roundrobin',
nodes: {
[node]: 1
Expand All @@ -106,6 +116,6 @@ async function createWebsiteCustomRoute(url: string, route: IRouteData) {
return await ApiSixHttpUtils.put(url, route.website_id, data)
}

async function deleteWebsiteCustomRoute(url: string, route: IRouteData) {
async function deleteWebsiteRoute(url: string, route: IRouteData) {
return await ApiSixHttpUtils.delete(url, route.website_id)
}
2 changes: 1 addition & 1 deletion 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',
WEBSITE_CUSTOM = 'website_custom'
WEBSITE = 'website'
}


Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/handler/website/domain-bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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";
import {createWebsiteRoute} from "../../support/route";


/**
Expand Down Expand Up @@ -71,8 +71,8 @@ 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)
const domainList = [domain, website.cname]
const rt = await createWebsiteRoute('website-custom', app.appid, website_id, domainList, uid)
if (!rt) {
return res.send({ code: 'ROUTE CREATE FAILED', error: "route create failed" })
}
Expand Down
7 changes: 7 additions & 0 deletions packages/system-server/src/handler/website/website-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkPermission } from "../../support/permission"
import { URL } from "node:url"
import Config from "../../config"
import { WebsiteActionDef } from "../../actions"
import {createWebsiteRoute} from "../../support/route";

/**
* handle create website hosting
Expand Down Expand Up @@ -69,6 +70,12 @@ export async function handleCreateWebsite(req: Request, res: Response) {
return res.status(500).send("insert failed")
}

const domainList = [cname]
const rt = await createWebsiteRoute('website-custom', app.appid, result.insertedId.toString(), domainList, uid)
if (!rt) {
return res.send({ code: 'ROUTE CREATE FAILED', error: "route create failed" })
}

// return data
res.send({
data: { id: result.insertedId }
Expand Down
5 changes: 5 additions & 0 deletions packages/system-server/src/handler/website/website-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IApplicationData } from "../../support/application"
import { checkPermission } from "../../support/permission"
import { ObjectId } from 'mongodb'
import { WebsiteActionDef } from '../../actions'
import {deleteWebsiteRoute} from "../../support/route";

/**
* handle delete website
Expand Down Expand Up @@ -42,5 +43,9 @@ export async function handleDeleteWebsite(req: Request, res: Response) {
{ _id: new ObjectId(id), appid: app.appid },
{ $set: { status: "deleted", state: "pending", updated_at: new Date() } }
)
const rt = await deleteWebsiteRoute(app.appid, id)
if (!rt) {
return res.send({ code: 'ROUTE DELETE FAILED', error: "route delete failed" })
}
return res.send({ data: r.modifiedCount })
}
8 changes: 4 additions & 4 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',
WEBSITE_CUSTOM = 'website_custom'
WEBSITE = 'website'
}


Expand Down Expand Up @@ -70,7 +70,7 @@ export async function deleteApplicationRoute(appid: string) {
}


export async function createWebsiteCustomRoute(name: string, appid: string, websiteId: string, domain: string[], uid: any): Promise<Boolean> {
export async function createWebsiteRoute(name: string, appid: string, websiteId: string, domain: string[], uid: any): Promise<Boolean> {
const route = await DatabaseAgent.db.collection(CN_ROUTES).findOne({
appid: appid,
websiteId: websiteId
Expand All @@ -96,7 +96,7 @@ export async function createWebsiteCustomRoute(name: string, appid: string, webs
let data: IRouteData = {
name: name,
appid: appid,
type: RouteType.WEBSITE_CUSTOM,
type: RouteType.WEBSITE,
website_id: websiteId,
domain: domain,
status: RouteStatus.PREPARED_CREATE,
Expand All @@ -115,7 +115,7 @@ export async function createWebsiteCustomRoute(name: string, appid: string, webs
return true
}

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