Skip to content

Commit

Permalink
feat: opt ssl create and code style (#282)
Browse files Browse the repository at this point in the history
* feat: opt ssl create

* feat: opt gateway instance code style
  • Loading branch information
skyoct authored Aug 21, 2022
1 parent 1d44356 commit f118412
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 428 deletions.
18 changes: 9 additions & 9 deletions packages/gateway-controller/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default class Config {
}

/**
* the mongodb connection configuration of apps' db, use for creating app databases;
*/
* the mongodb connection configuration of apps' db, use for creating app databases;
*/
static get APP_DB_URI() {
if (!process.env['APP_DB_URI']) {
throw new Error('env: `APP_DB_URI` is missing')
Expand Down Expand Up @@ -112,13 +112,13 @@ export default class Config {
return process.env['DEPLOY_OSS_DOMAIN']
}

/**
* The schema of app deployed url: `http` | `https`.
* Default value is `http`.
*/
static get APP_SERVICE_DEPLOY_URL_SCHEMA(): string {
return process.env.APP_SERVICE_DEPLOY_URL_SCHEMA ?? 'http'
}
/**
* The schema of app deployed url: `http` | `https`.
* Default value is `http`.
*/
static get APP_SERVICE_DEPLOY_URL_SCHEMA(): string {
return process.env.APP_SERVICE_DEPLOY_URL_SCHEMA ?? 'http'
}


}
38 changes: 19 additions & 19 deletions packages/gateway-controller/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as express from 'express'
import Config from './config'
import { logger } from './support/logger'
import { DatabaseAgent } from './support/db'
import { start_scheduler } from './scheduler'
import { initBaseRoute, initBaseSSL } from "./support/apisix-gateway-init"
import {logger} from './support/logger'
import {DatabaseAgent} from './support/db'
import {start_scheduler} from './scheduler'
import {initBaseRoute, initBaseSSL} from "./support/apisix-gateway-init"

DatabaseAgent.init(Config.SYS_DB_URI)

const app = express()

app.get('/healthz', (_req, res) => {
if (!DatabaseAgent.db) {
return res.status(400).send('no db connection')
}
return res.status(200).send('ok')
if (!DatabaseAgent.db) {
return res.status(400).send('no db connection')
}
return res.status(200).send('ok')
})
// init base route
initBaseRoute()

// init base ssl
if (Config.APP_SERVICE_DEPLOY_URL_SCHEMA === 'https') {
initBaseSSL()
initBaseSSL()
}

start_scheduler()
Expand All @@ -29,24 +29,24 @@ start_scheduler()
const server = app.listen(Config.PORT, () => logger.info(`listened on ${Config.PORT}`))

process.on('unhandledRejection', (reason, promise) => {
logger.error(`Caught unhandledRejection:`, reason, promise)
logger.error(`Caught unhandledRejection:`, reason, promise)
})

process.on('uncaughtException', err => {
logger.error(`Caught uncaughtException:`, err)
logger.error(`Caught uncaughtException:`, err)
})


process.on('SIGTERM', gracefullyExit)
process.on('SIGINT', gracefullyExit)

async function gracefullyExit() {
logger.info('exiting: closing db connection')
await DatabaseAgent.conn.close()
logger.info('exiting: db connection has been closed')

server.close(async () => {
logger.info('process gracefully exited!')
process.exit(0)
})
logger.info('exiting: closing db connection')
await DatabaseAgent.conn.close()
logger.info('exiting: db connection has been closed')

server.close(async () => {
logger.info('process gracefully exited!')
process.exit(0)
})
}
62 changes: 31 additions & 31 deletions packages/gateway-controller/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ import {getRoutesInStatus, RouteStatus, updateRouteStatus} from "./support/route
import {GatewayOperator} from "./support/gateway-operator";

export function start_scheduler() {
logger.info('start scheduler loop')
setInterval(loop, Config.SCHEDULER_INTERVAL)
logger.info('start scheduler loop')
setInterval(loop, Config.SCHEDULER_INTERVAL)
}

function loop() {
if (!DatabaseAgent.db) {
return logger.info('waiting for db connected...')
}
const tick = new Date()
handle_prepared_create(tick)
handle_prepared_delete(tick)
if (!DatabaseAgent.db) {
return logger.info('waiting for db connected...')
}
const tick = new Date()
handle_prepared_create(tick)
handle_prepared_delete(tick)

}

async function handle_prepared_create(tick: any) {
const routes = await getRoutesInStatus(RouteStatus.PREPARED_CREATE)
for (let route of routes) {
try {
let res = await GatewayOperator.create(route)
console.log(res)
if (res) {
logger.info(tick, `update ${route.appid} status from 'PREPARED_CREATE' to 'CREATED'`)
await updateRouteStatus(route.appid, route.status, RouteStatus.CREATED)
}
} catch (error) {
logger.error(tick, `handle_prepared_create(${route.appid}) error: `, error)
}
const routes = await getRoutesInStatus(RouteStatus.PREPARED_CREATE)
for (let route of routes) {
try {
let res = await GatewayOperator.create(route)
console.log(res)
if (res) {
logger.info(tick, `update ${route.appid} status from 'PREPARED_CREATE' to 'CREATED'`)
await updateRouteStatus(route.appid, route.status, RouteStatus.CREATED)
}
} catch (error) {
logger.error(tick, `handle_prepared_create(${route.appid}) error: `, error)
}
}
}

async function handle_prepared_delete(tick: any) {
const routes = await getRoutesInStatus(RouteStatus.PREPARED_DELETE)
for (let route of routes) {
try {
let res = await GatewayOperator.delete(route)
if (res) {
logger.info(tick, `update ${route.appid} status from 'PREPARED_DELETE' to 'DELETED'`)
await updateRouteStatus(route.appid, route.status, RouteStatus.DELETED)
}
} catch (error) {
logger.error(tick, `handle_prepared_delete(${route.appid}) error: `, error)
}
const routes = await getRoutesInStatus(RouteStatus.PREPARED_DELETE)
for (let route of routes) {
try {
let res = await GatewayOperator.delete(route)
if (res) {
logger.info(tick, `update ${route.appid} status from 'PREPARED_DELETE' to 'DELETED'`)
await updateRouteStatus(route.appid, route.status, RouteStatus.DELETED)
}
} catch (error) {
logger.error(tick, `handle_prepared_delete(${route.appid}) error: `, error)
}
}
}
Loading

0 comments on commit f118412

Please sign in to comment.