-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#7] refactor: move mongoose connection logic into seperate util file
- Loading branch information
Showing
2 changed files
with
21 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { connect } from 'mongoose' | ||
import logger from './logger.util' | ||
|
||
export default async (connectionString: string): Promise<void> => { | ||
await connect(connectionString) | ||
.then(() => { | ||
logger.info(`[Init] Connected to database`) | ||
}) | ||
.catch((error: Error) => { | ||
logger.error(`[Init] Failed to connect to database: ${error.message}`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import 'dotenv/config' | ||
import http, { Server } from 'http' | ||
import { connect } from 'mongoose' | ||
import logger from './common/logger.util' | ||
import connectToDatabase from './common/mongodb.util' | ||
import index from './index' | ||
|
||
const port: string = process.env.PORT ?? '3000' | ||
const dbUrl: string | undefined = process.env.DB_URL | ||
if (!dbUrl) { | ||
logger.error(`[Init] DB_URL is not set`) | ||
} | ||
|
||
const server: Server = http.createServer(index) | ||
|
||
connect(dbUrl!) | ||
.then(() => { | ||
logger.info(`[Init] Connected to MongoDB`) | ||
}) | ||
.catch((error: Error) => { | ||
logger.error(`[Init] ${error.message}`) | ||
}) | ||
|
||
server.listen(port, async () => { | ||
logger.info(`[Init] Server is listening on port ${port}`) | ||
}) | ||
|
||
const connectionString: string | undefined = process.env.DB_URL | ||
if (!connectionString) { | ||
logger.error(`[Init] DB_URL is not set`) | ||
process.exit(1) | ||
} | ||
|
||
connectToDatabase(connectionString) |