Skip to content

Commit

Permalink
updated(task_manager_api): dockernized the application
Browse files Browse the repository at this point in the history
  • Loading branch information
solo21-12 committed Aug 15, 2024
1 parent 45a0c1f commit ab3163b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
coverage.out
coverage.out
temp/
1 change: 1 addition & 0 deletions Delivery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func main() {

app := bootstrap.App()
env := app.Env
db := app.Mongo.Database(env.MONGO_DATABASE)
Expand Down
1 change: 1 addition & 0 deletions bootstrap/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions bootstrap/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bootstrap

import (
"log"
"github.com/spf13/viper"
)

Expand All @@ -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
}
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
32 changes: 32 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions tmp/build-errors.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit status 1

0 comments on commit ab3163b

Please sign in to comment.