Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@ services:
networks:
- backend

sc-api-getaway:
build: ./sc-api-getaway
container_name: api-gateway
sc-auth:
build:
context: .
dockerfile: sc-auth/Dockerfile
container_name: sc-auth
depends_on:
- postgres
environment:
- DATABASE_URL=postgresql://root:secret@localhost:5433/sc_db?sslmode=disable
SERVER_PORT: "50051"
DB_SOURCE: postgresql://root:secret@postgres:5432/sc_db?sslmode=disable
ports:
- "8000:8000"
- "50051:50051"
networks:
- backend

nginx:
build: ./sc-api-getaway
container_name: nginx
sc-api-getaway:
build:
context: .
dockerfile: sc-api-getaway/Dockerfile
container_name: api-gateway
depends_on:
- postgres
- sc-auth
environment:
SERVER_PORT: "8000"
DB_SOURCE: postgresql://root:secret@postgres:5432/sc_db?sslmode=disable
GRPC_AUTH_PORT: sc-auth:50051
ports:
- "80:80"
- "8000:8000"
networks:
- backend

Expand Down Expand Up @@ -88,4 +100,4 @@ networks:
backend:

volumes:
pgdata:
pgdata:
25 changes: 20 additions & 5 deletions sc-api-getaway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
FROM nginx:alpine
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY cmd/general /usr/share/nginx/html
WORKDIR /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
# syntax=docker/dockerfile:1.4

FROM golang:1.22 AS builder
WORKDIR /src

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/api-gateway ./sc-api-getaway/cmd/general

FROM gcr.io/distroless/base-debian12:nonroot AS final
WORKDIR /app

COPY --from=builder /out/api-gateway /app/api-gateway

EXPOSE 8000

ENTRYPOINT ["/app/api-gateway"]
3 changes: 2 additions & 1 deletion sc-api-getaway/cmd/general/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
os.Exit(1)
}

authConn, err := grpc.NewClient("localhost"+":"+newConfig.GrpcAuthPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.Dial(newConfig.GrpcAuthTarget, grpc.WithTransportCredentials(insecure.NewCredentials()))

if err != nil {
log.Fatalf("failed to connect to Auth Service: %v", err)
}
defer authConn.Close()

Check failure on line 34 in sc-api-getaway/cmd/general/main.go

View workflow job for this annotation

GitHub Actions / Lint and Test Backend

undefined: authConn

postConn, err := grpc.NewClient("localhost"+":"+newConfig.GrpcPostPort, grpc.WithTransportCredentials(insecure.NewCredentials()))

Check failure on line 36 in sc-api-getaway/cmd/general/main.go

View workflow job for this annotation

GitHub Actions / Lint and Test Backend

newConfig.GrpcPostPort undefined (type config.Config has no field or method GrpcPostPort) (typecheck)
if err != nil {
log.Fatalf("failed to connect to Post Service: %v", err)
}
Expand Down
19 changes: 11 additions & 8 deletions sc-api-getaway/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package config

import (
"errors"
"log"

"github.com/spf13/viper"
)

type Config struct {
EnvType string `mapstructure:"ENV_TYPE"`
ServerPort string `mapstructure:"SERVER_PORT"`
DBSource string `mapstructure:"DB_SOURCE"`
GrpcAuthPort string `mapstructure:"GPRC_AUTH_PORT"`
GrpcPostPort string `mapstructure:"GRPC_POST_PORT"`
WebappBaseUrl string `mapstructure:"WEBAPP_BASE_URL"`
EnvType string `mapstructure:"ENV_TYPE"`
ServerPort string `mapstructure:"SERVER_PORT"`
DBSource string `mapstructure:"DB_SOURCE"`
GrpcAuthTarget string `mapstructure:"GRPC_AUTH_PORT"`
WebappBaseUrl string `mapstructure:"WEBAPP_BASE_URL"`
}

func LoadConfig(path string) (config Config, err error) {
Expand All @@ -22,8 +22,11 @@ func LoadConfig(path string) (config Config, err error) {

err = viper.ReadInConfig()
if err != nil {
log.Fatalf("could not loadconfig: %v", err)
return
var configErr viper.ConfigFileNotFoundError
if !errors.As(err, &configErr) {
log.Fatalf("could not loadconfig: %v", err)
return
}
}

err = viper.Unmarshal(&config)
Expand Down
29 changes: 26 additions & 3 deletions sc-auth/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
FROM ubuntu:latest
LABEL authors="karadyauran"
# syntax=docker/dockerfile:1.4

ENTRYPOINT ["top", "-b"]
FROM golang:1.22 AS builder
WORKDIR /src

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/auth-service ./sc-auth/cmd/general
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/migrate github.com/golang-migrate/migrate/v4/cmd/migrate
Comment thread
karadyaur marked this conversation as resolved.

FROM busybox:1.36.1 AS busybox

FROM gcr.io/distroless/base-debian12:nonroot AS final
WORKDIR /app

COPY --from=builder /out/auth-service /app/auth-service
COPY --from=builder /out/migrate /bin/migrate
COPY --from=builder /src/sc-auth/internal/db/migration /app/internal/db/migration
COPY --from=busybox /bin/busybox /busybox
COPY --chmod=0755 sc-auth/entrypoint.sh /entrypoint.sh

ENV PATH="/bin:${PATH}"

ENTRYPOINT ["/entrypoint.sh"]
13 changes: 6 additions & 7 deletions sc-auth/entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/bin/sh
#!/busybox/sh
Comment thread
karadyaur marked this conversation as resolved.
set -euo pipefail

go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
migrate create -ext sql -dir internal/db/migration -seq init_schema
MIGRATION_DIR="${MIGRATION_DIR:-/app/internal/db/migration}"
DB_SOURCE="${DB_SOURCE:?DB_SOURCE environment variable is required}"

# Perform migrations
migrate -path internal/db/migration -database "postgresql://root:secret@postgres-auth:5432/sc_db?sslmode=disable" -verbose up
/bin/migrate -path "${MIGRATION_DIR}" -database "${DB_SOURCE}" up

# Start the server
./main
exec /app/auth-service
8 changes: 6 additions & 2 deletions sc-auth/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"log"

"github.com/spf13/viper"
Expand All @@ -19,8 +20,11 @@ func LoadConfig(path string) (config Config, err error) {

err = viper.ReadInConfig()
if err != nil {
log.Fatalf("could not loadconfig: %v", err)
return
var configErr viper.ConfigFileNotFoundError
if !errors.As(err, &configErr) {
log.Fatalf("could not loadconfig: %v", err)
return
}
}

err = viper.Unmarshal(&config)
Expand Down
Loading