mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve initialization logic
This commit is contained in:
parent
b1316eb547
commit
5be18d916d
@ -22,6 +22,8 @@ global.signLocation = process.env.signLocation
|
|||||||
|
|
||||||
export default class API {
|
export default class API {
|
||||||
server = global.server = new Server({
|
server = global.server = new Server({
|
||||||
|
name: "Main-API",
|
||||||
|
minimal: true,
|
||||||
listen_port: process.env.MAIN_LISTEN_PORT ?? 3000,
|
listen_port: process.env.MAIN_LISTEN_PORT ?? 3000,
|
||||||
onWSClientConnection: (...args) => {
|
onWSClientConnection: (...args) => {
|
||||||
this.onWSClientConnection(...args)
|
this.onWSClientConnection(...args)
|
||||||
@ -109,7 +111,7 @@ export default class API {
|
|||||||
events = internalEvents
|
events = internalEvents
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
await this.DB.connect()
|
await this.DB.initialize()
|
||||||
await this.initializeConfigDB()
|
await this.initializeConfigDB()
|
||||||
|
|
||||||
await this.storage.initialize()
|
await this.storage.initialize()
|
||||||
|
@ -1,38 +1,58 @@
|
|||||||
import mongoose from "mongoose"
|
import mongoose from "mongoose"
|
||||||
|
|
||||||
function getConnectionConfig(obj) {
|
function getConnectionConfig(obj) {
|
||||||
const { db_user, db_driver, db_name, db_pwd, db_hostname, db_port } = obj
|
const { DB_USER, DB_DRIVER, DB_NAME, DB_PWD, DB_HOSTNAME, DB_PORT } = obj
|
||||||
|
|
||||||
return [`${db_driver ?? "mongodb"}://${db_user ? `${db_user}` : ""}${db_pwd ? `:${db_pwd}` : ""}${db_user ? "@" : ""}${db_hostname ?? "localhost"}:${db_port ?? "27017"}${db_user ? "/?authMechanism=DEFAULT" : ""}`, {
|
let auth = [
|
||||||
dbName: db_name,
|
DB_DRIVER ?? "mongodb",
|
||||||
useNewUrlParser: true,
|
"://",
|
||||||
useUnifiedTopology: true,
|
]
|
||||||
}]
|
|
||||||
|
if (DB_USER && DB_PWD) {
|
||||||
|
auth.push(`${DB_USER}:${DB_PWD}@`)
|
||||||
|
}
|
||||||
|
|
||||||
|
auth.push(DB_HOSTNAME ?? "localhost")
|
||||||
|
auth.push(`:${DB_PORT ?? "27017"}`)
|
||||||
|
|
||||||
|
if (DB_USER) {
|
||||||
|
auth.push("/?authMechanism=DEFAULT")
|
||||||
|
}
|
||||||
|
|
||||||
|
auth = auth.join("")
|
||||||
|
|
||||||
|
return [
|
||||||
|
auth,
|
||||||
|
{
|
||||||
|
dbName: DB_NAME,
|
||||||
|
useNewUrlParser: true,
|
||||||
|
useUnifiedTopology: true,
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class DBManager {
|
export default class DBManager {
|
||||||
env = process.env
|
initialize = async (config) => {
|
||||||
|
console.log("🔌 Connecting to DB...")
|
||||||
|
|
||||||
connect = () => {
|
const dbConfig = getConnectionConfig(config ?? process.env)
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
try {
|
|
||||||
console.log("🌐 Trying to connect to DB...")
|
|
||||||
const dbConfig = getConnectionConfig(this.env)
|
|
||||||
|
|
||||||
mongoose.connect(...dbConfig)
|
mongoose.set("strictQuery", false)
|
||||||
.then((res) => { return resolve(true) })
|
|
||||||
.catch((err) => { return reject(err) })
|
const connection = await mongoose.connect(...dbConfig)
|
||||||
} catch (err) {
|
.catch((err) => {
|
||||||
return reject(err)
|
console.log(`❌ Failed to connect to DB, retrying...\n`)
|
||||||
}
|
console.log(error)
|
||||||
}).then(done => {
|
|
||||||
console.log(`✅ Connected to DB`)
|
// setTimeout(() => {
|
||||||
}).catch((error) => {
|
// this.initialize()
|
||||||
console.log(`❌ Failed to connect to DB, retrying...\n`)
|
// }, 1000)
|
||||||
console.log(error)
|
|
||||||
setTimeout(() => {
|
return false
|
||||||
this.connect()
|
})
|
||||||
}, 1000)
|
|
||||||
})
|
if (connection) {
|
||||||
|
console.log(`✅ Connected to DB.`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -48,13 +48,15 @@ export class StorageClient extends Minio.Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initialize = async () => {
|
initialize = async () => {
|
||||||
|
console.log("🔌 Checking if storage client have default bucket...")
|
||||||
|
|
||||||
// check connection with s3
|
// check connection with s3
|
||||||
const bucketExists = await this.bucketExists(this.defaultBucket).catch(() => {
|
const bucketExists = await this.bucketExists(this.defaultBucket).catch(() => {
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!bucketExists) {
|
if (!bucketExists) {
|
||||||
console.warn("Default bucket not exists! Creating new bucket...")
|
console.warn("🪣 Default bucket not exists! Creating new bucket...")
|
||||||
|
|
||||||
await this.makeBucket(this.defaultBucket, "s3")
|
await this.makeBucket(this.defaultBucket, "s3")
|
||||||
|
|
||||||
@ -71,19 +73,21 @@ export class StorageClient extends Minio.Client {
|
|||||||
// set default bucket policy
|
// set default bucket policy
|
||||||
await this.setDefaultBucketPolicy(this.defaultBucket)
|
await this.setDefaultBucketPolicy(this.defaultBucket)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("✅ Storage client is ready.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createStorageClientInstance = (options) => {
|
export const createStorageClientInstance = (options) => {
|
||||||
return new StorageClient({
|
return new StorageClient({
|
||||||
...options,
|
...options,
|
||||||
endPoint: process.env.s3_endpoint,
|
endPoint: process.env.S3_ENDPOINT,
|
||||||
port: Number(process.env.s3_port),
|
port: Number(process.env.S3_PORT),
|
||||||
useSSL: toBoolean(process.env.s3_use_ssl),
|
useSSL: toBoolean(process.env.S3_USE_SSL),
|
||||||
accessKey: process.env.s3_access_key,
|
accessKey: process.env.S3_ACCESS_KEY,
|
||||||
secretKey: process.env.s3_secret_key,
|
secretKey: process.env.S3_SECRET_KEY,
|
||||||
defaultBucket: process.env.s3_bucket_name,
|
defaultBucket: process.env.S3_BUCKET,
|
||||||
defaultRegion: process.env.s3_region,
|
defaultRegion: process.env.S3_REGION,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,6 @@ import API from "./api"
|
|||||||
async function main() {
|
async function main() {
|
||||||
const mainAPI = new API()
|
const mainAPI = new API()
|
||||||
|
|
||||||
console.log("\n▶️ Initializing main API...\n")
|
|
||||||
await mainAPI.initialize()
|
await mainAPI.initialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user