Skip to content

Commit

Permalink
build: dockerised
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-Chowdhary committed Aug 14, 2024
1 parent be0669e commit 09e050b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 15 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:alpine AS builder

RUN mkdir /app
WORKDIR /app

COPY ./go.mod ./go.sum ./

RUN go mod download && go mod tidy

COPY ./ ./

RUN go build -o main ./cmd/api

# Run stage
FROM alpine
WORKDIR /app
COPY --from=builder /app/main .
CMD [ "/app/main" ]
46 changes: 43 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
version: '3.8'

services:
api:
build: .
ports:
- 127.0.0.1:8080:8080
environment:
- DB_HOST=psql
- DB_USERNAME=${DB_USERNAME}
- DB_PASSWORD=${DB_PASSWORD}
- DB_DATABASE=${DB_DATABASE}
- DB_PORT=${DB_PORT}
- PORT=8080
- ENV="production"
depends_on:
- psql
- migrate
restart: always
networks:
- triton-network

psql:
image: postgres:latest
environment:
Expand All @@ -9,8 +26,31 @@ services:
POSTGRES_PASSWORD: ${DB_PASSWORD}
ports:
- "${DB_PORT}:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- psql_volume:/var/lib/postgresql/data
networks:
- triton-network

migrate:
image: migrate/migrate
depends_on:
- psql
restart: on-failure
volumes:
- ./internal/database/migrations:/migrations
command: ["-path", "/migrations", "-database", "postgres://${DB_USERNAME}:${DB_PASSWORD}@psql:${DB_PORT}/${DB_DATABASE}?sslmode=disable", "up"]
links:
- psql
networks:
- triton-network

volumes:
psql_volume:
psql_volume:

networks:
triton-network:
7 changes: 7 additions & 0 deletions docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
How to use
1. Install docker
2. go to the directory which has the Dockerfile in it (spotify collab backend)
3. run `docker compose up -d` [-d for detached mode]
4. Let everything build and run `docker compose ps` to confirm its running (you should see both psql and api)
5. Send requests on 127.0.0.1:8080
6. Check [routes](.\internal\server\routes.go) folder for the routes
3 changes: 1 addition & 2 deletions internal/controllers/v1/pastebin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func (p *PastebinHandler) GetPastebin(c *gin.Context) {
}

q := database.New(p.db)
pastebin, err :=
q.GetPastebin(c, input.URL)
pastebin, err := q.GetPastebin(c, input.URL)
if errors.Is(err, pgx.ErrNoRows) {
merrors.NotFound(c, "This page does not exist!")
return
Expand Down
18 changes: 9 additions & 9 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type Service struct {
}

var (
database = os.Getenv("DB_DATABASE")
password = os.Getenv("DB_PASSWORD")
username = os.Getenv("DB_USERNAME")
port = os.Getenv("DB_PORT")
host = os.Getenv("DB_HOST")
schema = os.Getenv("DB_SCHEMA")
database = os.Getenv("DB_DATABASE")
password = os.Getenv("DB_PASSWORD")
username = os.Getenv("DB_USERNAME")
port = os.Getenv("DB_PORT")
host = os.Getenv("DB_HOST")
// schema = os.Getenv("DB_SCHEMA")
dbInstance *pgxpool.Pool
)

Expand All @@ -42,7 +42,7 @@ func NewService() *pgxpool.Pool {
if dbInstance != nil {
return dbInstance
}
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable&search_path=%s", username, password, host, port, database, schema)
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", username, password, host, port, database)
db, err := pgxpool.New(context.Background(), connStr)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -75,10 +75,10 @@ func (s *Service) Health() map[string]string {

// Get database stats (like open connections, in use, idle, etc.)
// dbStats := s.db.Stats()
_ = s.Db.Stat()
// dbstats := s.Db.Stat()
// stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
// stats["in_use"] = strconv.Itoa(dbStats.InUse)
// stats["idle"] = strconv.Itoa(dbStats.Idle)
// stats["idle"] = strconv.Itoa(int(dbstats.IdleConns()))
// stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
// stats["wait_duration"] = dbStats.WaitDuration.String()
// stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewServer() *http.Server {

// Declare Server config
server := &http.Server{
Addr: fmt.Sprintf("localhost:%d", NewServer.port),
Addr: fmt.Sprintf(":%d", NewServer.port),
Handler: NewServer.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
Expand Down

0 comments on commit 09e050b

Please sign in to comment.