mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +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 {
|
||||
server = global.server = new Server({
|
||||
name: "Main-API",
|
||||
minimal: true,
|
||||
listen_port: process.env.MAIN_LISTEN_PORT ?? 3000,
|
||||
onWSClientConnection: (...args) => {
|
||||
this.onWSClientConnection(...args)
|
||||
@ -109,7 +111,7 @@ export default class API {
|
||||
events = internalEvents
|
||||
|
||||
async initialize() {
|
||||
await this.DB.connect()
|
||||
await this.DB.initialize()
|
||||
await this.initializeConfigDB()
|
||||
|
||||
await this.storage.initialize()
|
||||
|
@ -1,38 +1,58 @@
|
||||
import mongoose from "mongoose"
|
||||
|
||||
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" : ""}`, {
|
||||
dbName: db_name,
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
}]
|
||||
let auth = [
|
||||
DB_DRIVER ?? "mongodb",
|
||||
"://",
|
||||
]
|
||||
|
||||
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 {
|
||||
env = process.env
|
||||
initialize = async (config) => {
|
||||
console.log("🔌 Connecting to DB...")
|
||||
|
||||
connect = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
console.log("🌐 Trying to connect to DB...")
|
||||
const dbConfig = getConnectionConfig(this.env)
|
||||
const dbConfig = getConnectionConfig(config ?? process.env)
|
||||
|
||||
mongoose.connect(...dbConfig)
|
||||
.then((res) => { return resolve(true) })
|
||||
.catch((err) => { return reject(err) })
|
||||
} catch (err) {
|
||||
return reject(err)
|
||||
}
|
||||
}).then(done => {
|
||||
console.log(`✅ Connected to DB`)
|
||||
}).catch((error) => {
|
||||
console.log(`❌ Failed to connect to DB, retrying...\n`)
|
||||
console.log(error)
|
||||
setTimeout(() => {
|
||||
this.connect()
|
||||
}, 1000)
|
||||
})
|
||||
mongoose.set("strictQuery", false)
|
||||
|
||||
const connection = await mongoose.connect(...dbConfig)
|
||||
.catch((err) => {
|
||||
console.log(`❌ Failed to connect to DB, retrying...\n`)
|
||||
console.log(error)
|
||||
|
||||
// setTimeout(() => {
|
||||
// this.initialize()
|
||||
// }, 1000)
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if (connection) {
|
||||
console.log(`✅ Connected to DB.`)
|
||||
}
|
||||
}
|
||||
}
|
@ -48,13 +48,15 @@ export class StorageClient extends Minio.Client {
|
||||
}
|
||||
|
||||
initialize = async () => {
|
||||
console.log("🔌 Checking if storage client have default bucket...")
|
||||
|
||||
// check connection with s3
|
||||
const bucketExists = await this.bucketExists(this.defaultBucket).catch(() => {
|
||||
return false
|
||||
})
|
||||
|
||||
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")
|
||||
|
||||
@ -71,19 +73,21 @@ export class StorageClient extends Minio.Client {
|
||||
// set default bucket policy
|
||||
await this.setDefaultBucketPolicy(this.defaultBucket)
|
||||
}
|
||||
|
||||
console.log("✅ Storage client is ready.")
|
||||
}
|
||||
}
|
||||
|
||||
export const createStorageClientInstance = (options) => {
|
||||
return new StorageClient({
|
||||
...options,
|
||||
endPoint: process.env.s3_endpoint,
|
||||
port: Number(process.env.s3_port),
|
||||
useSSL: toBoolean(process.env.s3_use_ssl),
|
||||
accessKey: process.env.s3_access_key,
|
||||
secretKey: process.env.s3_secret_key,
|
||||
defaultBucket: process.env.s3_bucket_name,
|
||||
defaultRegion: process.env.s3_region,
|
||||
endPoint: process.env.S3_ENDPOINT,
|
||||
port: Number(process.env.S3_PORT),
|
||||
useSSL: toBoolean(process.env.S3_USE_SSL),
|
||||
accessKey: process.env.S3_ACCESS_KEY,
|
||||
secretKey: process.env.S3_SECRET_KEY,
|
||||
defaultBucket: process.env.S3_BUCKET,
|
||||
defaultRegion: process.env.S3_REGION,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,6 @@ import API from "./api"
|
||||
async function main() {
|
||||
const mainAPI = new API()
|
||||
|
||||
console.log("\n▶️ Initializing main API...\n")
|
||||
await mainAPI.initialize()
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user