Skip to content

Commit

Permalink
fix: fix CI/CD env of Backend pixel (#75)
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
  • Loading branch information
EjembiEmmanuel authored Aug 28, 2024
1 parent aee9172 commit c81be6e
Show file tree
Hide file tree
Showing 44 changed files with 358 additions and 231 deletions.
6 changes: 6 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REDIS_HOST=localhost
REDIS_PORT=6379
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_DATABASE=postgres
21 changes: 20 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ RUN curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master
RUN /bin/bash /root/.local/bin/snfoundryup --version 0.21.0

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

# Copy over the scripts
Expand All @@ -21,6 +20,26 @@ COPY ./backend/go.mod ./backend/go.sum ./
RUN go mod download
COPY ./backend .

# 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}

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

Expand Down
21 changes: 20 additions & 1 deletion backend/Dockerfile.consumer
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FROM golang:1.22.2-alpine
RUN apk add --no-cache bash curl git jq

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

# Copy over the app
Expand All @@ -12,6 +11,26 @@ COPY /go.mod /go.sum ./
RUN go mod download
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}

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

Expand Down
21 changes: 20 additions & 1 deletion backend/Dockerfile.consumer.prod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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-database.config.json ./database.config.json
COPY ./configs/prod-backend.config.json ./backend.config.json

# Copy over the app
Expand All @@ -12,6 +11,26 @@ COPY ./go.mod ./go.sum ./
RUN go mod download
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}

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

Expand Down
21 changes: 20 additions & 1 deletion backend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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-database.config.json ./database.config.json
COPY ./configs/prod-backend.config.json ./backend.config.json

# Copy over the app
Expand All @@ -12,6 +11,26 @@ COPY ./backend/go.mod ./backend/go.sum ./
RUN go mod download
COPY ./backend .

# 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}

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

Expand Down
36 changes: 32 additions & 4 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# art/peace Backend
# AFK Backend

This directory contains the Go backend for `art/peace`, which provides routes for managing and retrieving `art/peace` info from the Redis and Postgres DBs. Also, it contains other utilities to do things like get the contract address, use devnet transaction invoke scripts for easy devnet testing, maintain websocket connections for pixel updates, and more.
This directory contains the Go backend for `AFK`, which provides routes for managing and retrieving `AFK` info from the Redis and Postgres DBs. Also, it contains other utilities to do things like get the contract address, use devnet transaction invoke scripts for easy devnet testing, maintain websocket connections for pixel updates, and more.

## Running
## Environment Variables

The following environment variables need to be set in `.env`

```sh
REDIS_HOST=your_redis_host
REDIS_PORT=your_redis_port
POSTGRES_HOST=your_postgres_host
POSTGRES_PORT=your_postgres_port
POSTGRES_USER=your_postgres_user
POSTGRES_DATABASE=your_postgres_database

```

## Running

```sh
go run main.go

go run ./cmd/backend/backend.go
Expand All @@ -13,11 +27,25 @@ go run ./cmd/backend/backend.go

## Build

```
```sh
go mod download
go build
```

## Using Docker

Build the image

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

Run the container

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

## TODO
- [] Fix env Database Redis and Postgres in DockerFile
- [] More coming soon
21 changes: 14 additions & 7 deletions backend/cmd/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package main

import (
"flag"
"log"

"github.com/keep-starknet-strange/art-peace/backend/config"
"github.com/keep-starknet-strange/art-peace/backend/core"
"github.com/keep-starknet-strange/art-peace/backend/routes"
"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/joho/godotenv"
)

func isFlagSet(name string) bool {
Expand All @@ -19,8 +22,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")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database 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 @@ -32,7 +39,7 @@ func main() {
panic(err)
}

databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename)
databaseConfig, err := config.LoadDatabaseConfig()
if err != nil {
panic(err)
}
Expand All @@ -49,9 +56,9 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, *admin)
core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, *admin)

routes.InitRoutes()

core.ArtPeaceBackend.Start(core.ArtPeaceBackend.BackendConfig.Port)
core.AFKBackend.Start(core.AFKBackend.BackendConfig.Port)
}
15 changes: 7 additions & 8 deletions backend/cmd/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"flag"

"github.com/keep-starknet-strange/art-peace/backend/config"
"github.com/keep-starknet-strange/art-peace/backend/core"
"github.com/keep-starknet-strange/art-peace/backend/routes"
"github.com/keep-starknet-strange/art-peace/backend/routes/indexer"
"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"
)

func isFlagSet(name string) bool {
Expand All @@ -21,7 +21,6 @@ func isFlagSet(name string) bool {

func main() {
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
production := flag.Bool("production", false, "Production mode")

Expand All @@ -32,7 +31,7 @@ func main() {
panic(err)
}

databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename)
databaseConfig, err := config.LoadDatabaseConfig()
if err != nil {
panic(err)
}
Expand All @@ -49,13 +48,13 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, false)
core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, false)

routes.InitBaseRoutes()
indexer.InitIndexerRoutes()
routes.InitWebsocketRoutes()
routes.InitNFTStaticRoutes()
indexer.StartMessageProcessor()

core.ArtPeaceBackend.Start(core.ArtPeaceBackend.BackendConfig.ConsumerPort)
core.AFKBackend.Start(core.AFKBackend.BackendConfig.ConsumerPort)
}
15 changes: 7 additions & 8 deletions backend/cmd/video-gen/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package main
import (
"flag"

"github.com/keep-starknet-strange/art-peace/backend/config"
"github.com/keep-starknet-strange/art-peace/backend/core"
"github.com/keep-starknet-strange/art-peace/backend/routes"
"github.com/keep-starknet-strange/art-peace/backend/routes/indexer"
"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"
)

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

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

databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename)
databaseConfig, err := config.LoadDatabaseConfig()
if err != nil {
panic(err)
}
Expand All @@ -34,12 +33,12 @@ func main() {
databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig, true)
core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, true)

routes.InitBaseRoutes()
routes.InitCanvasRoutes()
indexer.InitIndexerRoutes()
indexer.StartMessageProcessor()

core.ArtPeaceBackend.Start(core.ArtPeaceBackend.BackendConfig.ConsumerPort)
core.AFKBackend.Start(core.AFKBackend.BackendConfig.ConsumerPort)
}
Loading

0 comments on commit c81be6e

Please sign in to comment.