From 66cf89e696a0cc16d6c1351486bee0d2ba3af851 Mon Sep 17 00:00:00 2001 From: Ajay Gupta Date: Tue, 22 Oct 2024 23:09:48 +0530 Subject: [PATCH 1/2] dockerized the whole project moved the server to its own folder --- client/.dockerignore | 9 +++++ client/client.dockerfile | 13 ++++++++ client/package.json | 66 ++++++++++++++++++------------------- docker-compose.yml | 28 ++++++++++++++++ server/.dockerignore | 3 ++ air.toml => server/air.toml | 0 go.mod => server/go.mod | 0 go.sum => server/go.sum | 0 main.go => server/main.go | 2 +- server/server.dockerfile | 21 ++++++++++++ 10 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 client/.dockerignore create mode 100644 client/client.dockerfile create mode 100644 docker-compose.yml create mode 100644 server/.dockerignore rename air.toml => server/air.toml (100%) rename go.mod => server/go.mod (100%) rename go.sum => server/go.sum (100%) rename main.go => server/main.go (98%) create mode 100644 server/server.dockerfile diff --git a/client/.dockerignore b/client/.dockerignore new file mode 100644 index 0000000..1b61deb --- /dev/null +++ b/client/.dockerignore @@ -0,0 +1,9 @@ +Dockerfile +.dockerignore +.gitignore +.git +README.md +frontend.dockerfile + +build +node_modules \ No newline at end of file diff --git a/client/client.dockerfile b/client/client.dockerfile new file mode 100644 index 0000000..964b3c8 --- /dev/null +++ b/client/client.dockerfile @@ -0,0 +1,13 @@ +FROM node:alpine3.19 + +WORKDIR /app + +COPY client/package*.json ./ + +RUN npm install --ignore-platform + +COPY client/ . + +EXPOSE 5173 + +CMD [ "npm" , "run" , "dev" ] \ No newline at end of file diff --git a/client/package.json b/client/package.json index f1eed5f..4f19081 100644 --- a/client/package.json +++ b/client/package.json @@ -1,35 +1,35 @@ { - "name": "client", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "dev": "vite", - "build": "tsc -b && vite build", - "lint": "eslint .", - "preview": "vite preview" - }, - "dependencies": { - "@chakra-ui/react": "^2.10.3", - "@emotion/react": "^11.13.3", - "@emotion/styled": "^11.13.0", - "@tanstack/react-query": "^5.59.15", - "framer-motion": "^11.11.9", - "react": "^18.3.1", - "react-dom": "^18.3.1", - "react-icons": "^5.3.0" - }, - "devDependencies": { - "@eslint/js": "^9.11.1", - "@types/react": "^18.3.10", - "@types/react-dom": "^18.3.0", - "@vitejs/plugin-react": "^4.3.2", - "eslint": "^9.11.1", - "eslint-plugin-react-hooks": "^5.1.0-rc.0", - "eslint-plugin-react-refresh": "^0.4.12", - "globals": "^15.9.0", - "typescript": "^5.5.3", - "typescript-eslint": "^8.7.0", - "vite": "^5.4.8" - } + "name": "client", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite --host", + "build": "tsc -b && vite build", + "lint": "eslint .", + "preview": "vite preview" + }, + "dependencies": { + "@chakra-ui/react": "^2.10.3", + "@emotion/react": "^11.13.3", + "@emotion/styled": "^11.13.0", + "@tanstack/react-query": "^5.59.15", + "framer-motion": "^11.11.9", + "react": "^18.3.1", + "react-dom": "^18.3.1", + "react-icons": "^5.3.0" + }, + "devDependencies": { + "@eslint/js": "^9.11.1", + "@types/react": "^18.3.10", + "@types/react-dom": "^18.3.0", + "@vitejs/plugin-react": "^4.3.2", + "eslint": "^9.11.1", + "eslint-plugin-react-hooks": "^5.1.0-rc.0", + "eslint-plugin-react-refresh": "^0.4.12", + "globals": "^15.9.0", + "typescript": "^5.5.3", + "typescript-eslint": "^8.7.0", + "vite": "^5.4.8" + } } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e5726fe --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +services: + backend: + container_name: go-server + build: + context: . + dockerfile: server/server.dockerfile + environment: + MONGODB_URI: + PORT: "5000" + ENV: development + ports: + - "5000:5000" + + healthcheck: + interval: 2s + test: "exit 0" + + frontend: + container_name: react-frontend + build: + context: . + dockerfile: client/client.dockerfile + + ports: + - "5173:5173" + depends_on: + backend: + condition: service_healthy diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 0000000..f9dbdd0 --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1,3 @@ +tmp +backend.dockerfile +.dockerignore \ No newline at end of file diff --git a/air.toml b/server/air.toml similarity index 100% rename from air.toml rename to server/air.toml diff --git a/go.mod b/server/go.mod similarity index 100% rename from go.mod rename to server/go.mod diff --git a/go.sum b/server/go.sum similarity index 100% rename from go.sum rename to server/go.sum diff --git a/main.go b/server/main.go similarity index 98% rename from main.go rename to server/main.go index 342acda..3f178df 100644 --- a/main.go +++ b/server/main.go @@ -70,7 +70,7 @@ func main() { } if os.Getenv("ENV") == "production" { - app.Static("/", "./client/dist") + app.Static("/", "./dist") } log.Fatal(app.Listen("0.0.0.0:" + port)) diff --git a/server/server.dockerfile b/server/server.dockerfile new file mode 100644 index 0000000..d46e7c1 --- /dev/null +++ b/server/server.dockerfile @@ -0,0 +1,21 @@ +# use official Golang image +FROM golang:alpine3.20 + +# set working directory +WORKDIR /app +RUN mkdir dist + +# Copy the source code +COPY server/ . +COPY client/dist ./dist +# Download and install the dependencies +RUN go mod tidy + +# Build the Go app +RUN go build -o todoApp . + +#EXPOSE the port +EXPOSE 5000 + +# Run the executable +CMD ["./todoApp"] \ No newline at end of file From ecfa1e4566844610a1a111223817b13c53003a2a Mon Sep 17 00:00:00 2001 From: Ajay Gupta Date: Wed, 23 Oct 2024 10:12:50 +0530 Subject: [PATCH 2/2] changed cors config -> now delete and patch request allowed removed requirement of .env file --- docker-compose.yml | 2 +- server/main.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e5726fe..4f5bc33 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: environment: MONGODB_URI: PORT: "5000" - ENV: development + ENV: production ports: - "5000:5000" diff --git a/server/main.go b/server/main.go index 3f178df..40d4db4 100644 --- a/server/main.go +++ b/server/main.go @@ -7,7 +7,7 @@ import ( "os" "github.com/gofiber/fiber/v2" - "github.com/joho/godotenv" + "github.com/gofiber/fiber/v2/middleware/cors" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -27,10 +27,10 @@ func main() { if os.Getenv("ENV") != "production" { // Load the .env file if not in production - err := godotenv.Load(".env") - if err != nil { - log.Fatal("Error loading .env file:", err) - } + // err := godotenv.Load(".env") + // if err != nil { + // log.Fatal("Error loading .env file:", err) + // } } MONGODB_URI := os.Getenv("MONGODB_URI") @@ -54,10 +54,10 @@ func main() { app := fiber.New() - // app.Use(cors.New(cors.Config{ - // AllowOrigins: "http://localhost:5173", - // AllowHeaders: "Origin,Content-Type,Accept", - // })) + app.Use(cors.New(cors.Config{ + AllowOrigins: "http://localhost:5173", + AllowHeaders: "Origin,Content-Type,Accept", + })) app.Get("/api/todos", getTodos) app.Post("/api/todos", createTodo)