diff --git a/Dockerfile b/Dockerfile index d65ce2c..0b54919 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,18 @@ -# --- Build Stage --- -FROM node:alpine3.20 AS builder +# Base image +FROM node:alpine3.20 +# Set working directory WORKDIR /app -# Copy package files and install all dependencies -COPY package*.json ./ +# Copy package files and install dependencies +COPY package.json ./ RUN npm install +# Copy application files COPY . . - -# --- Production Stage --- -FROM node:alpine3.20 - -WORKDIR /app - -# Only install production dependencies for a smaller, safer image -COPY package*.json ./ -RUN npm install --only=production - -# Copy the app from the 'builder' stage -COPY --from=builder /app . - -# Expose the port +# Expose the application port (server_port) EXPOSE 5000 -# run the app -CMD ["node", "index.js"] \ No newline at end of file +# Command to run the application +CMD ["npm", "start"] \ No newline at end of file diff --git a/__test__/healthcheck.test.js b/__test__/healthcheck.test.js deleted file mode 100644 index dd7954a..0000000 --- a/__test__/healthcheck.test.js +++ /dev/null @@ -1,20 +0,0 @@ -import request from "supertest"; -import app from "../index.js"; - -/** - * Test group for the Health Check endpoint. - * This endpoint is important for ensuring application performance - * and for Liveness/Readiness Probes in Kubernetes. - */ -describe("GET /healthz", () => { - it('should respond with a 200 status code and the message "OK"', async () => { - // Make a GET request to the /healthz endpoint using supertest - const response = await request(app).get("/healthz"); - - // Checks (Assert) whether the status code of the response is 200 - expect(response.statusCode).toBe(200); - - // Checks (Assert) whether the body of the response is the string "OK" - expect(response.text).toBe("OK"); - }); -}); diff --git a/index.js b/index.js index 91bfc9d..bd7cd00 100644 --- a/index.js +++ b/index.js @@ -70,19 +70,9 @@ if (process.env.NODE_ENV === "production") { app.use(errorHandler); -app.get("/healthz", (req, res) => { - res.status(200).send("OK"); -}); +mongoose.set("strictQuery", true); -export default app; - -const startServer = () => { - mongoose - .connect(CONNECTION_URL, { dbName: DB_NAME }) - .then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))) - .catch((error) => console.log(error.message)); -}; - -if (process.env.NODE_ENV !== "test") { - startServer(); -} +mongoose + .connect(CONNECTION_URL, { dbName: DB_NAME }) + .then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))) + .catch((error) => console.log(error.message)); diff --git a/terraform/main.tf b/terraform/main.tf deleted file mode 100644 index b3ff84f..0000000 --- a/terraform/main.tf +++ /dev/null @@ -1,84 +0,0 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 5.92" - } - } - - required_version = ">= 1.2" -} - -provider "aws" { - region = "ap-southeast-2" -} - -// create a Container Registry to store Docker images -resource "aws_ecr_repository" "app_repo" { - name = "restapi-users" - image_tag_mutability = "IMMUTABLE" - force_delete = true - - image_scanning_configuration { - scan_on_push = true - } -} - -// create a Network (VPC) for Kubernetes -module "vpc" { - source = "terraform-aws-modules/vpc/aws" - version = "6.0.1" - - name = "app-vpc" - cidr = "10.0.0.0/16" - - azs = ["ap-southeast-2a", "ap-southeast-2b"] - private_subnets = ["10.0.1.0/24", "10.0.2.0/24"] - public_subnets = ["10.0.101.0/24", "10.0.102.0/24"] - - enable_nat_gateway = true - single_nat_gateway = true - - tags = { - Project = "restapi-users" - Environment = "staging" - ManagedBy = "Terraform" - } -} - -// create a Kubernetes Cluster (EKS) -module "eks" { - source = "terraform-aws-modules/eks/aws" - version = "20.8.4" - - cluster_name = "restapi-users-cluster" - cluster_version = "1.29" - - vpc_id = module.vpc.vpc_id - subnet_ids = module.vpc.private_subnets - - eks_managed_node_groups = { - one = { - min_size = 1 - max_size = 3 - desired_size = 2 - - instance_types = ["t3.small"] - } - } - - tags = { - Project = "restapi-users" - Environment = "staging" - ManagedBy = "Terraform" - } -} - -// provides output of ECR repository URL and cluster name -output "ecr_repository_url" { - value = aws_ecr_repository.app_repo.repository_url -} - -output "eks_cluster_name" { - value = module.eks.cluster_name -} \ No newline at end of file