diff --git a/src/server.ts b/src/server.ts index 5c2ba8cc2d57cac803a165007b0b6a0aa23c4f0f..8acf94482dcaf61561de0abeb4874c4a07b498cc 100644 --- a/src/server.ts +++ b/src/server.ts @@ -186,16 +186,53 @@ server.applyMiddleware({ app }); app.listen({ port: 4000 }); +/** + * DBNAME is required env arg for the application, exit if unset + */ if (!dbName) { console.error("Please set DBNAME environment variable"); process.exit(1); } +/** + * Creates a new Database, used when DBNAME doesn't exist + * + * In case of failure, the server will exist with error + * TODO: init DB with defaults + * + * @param appDB CouchDB DatabaseScope instance where to create the new DB + * @param dbName new DB name + */ +function createDB(appDB: Nano.DatabaseScope, dbName: string) { + console.log("[SERVER] Creating DB:" + dbName); + appDB + .create(dbName) + .then((body) => { + console.log(`[SERVER] database ${dbName} created!`); + }) + .catch((error) => { + console.log(`[SERVER][ERROR] Failed creating database ${dbName}`); + console.log(error); + process.exit(1); + }); +} + +/** + * Poll appDB waiting for connection ack + * + * If DBNAME doesn't exist, it will be created + * + * @param timeout polling sleep duration, multiplied by 2 at each retry + */ const connectDB = (timeout = 1000) => appDB.info().catch((error) => { const retryIn = Math.min(timeout * 2, maxRetryTimeout); setTimeout(() => connectDB(retryIn), retryIn); console.warn(error, `retry in ${retryIn}`); + // If DB doesn't exist, create it before first retry + if (error && error.error === "not_found" && timeout <= 1000) { + createDB(client.db, dbName); + } }); const client = Nano({ url: dbUrl, requestDefaults: { jar: true } });