Skip to content

Commit

Permalink
logging and error checking also tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Apr 24, 2024
1 parent d5c462f commit b1a7824
Show file tree
Hide file tree
Showing 25 changed files with 1,369 additions and 338 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
/environment/artifacts
.DS_Store
coverage
logs
193 changes: 0 additions & 193 deletions controllers/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions db.js

This file was deleted.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
# entrypoint: ["tail"]
# command: ["-f", "/dev/null"]
environment:
DATABASE_URL: mongodb://${MONGO_INITDB_ROOT_USERNAME:-outpost}:${MONGO_INITDB_ROOT_PASSWORD:-password}@mongo/${MONGO_INITDB_DATABASE:-outpost_api_development}?retryWrites=true&w=majority
DB_URI: mongodb://${MONGO_INITDB_ROOT_USERNAME:-outpost}:${MONGO_INITDB_ROOT_PASSWORD:-password}@mongo/${MONGO_INITDB_DATABASE:-outpost_api_development}?retryWrites=true&w=majority
platform: linux/amd64
volumes:
- ./:/app:cached
Expand Down
93 changes: 70 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
require("dotenv").config()
const express = require("express")
const forceSSL = require("express-force-ssl")
const rateLimit = require("express-rate-limit")
const cors = require("cors")
const { connect } = require("./db")
const routes = require("./routes")

const server = express()
const port = process.env.NODE_PORT || 3000
const environment = process.env.NODE_ENV
const isDevelopment = environment === "development"
require("dotenv").config();
const express = require("express");
const forceSSL = require("express-force-ssl");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const morganMiddleware = require("./src/middleware/morgan.middleware");
const logger = require("./src/utils/logger");
const { connect } = require("./src/db");
const v1 = require("./src/controllers/v1");

const router = express.Router();
const server = express();
const port = process.env.NODE_PORT || 3000;
const environment = process.env.NODE_ENV || "production";
const isDevelopment = environment === "development";

/**
* Create the initial database connection here
*/
connect(() =>
console.log(
logger.info(
`📡 Database connection established http${
!isDevelopment ? "s" : ""
}://localhost:${port}/api/v1/services`
)
)
);

server.set("trust proxy", 1)
/**
* Settings & middleware
*/
server.set("trust proxy", 1);

// outside of dev environment if we send FORCE_SSL then SSL is forced
if (!isDevelopment) {
if (process.env.FORCE_SSL && process.env.FORCE_SSL.toLowerCase() === "true") {
server.use(forceSSL)
server.use(forceSSL);
}
}

Expand All @@ -33,18 +42,56 @@ server.use(
windowMs: 15 * 60 * 1000,
max: 100,
})
)
);

server.use(cors());
server.use(morganMiddleware);

/**
* Routes
*/

server.get("/api/v1/services", v1.services.index);
server.get("/api/v1/services/:id", v1.services.show);

server.use(cors())
server.get("/health", (req, res) => {
const data = {
uptime: process.uptime(),
message: "Ok",
date: new Date(),
}
};

res.status(200).send(data);
});

res.status(200).send(data)
})
server.use("/api/v1/", routes)
// 404
server.use("*", (req, res, next) => {
res.status(404).json({
error: "No route matches your request",
});
});

// Error handling
server.use((err, req, res, next) => {
logger.error(err.stack);
if (err.message === "No matching document") {
res.status(404).json({
error: "No matching document",
});
} else {
res.status(500).json({
error:
process.env.NODE_ENV === "production"
? "There was an internal server error. Please try again later"
: err.message,
});
}
});

server.listen(port, () => console.log(`✅ Listening on port ${port}`))
/**
* Start the server
*/
server.listen(port, () => {
logger.info(`Server is running on port ${port}`);
logger.info(`Logging level is set to ${logger.level}`);
});
51 changes: 0 additions & 51 deletions lib/queries.js

This file was deleted.

Loading

0 comments on commit b1a7824

Please sign in to comment.