Skip to content

Commit 3cb6526

Browse files
committed
fix: update config app & routes
1 parent c400335 commit 3cb6526

File tree

4 files changed

+51
-56
lines changed

4 files changed

+51
-56
lines changed

src/config/app.ts

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { blue, green } from 'colorette'
21
import compression from 'compression'
32
import cookieParser from 'cookie-parser'
43
import cors from 'cors'
@@ -18,15 +17,15 @@ import { expressRateLimit } from '~/app/middleware/expressRateLimit'
1817
import { expressUserAgent } from '~/app/middleware/expressUserAgent'
1918
import { expressWithState } from '~/app/middleware/expressWithState'
2019
import { optionsSwaggerUI, swaggerSpec } from '~/core/modules/docsSwagger'
21-
import ResponseError from '~/core/modules/response/ResponseError'
22-
import db from '~/database/data-source'
20+
import ErrorResponse from '~/core/modules/response/ErrorResponse'
2321
import indexRoutes from '../routes'
2422
import { corsOptions } from './cors'
2523
import { env } from './env'
2624
import { i18n } from './i18n'
2725
import { mailService } from './mail'
28-
import { httpLogger, logger } from './pino'
26+
import { httpLogger } from './pino'
2927
import { storageService } from './storage'
28+
import { storageExists } from '~/core/utils/boolean'
3029

3130
/**
3231
* Initialize Bootsrap Application
@@ -41,7 +40,6 @@ export class App {
4140

4241
this._plugins()
4342
this._provider()
44-
this._database()
4543

4644
// docs swagger disable for production mode
4745
if (env.NODE_ENV !== 'production') {
@@ -78,12 +76,10 @@ export class App {
7876
* Initialize Provider
7977
*/
8078
private _provider(): void {
79+
const storage_exists = storageExists()
80+
8181
// storage
82-
if (
83-
env.STORAGE_PROVIDER &&
84-
env.STORAGE_ACCESS_KEY &&
85-
env.STORAGE_SECRET_KEY
86-
) {
82+
if (storage_exists) {
8783
void storageService.initialize()
8884
}
8985

@@ -126,44 +122,12 @@ export class App {
126122

127123
const endpoint = `${host}${url}`
128124

129-
throw new ResponseError.NotFound(
125+
throw new ErrorResponse.NotFound(
130126
`Sorry, the ${endpoint} HTTP method ${method} resource you are looking for was not found.`
131127
)
132128
})
133129
}
134130

135-
/**
136-
* Initialize Database
137-
*/
138-
private _database(): void {
139-
const dbDialect = blue(env.SEQUELIZE_CONNECTION)
140-
const dbName = blue(env.SEQUELIZE_DATABASE)
141-
142-
// connect to database
143-
db.sequelize
144-
.authenticate()
145-
.then(async () => {
146-
const msgType = green(`sequelize`)
147-
const message = `connection ${dbDialect}: ${dbName} has been established successfully.`
148-
149-
logger.info(`${msgType} - ${message}`)
150-
151-
// not recommended when running in production mode
152-
if (env.SEQUELIZE_SYNC) {
153-
await db.sequelize.sync({ force: true })
154-
155-
logger.info(`${msgType} - all sync database successfully`)
156-
}
157-
})
158-
.catch((err: any) => {
159-
const errType = `sequelize error:`
160-
const message = `unable to connect to the database ${dbDialect}: ${dbName}`
161-
162-
logger.error(`${errType} - ${message}`)
163-
console.log(err)
164-
})
165-
}
166-
167131
/**
168132
* Create Bootstrap App
169133
*/

src/core/modules/docsSwagger.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import swaggerJSDoc from 'swagger-jsdoc'
55
import { env } from '~/config/env'
66
import { BASE_URL_SERVER } from '../constants/baseURL'
77

8-
const baseRouteDocs = path.resolve(`${__dirname}/../../app/docs/routes`)
9-
const baseSchemaDocs = path.resolve(`${__dirname}/../../app/docs/schemas`)
8+
const _pathRouteDocs = path.resolve(`${__dirname}/../../app/docs/routes`)
9+
const _pathSchemaDocs = path.resolve(`${__dirname}/../../app/docs/schemas`)
1010

1111
/**
1212
* Get Route Docs
1313
* @param _path
1414
* @returns
1515
*/
16-
function getDocsSwaggers(_path: string | Buffer): Record<string, unknown> {
16+
function _getDocsSwaggers(_path: string | Buffer): Record<string, unknown> {
1717
return fs.readdirSync(_path).reduce((acc, file) => {
1818
// eslint-disable-next-line @typescript-eslint/no-var-requires
1919
const data = require(`${_path}/${file}`)
@@ -23,8 +23,8 @@ function getDocsSwaggers(_path: string | Buffer): Record<string, unknown> {
2323
}, {})
2424
}
2525

26-
const docsRoutes = getDocsSwaggers(baseRouteDocs)
27-
const docsSchemas = getDocsSwaggers(baseSchemaDocs)
26+
const routesDocs = _getDocsSwaggers(_pathRouteDocs)
27+
const schemaDocs = _getDocsSwaggers(_pathSchemaDocs)
2828

2929
const baseURLServer = [
3030
{
@@ -69,7 +69,7 @@ export const swaggerOptions = {
6969
'JWT Authorization header using the JWT scheme. Example: “Authorization: JWT {token}”',
7070
},
7171
},
72-
schemas: docsSchemas,
72+
schemas: schemaDocs,
7373
parameters: {
7474
page: {
7575
in: 'query',
@@ -105,7 +105,7 @@ export const swaggerOptions = {
105105
},
106106
},
107107
},
108-
paths: docsRoutes,
108+
paths: routesDocs,
109109
},
110110
apis: [],
111111
}

src/main.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { blue, green } from 'colorette'
2+
import { logger } from 'expresso-core'
13
import http from 'http'
24
import { App } from './config/app'
35
import { env } from './config/env'
46
import { httpHandle } from './core/modules/http/handle'
7+
import db from './database/datasource'
58

69
function bootstrap(): void {
710
const port = env.APP_PORT
@@ -14,10 +17,38 @@ function bootstrap(): void {
1417
// http handle
1518
const { onError, onListening } = httpHandle(server, port)
1619

17-
// run server listen
18-
server.listen(port)
19-
server.on('error', onError)
20-
server.on('listening', onListening)
20+
const dbDialect = blue(env.SEQUELIZE_CONNECTION)
21+
const dbName = blue(env.SEQUELIZE_DATABASE)
22+
23+
// connect to database
24+
db.sequelize
25+
.authenticate()
26+
.then(async () => {
27+
const msgType = green(`sequelize`)
28+
const message = `connection ${dbDialect}: ${dbName} has been established successfully.`
29+
30+
logger.info(`${msgType} - ${message}`)
31+
32+
// not recommended when running in production mode
33+
if (env.SEQUELIZE_SYNC) {
34+
await db.sequelize.sync({ force: true })
35+
36+
logger.info(`${msgType} - all sync database successfully`)
37+
}
38+
39+
// run server listen
40+
server.listen(port)
41+
server.on('error', onError)
42+
server.on('listening', onListening)
43+
})
44+
.catch((err: any) => {
45+
const errType = `sequelize error:`
46+
const message = `unable to connect to the database ${dbDialect}: ${dbName}`
47+
console.log(err)
48+
49+
logger.error(`${errType} - ${message}`)
50+
process.exit(1)
51+
})
2152
}
2253

2354
bootstrap()

src/routes/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express, { type Request, type Response } from 'express'
22
import { env } from '~/config/env'
33
import { BASE_URL_SERVER } from '~/core/constants/baseURL'
44
import HttpResponse from '~/core/modules/response/HttpResponse'
5-
import ResponseError from '~/core/modules/response/ResponseError'
5+
import ErrorResponse from '~/core/modules/response/ErrorResponse'
66
import { formatDateTime } from '~/core/utils/date'
77
import v1Routes from '~/routes/v1'
88

@@ -54,7 +54,7 @@ route.get('/v1', function (req: Request, res: Response) {
5454

5555
const endpoint = `${host}${url}`
5656

57-
throw new ResponseError.Forbidden(
57+
throw new ErrorResponse.Forbidden(
5858
`Forbidden, wrong access method ${method} endpoint: ${endpoint}`
5959
)
6060
})

0 commit comments

Comments
 (0)