Skip to content

Commit

Permalink
fix: fix backend Golang (#92)
Browse files Browse the repository at this point in the history
* refac: change `go.mod` module url

* refac: refactor Dockerfile.consumer.prod

refactor Dockerfile.consumer.prod to use ENV variables for Redis and
Postgres connection

* fix: fix github imports

* refac: use ENV variables in `consumer.go`

* refac: replace all ArtPeaceBackend with AFKBackend

* refac: remove unused database configs

* feat: add .env.example

* feat: load env variables

* chore: update README

* feat: use ENV variables in Dockerfile

* feat: use ENV variables in Dockerfile.consumer

* feat: use ENV variables in Dockerfile.prod

* fix: xix backend Golang

* refac: modify consumer.go

* refac: move production mode to env
  • Loading branch information
EjembiEmmanuel authored Aug 29, 2024
1 parent 06b407f commit eae6c49
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 119 deletions.
10 changes: 10 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
REDIS_HOST=localhost
REDIS_PORT=6379

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DATABASE=postgres

BACKEND_HOST=localhost
BACKEND_PORT=8082
CONSUMER_PORT=8081

PRODUCTION=false

BACKEND_URL=https://backend-pixel.onrender.com/
26 changes: 24 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,31 @@ COPY ./tests/integration/docker/ .

# Copy over the app
WORKDIR /app
COPY ./backend/go.mod ./backend/go.sum ./
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./backend .
COPY ./ .

# Argument for Redis host and port
ARG REDIS_HOST
ARG REDIS_PORT

# Set Redis host and port environment variables
ENV REDIS_HOST=${REDIS_HOST}
ENV REDIS_PORT=${REDIS_PORT}

# Argument for Postgres host, port, user, and database
ARG POSTGRES_HOST
ARG POSTGRES_PORT
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DATABASE

# Set Postgres host, port, user, and database environment variables
ENV POSTGRES_HOST=${POSTGRES_HOST}
ENV POSTGRES_PORT=${POSTGRES_PORT}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DATABASE=${POSTGRES_DATABASE}

# Argument for Redis host and port
ARG REDIS_HOST
Expand Down
2 changes: 2 additions & 0 deletions backend/Dockerfile.consumer
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ ENV REDIS_PORT=${REDIS_PORT}
ARG POSTGRES_HOST
ARG POSTGRES_PORT
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DATABASE

# Set Postgres host, port, user, and database environment variables
ENV POSTGRES_HOST=${POSTGRES_HOST}
ENV POSTGRES_PORT=${POSTGRES_PORT}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DATABASE=${POSTGRES_DATABASE}

# Build the app & run it
Expand Down
21 changes: 15 additions & 6 deletions backend/Dockerfile.consumer.prod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ FROM --platform=linux/amd64 golang:1.22.2-alpine

RUN apk add --no-cache bash curl git jq

# Copy over the configs
COPY ./configs/prod-backend.config.json ./backend.config.json

# Copy over the app
WORKDIR /app
COPY ./go.mod ./go.sum ./
Expand All @@ -19,21 +16,33 @@ ARG REDIS_PORT
ENV REDIS_HOST=${REDIS_HOST}
ENV REDIS_PORT=${REDIS_PORT}

# Argument for Postgres host, port, user, and database
# Argument for Postgres host, port, user, password, and database
ARG POSTGRES_HOST
ARG POSTGRES_PORT
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DATABASE

# Set Postgres host, port, user, and database environment variables
# Set Postgres host, port, user, password and database environment variables
ENV POSTGRES_HOST=${POSTGRES_HOST}
ENV POSTGRES_PORT=${POSTGRES_PORT}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DATABASE=${POSTGRES_DATABASE}

# Argument for Backend host, port, and consumer port
ARG BACKEND_HOST
ARG BACKEND_PORT
ARG CONSUMER_PORT

# Set Backend host, port, and consumer port environment variables
ENV BACKEND_HOST=${BACKEND_HOST}
ENV BACKEND_PORT=${BACKEND_PORT}
ENV CONSUMER_PORT=${CONSUMER_PORT}

# Build the app & run it
RUN go build -o consumer ./cmd/consumer/consumer.go

EXPOSE 8082
EXPOSE 8081

CMD ["./consumer"]
24 changes: 22 additions & 2 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ COPY ./configs/prod-backend.config.json ./backend.config.json

# Copy over the app
WORKDIR /app
COPY ./backend/go.mod ./backend/go.sum ./
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./backend .
COPY ./ .

# Argument for Redis host and port
ARG REDIS_HOST
ARG REDIS_PORT

# Set Redis host and port environment variables
ENV REDIS_HOST=${REDIS_HOST}
ENV REDIS_PORT=${REDIS_PORT}

# Argument for Postgres host, port, user, and database
ARG POSTGRES_HOST
ARG POSTGRES_PORT
ARG POSTGRES_USER
ARG POSTGRES_DATABASE

# Set Postgres host, port, user, and database environment variables
ENV POSTGRES_HOST=${POSTGRES_HOST}
ENV POSTGRES_PORT=${POSTGRES_PORT}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_DATABASE=${POSTGRES_DATABASE}

# Argument for Redis host and port
ARG REDIS_HOST
Expand Down
5 changes: 3 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ REDIS_PORT=your_redis_port
POSTGRES_HOST=your_postgres_host
POSTGRES_PORT=your_postgres_port
POSTGRES_USER=your_postgres_user
POSTGRES_PASSWORD=your_postgres_password
POSTGRES_DATABASE=your_postgres_database

```
Expand All @@ -37,13 +38,13 @@ go build
Build the image

```sh
docker build -f Dockerfile.consumer.prod -t afk-backend .
docker build -f Dockerfile.consumer.prod -t consumer-app .
```

Run the container

```sh
docker run --env-file .env -d -p 8082:8082 --name afk-backend afk-backend
docker run --env-file .env -d -p 8081:8081 --name consumer-app consumer-app
```

## TODO
Expand Down
11 changes: 5 additions & 6 deletions backend/cmd/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ func isFlagSet(name string) bool {

func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
if err != nil {
log.Fatal("Error loading .env file")
}

canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
production := flag.Bool("production", false, "Production mode")
admin := flag.Bool("admin", false, "Admin mode")

Expand All @@ -44,7 +43,7 @@ func main() {
panic(err)
}

backendConfig, err := config.LoadBackendConfig(*backendConfigFilename)
backendConfig, err := config.LoadBackendConfig()
if err != nil {
panic(err)
}
Expand Down
11 changes: 9 additions & 2 deletions backend/cmd/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package main

import (
"flag"
"log"

"github.com/AFK-AlignedFamKernel/afk_monorepo/backend/config"
"github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core"
"github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes"
"github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/indexer"

"github.com/joho/godotenv"
)

func isFlagSet(name string) bool {
Expand All @@ -20,8 +23,12 @@ func isFlagSet(name string) bool {
}

func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}

canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
production := flag.Bool("production", false, "Production mode")

flag.Parse()
Expand All @@ -36,7 +43,7 @@ func main() {
panic(err)
}

backendConfig, err := config.LoadBackendConfig(*backendConfigFilename)
backendConfig, err := config.LoadBackendConfig()
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions backend/cmd/video-gen/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

func main() {
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")

flag.Parse()

Expand All @@ -25,7 +24,7 @@ func main() {
panic(err)
}

backendConfig, err := config.LoadBackendConfig(*backendConfigFilename)
backendConfig, err := config.LoadBackendConfig()
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit eae6c49

Please sign in to comment.