From ab3163b600ccf7c34ed469f7f08686d4c3bc641a Mon Sep 17 00:00:00 2001 From: solo2112 Date: Thu, 15 Aug 2024 13:41:00 +0300 Subject: [PATCH] updated(task_manager_api): dockernized the application --- .github/workflows/tests.yml | 2 +- .gitignore | 3 ++- Delivery/main.go | 1 + bootstrap/database.go | 1 + bootstrap/env.go | 16 ++++++++------ docker-compose.yml | 42 +++++++++++++++++++++++++++++++++++++ dockerfile | 32 ++++++++++++++++++++++++++++ go.mod | 2 +- tmp/build-errors.log | 1 + 9 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yml create mode 100644 dockerfile create mode 100644 tmp/build-errors.log diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4417878..61d22ae 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -31,7 +31,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: "1.22.5" + go-version: "1.22" - name: Cache Go modules uses: actions/cache@v3 diff --git a/.gitignore b/.gitignore index fef0321..38799bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env -coverage.out \ No newline at end of file +coverage.out +temp/ \ No newline at end of file diff --git a/Delivery/main.go b/Delivery/main.go index 083161d..78599c3 100644 --- a/Delivery/main.go +++ b/Delivery/main.go @@ -7,6 +7,7 @@ import ( ) func main() { + app := bootstrap.App() env := app.Env db := app.Mongo.Database(env.MONGO_DATABASE) diff --git a/bootstrap/database.go b/bootstrap/database.go index 9671e69..f297eac 100644 --- a/bootstrap/database.go +++ b/bootstrap/database.go @@ -16,6 +16,7 @@ func NewMongoDatabase(env *Env) *mongo.Client { defer cancel() dbURL := env.MONGO_URL + clientOptions := options.Client().ApplyURI(dbURL) client, err := mongo.Connect(ctx, clientOptions) diff --git a/bootstrap/env.go b/bootstrap/env.go index c0acbce..902ca26 100644 --- a/bootstrap/env.go +++ b/bootstrap/env.go @@ -1,7 +1,6 @@ package bootstrap import ( - "log" "github.com/spf13/viper" ) @@ -22,11 +21,16 @@ func NewEnv() *Env { viper.AutomaticEnv() env := Env{} - - err := viper.Unmarshal(&env) - if err != nil { - log.Fatalf("Environment can't be loaded : %v", err) - } + env.MONGO_URL = viper.GetString("MONGO_URL") + env.MONGO_DATABASE = viper.GetString("MONGO_DATABASE") + env.SERVER_ADDRESS = viper.GetString("SERVER_ADDRESS") + env.JWT_SECRET = viper.GetString("JWT_SECRET") + env.USER_COLLECTION = viper.GetString("USER_COLLECTION") + env.TASK_COLLECTION = viper.GetString("TASK_COLLECTION") + env.ALLOWED_USERS = viper.GetString("ALLOWED_USERS") + env.TEST_DATABASE = viper.GetString("TEST_DATABASE") + env.TEST_USER_COLLECTION = viper.GetString("TEST_USER_COLLECTION") + env.TEST_TASK_COLLECTION = viper.GetString("TEST_TASK_COLLECTION") return &env } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cc3aa1c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +version: "3.8" + +services: + mongodb: + image: mongo:latest + container_name: mongo + ports: + - "27017:27017" + volumes: + - mongo-data:/data/db + networks: + - app-network + + app: + build: + context: . + dockerfile: Dockerfile + container_name: task-manager + ports: + - "8081:8081" + depends_on: + - mongodb + environment: + MONGO_URL: "mongodb://mongo:27017/task_manager" + MONGO_DATABASE: "task_manager" + SERVER_ADDRESS: ":8081" + USER_COLLECTION: "users" + JWT_SECRET: "b37df767fedf114b3f326deb59fa27f1b7bcc1c94a4f99a6b5f7423165b5fb0d" + ALLOWED_USERS: "admin" + TASK_COLLECTION: "tasks" + TEST_DATABASE: "test_db" + TEST_USER_COLLECTION: "user_test" + TEST_TASK_COLLECTION: "task_test" + networks: + - app-network + +networks: + app-network: + driver: bridge + +volumes: + mongo-data: diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..335d7cd --- /dev/null +++ b/dockerfile @@ -0,0 +1,32 @@ +# Use an official Go runtime as a parent image +FROM golang:1.22-alpine AS builder + +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy the Go Modules manifests +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source code into the container +COPY . . + +# Build the Go app +RUN go build -o bin/task-manager ./Delivery/main.go + +# Use a minimal base image to run the Go app +FROM alpine:latest + +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy the binary from the builder stage +COPY --from=builder /app/bin/task-manager . + +# Expose port 8081 to the outside world +EXPOSE 8081 + +# Run the binary program produced by `go build` +CMD ["./task-manager"] diff --git a/go.mod b/go.mod index 42a9d26..1112746 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/solo21-12/A2SV_back_end_track/tree/main/task_seven -go 1.22.5 +go 1.22 require go.mongodb.org/mongo-driver v1.16.1 diff --git a/tmp/build-errors.log b/tmp/build-errors.log new file mode 100644 index 0000000..05e5985 --- /dev/null +++ b/tmp/build-errors.log @@ -0,0 +1 @@ +exit status 1 \ No newline at end of file