Skip to content

Commit

Permalink
Add Dockerfile and docker-compose
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Góes <[email protected]>
  • Loading branch information
tiagodread committed Aug 7, 2024
1 parent 3427988 commit 05b16d4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
todo-api
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.22

WORKDIR /app

COPY . /app

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -o todo-api cmd/main.go

WORKDIR /app

EXPOSE 8080

RUN chmod +x todo-api

CMD [ "./todo-api" ]
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func main() {
})
})

server.Run(":8000")
server.Run(":8080")

}
21 changes: 10 additions & 11 deletions db/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package db
import (
"database/sql"
"fmt"
"log"
"os"

_ "github.com/lib/pq"
)

const (
host = "localhost"
port = 5432
user = "postgres"
password = 1234
dbname = "postgres"
)

func ConnectDB() (*sql.DB, error) {
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
log.Fatal("DATABASE_URL is not set")
}

fmt.Print(databaseURL)
db, err := sql.Open("postgres", databaseURL)

psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%d dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
Expand All @@ -28,7 +27,7 @@ func ConnectDB() (*sql.DB, error) {
panic(err)
}

fmt.Printf("Connected to %s", dbname)
// fmt.Printf("Connected to %s", dbname)

return db, nil
}
29 changes: 23 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
# Use postgres/example user/password credentials
version: '3.9'

services:
api:
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://postgres:1234@db:5432/postgres?sslmode=disable
GIN_MODE: release
depends_on:
- db
networks:
- todo-network

db:
image: postgres
container_name: database
restart: always
ports:
- "5432:5432"
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
- pgdata:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: 1234
POSTGRES_USER: postgres
POSTGRES_DB: postgres
networks:
- todo-network

volumes:
pgdata: {}
pgdata: {}

networks:
todo-network:
driver: bridge
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module todo-api

go 1.22.6
go 1.22.5

require (
github.com/gin-gonic/gin v1.10.0
Expand Down

0 comments on commit 05b16d4

Please sign in to comment.