Commit e760a43b authored by Lakr Aream's avatar Lakr Aream 👼🏽

重连上线

parent 146d0596
......@@ -15,7 +15,7 @@ app:
# Database Config For Backend Application
database:
host: "iomun.cn" # database host name
host: "127.0.0.1" # database host name
port: 15092 # database port
user: "root" # database user name
base: "Candiay" # database name
......
......@@ -12,7 +12,7 @@ export class DataBase {
private base: string // which database to use
private url: string
private connection: any
private connection: any = undefined
constructor() {
console.log("DataBase constructor")
......@@ -24,12 +24,7 @@ export class DataBase {
this.base = object.database.base
const uuu = String(this.host + ":" + String(this.port))
this.url = uuu
this.connection = mysql.createConnection({
host : this.host,
user : this.user,
password : this.pass,
database : this.base,
})
this.setupConnect()
}
healthCheck() {
......@@ -42,14 +37,49 @@ export class DataBase {
console.log("DataBase Inf - url " + this.url + "")
}
connect() {
this.connection.connect()
this.createTableIfNotExists()
private connectRetryTimes = 0
private setupConnect(doSetup: boolean = true) {
this.connection = mysql.createConnection({
host : this.host,
user : this.user,
password : this.pass,
database : this.base,
})
this.connection.connect((err: { message: any }, result: any) => {
if (err) {
console.log('[SQL ERROR] - ', err.message)
console.log('[SQL RETRY] Attempt ' + this.connectRetryTimes)
this.connectRetryTimes += 1
} else {
this.connectRetryTimes = 0
}
console.log('--------------------------SQL----------------------------')
console.log("Connect:")
console.log(result)
console.log('---------------------------------------------------------')
if (this.connectRetryTimes > 2) {
console.error("[fatal] Database setup failed")
process.exit()
} else if (err) {
this.setupConnect(doSetup)
return
}
if (doSetup) {
this.createTableIfNotExists()
}
})
this.connection.on('error', (err: any) => {
console.log('--------------------------SQL----------------------------')
console.log("Connect Closed")
console.log(err)
console.log('---------------------------------------------------------')
this.setupConnect(false)
});
}
createTableIfNotExists() {
let cmd = "CREATE TABLE IF NOT EXISTS `Candiay`.`users` (" + "\n"
private createTableIfNotExists() {
let cmd = "CREATE TABLE IF NOT EXISTS `" + this.base + "`.`users` (" + "\n"
cmd += " `name` varchar(255) NOT NULL," + "\n"
cmd += " `pass` varchar(255) NULL," + "\n"
cmd += " `token` varchar(255) NULL," + "\n"
......@@ -71,18 +101,25 @@ export class DataBase {
);
*/
this.connection.query(cmd, (err, result) => {
if(err){
console.log('[SQL ERROR] - ',err.message)
this.connection.query(cmd, (err: { message: any }, result: any) => {
if (err) {
console.log('[SQL ERROR] - ',err.message)
}
console.log('--------------------------SQL----------------------------')
console.log(result)
console.log('---------------------------------------------------------')
console.log('--------------------------SQL----------------------------')
console.log(cmd)
console.log(result)
console.log('---------------------------------------------------------')
if (err) {
console.error("[fatal] Database setup failed")
process.exit()
}
this.configRootUser()
})
}
queryAllObjects(fromTable: string) {
private configRootUser() {
}
}
\ No newline at end of file
import express from "express"
import { ConfigManager } from "./config"
import { DataBase } from "./database"
import { StatusManager } from "./status"
ConfigManager.shared.healthCheck()
DataBase.shared.healthCheck()
DataBase.shared.connect()
const coreApp = express()
const port = ConfigManager.shared.portToBind
// Register enterpoints
coreApp.get("/", (req, res) => {
if (!StatusManager.shared.configSuccessed) {
res.send("Application in configuration")
}
res.send("Candiay Core")
})
......
export class StatusManager {
static shared = new StatusManager()
public configSuccessed = false
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment