From b3612769a0d1c148373fdf068cfbbee6426414ac Mon Sep 17 00:00:00 2001
From: Michael Handel <michael.handel@student.reutlingen-university.de>
Date: Tue, 29 Apr 2025 21:54:23 +0200
Subject: [PATCH] Exercise 2 added conterisation for database and application

---
 .gitignore                      | 17 ++++++++++-------
 Dockerfile                      | 20 ++++++++++++++++++++
 docker-compose.yml              | 22 ++++++++++++++++++++++
 index.html => public/index.html |  0
 style.css => public/style.css   |  0
 server.js                       | 14 ++++++++++----
 6 files changed, 62 insertions(+), 11 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 docker-compose.yml
 rename index.html => public/index.html (100%)
 rename style.css => public/style.css (100%)

diff --git a/.gitignore b/.gitignore
index 5422604..62f586f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,7 @@
 node_modules/
 
 # Logs
-logs
+logs/
 *.log
 npm-debug.log*
 yarn-debug.log*
@@ -11,11 +11,14 @@ yarn-error.log*
 # Environment variables
 .env
 
-# OS-specific files
+# Build output
+dist/
+build/
+
+# Docker
+docker-compose.override.yml
+.dockerignore
+
+# System files
 .DS_Store
 Thumbs.db
-
-# IDE-specific files
-.vscode/
-.idea/
-*.iml
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..b3de296
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,20 @@
+# Use the official Node.js image as the base image
+FROM node:18
+
+# Set the working directory inside the container
+WORKDIR /app
+
+# Copy package.json and package-lock.json to the container
+COPY package*.json ./
+
+# Install dependencies
+RUN npm install
+
+# Copy the rest of the application code to the container
+COPY . .
+
+# Expose the application port
+EXPOSE 3000
+
+# Start the application
+CMD ["node", "server.js"]
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..a5aab92
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,22 @@
+services:
+  app:
+    build:
+      context: .
+      dockerfile: Dockerfile
+    ports:
+      - "3000:3000" # Expose the app on port 3000
+    environment:
+      - MONGO_URL=mongodb://db:27017/todo-app # MongoDB connection string
+    depends_on:
+      - db
+
+  db:
+    image: mongo:6.0
+    container_name: mongodb
+    ports:
+      - "27017:27017" # Expose MongoDB on port 27017
+    volumes:
+      - mongo-data:/data/db # Persist MongoDB data
+
+volumes:
+  mongo-data:
\ No newline at end of file
diff --git a/index.html b/public/index.html
similarity index 100%
rename from index.html
rename to public/index.html
diff --git a/style.css b/public/style.css
similarity index 100%
rename from style.css
rename to public/style.css
diff --git a/server.js b/server.js
index 5acb3a9..28c9107 100644
--- a/server.js
+++ b/server.js
@@ -2,6 +2,7 @@
 const express = require('express');
 const mongoose = require('mongoose');
 const cors = require('cors');
+const path = require('path'); // Import path module for serving static files
 
 // Initialize Express app
 const app = express();
@@ -11,11 +12,11 @@ const PORT = 3000; // Define the port the server will run on
 app.use(express.json()); // Parse incoming JSON requests
 app.use(cors()); // Enable Cross-Origin Resource Sharing
 
+// Serve static files from the "public" directory
+app.use(express.static(path.join(__dirname, 'public')));
+
 // Connect to MongoDB database
-mongoose.connect('mongodb://localhost:27017/todo-app', {
-    useNewUrlParser: true,
-    useUnifiedTopology: true,
-});
+mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/todo-app');
 
 // Define the schema for a TODO item
 const todoSchema = new mongoose.Schema({
@@ -60,6 +61,11 @@ app.delete('/todos/:id', async (req, res) => {
     res.json({ message: 'Todo deleted' }); // Return a success message
 });
 
+// Default route to serve index.html
+app.get('/', (req, res) => {
+    res.sendFile(path.join(__dirname, 'public', 'index.html'));
+});
+
 // Start the server
 app.listen(PORT, () => {
     console.log(`Server running on http://localhost:${PORT}`); // Log the server URL
-- 
GitLab