Skip to content

Commit

Permalink
fix: fix conection to db
Browse files Browse the repository at this point in the history
  • Loading branch information
adioihsan committed Feb 22, 2024
1 parent 757644f commit d33f9bd
Show file tree
Hide file tree
Showing 5 changed files with 3,271 additions and 97 deletions.
12 changes: 6 additions & 6 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path');
const path = require("path");

module.exports = {
'config': path.resolve('config', 'dbConfig.js'),
'models-path': path.resolve('app', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
}
config: path.resolve("config", "dbConfig.js"),
"models-path": path.resolve("app", "models"),
"seeders-path": path.resolve("db", "seeders"),
"migrations-path": path.resolve("db", "migrations"),
};
54 changes: 28 additions & 26 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var app = express();
var dotenv = require('dotenv').config()
const response = require('./utils/formatResponse')
const indexRouter = require('./routes/index')
require("dotenv").config();
const response = require("./utils/formatResponse");
const indexRouter = require("./routes/index");

const swaggerJSON = require('./swagger.json');
const swaggerUI = require('swagger-ui-express');
const swaggerJSON = require("./swagger.json");
const swaggerUI = require("swagger-ui-express");

const cors = require('cors');
const cors = require("cors");
app.use(cors());
app.use(function(req, res, next) {
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});

app.use(logger('dev'));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/docs', swaggerUI.serve, swaggerUI.setup(swaggerJSON));
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/docs", swaggerUI.serve, swaggerUI.setup(swaggerJSON));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.error = req.app.get("env") === "development" ? err : {};

const status = err.status || 500;
if (err.name === "NotFoundError") {
return response(res, status, false, "Url Not Found", null)
}
return response(res, false, status, err.message, null)
return response(res, status, false, "Url Not Found", null);
}
return response(res, false, status, err.message, null);
});

module.exports = app;
module.exports = app;
36 changes: 16 additions & 20 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('be-second-hand:server');
var http = require('http');
var socket = require('../app/libs/socket');
var app = require("../app");
var debug = require("debug")("be-second-hand:server");
var http = require("http");
var socket = require("../app/libs/socket");

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var port = normalizePort(process.env.APP_PORT || "5000");
app.set("port", port);

/**
* Create HTTP server.
Expand All @@ -27,8 +27,8 @@ var server = http.createServer(app);
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
server.on("error", onError);
server.on("listening", onListening);

/**
* Normalize a port into a number, string, or false.
Expand All @@ -55,22 +55,20 @@ function normalizePort(val) {
*/

function onError(error) {
if (error.syscall !== 'listen') {
if (error.syscall !== "listen") {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
Expand All @@ -84,10 +82,8 @@ function onError(error) {

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}

/**
Expand Down
63 changes: 18 additions & 45 deletions config/dbConfig.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
require("dotenv").config();

module.exports = {
development: {
username: "postgres",
password: "123",
database: "db_2ndhand",
host: "127.0.0.1",
dialect: "postgres"
},
test: {
username: "postgres",
password: "admin",
database: "db_2ndhand",
host: "127.0.0.1",
dialect: "postgres"
},
production: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOST,
port: parseInt(process.env.PROD_DB_PORT),
url: `postgres://${process.env.PROD_DB_USERNAME}:${process.env.PROD_DB_PASSWORD}@${process.env.PROD_DB_HOST}:${process.env.PROD_DB_PORT}/${process.env.PROD_DB_NAME}`,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
},
test_production: {
username: process.env.TEST_DB_USERNAME,
password: process.env.TEST_DB_PASSWORD,
database: process.env.TEST_DB_NAME,
host: process.env.TEST_DB_HOST,
port: parseInt(process.env.TEST_DB_PORT),
url: `postgres://${process.env.TEST_DB_USERNAME}:${process.env.TEST_DB_PASSWORD}@${process.env.TEST_DB_HOST}:${process.env.TEST_DB_PORT}/${process.env.TEST_DB_NAME}`,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}
};
development: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOST,
port: parseInt(process.env.PROD_DB_PORT),
dialect: "postgres",
},
production: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOST,
port: parseInt(process.env.PROD_DB_PORT),
dialect: "postgres",
},
};
Loading

0 comments on commit d33f9bd

Please sign in to comment.