From 09e050b86d1a4d91d4fa20ee4c8153f67233cee3 Mon Sep 17 00:00:00 2001 From: Aditya-Chowdhary Date: Thu, 15 Aug 2024 01:23:22 +0530 Subject: [PATCH] build: dockerised --- Dockerfile | 18 ++++++++ docker-compose.yml | 46 ++++++++++++++++++-- docker.md | 7 +++ internal/controllers/v1/pastebin/handlers.go | 3 +- internal/database/database.go | 18 ++++---- internal/server/server.go | 2 +- 6 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 Dockerfile create mode 100644 docker.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b8440eb --- /dev/null +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0a98c00..13a68d5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: @@ -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: \ No newline at end of file + psql_volume: + +networks: + triton-network: \ No newline at end of file diff --git a/docker.md b/docker.md new file mode 100644 index 0000000..9d3462d --- /dev/null +++ b/docker.md @@ -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 \ No newline at end of file diff --git a/internal/controllers/v1/pastebin/handlers.go b/internal/controllers/v1/pastebin/handlers.go index 0279b97..e1eeb36 100644 --- a/internal/controllers/v1/pastebin/handlers.go +++ b/internal/controllers/v1/pastebin/handlers.go @@ -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 diff --git a/internal/database/database.go b/internal/database/database.go index 9c38c33..beca4fb 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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 ) @@ -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) @@ -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) diff --git a/internal/server/server.go b/internal/server/server.go index ed71a35..fa3967d 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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,