Skip to content

Commit

Permalink
fix: xix backend Golang
Browse files Browse the repository at this point in the history
  • Loading branch information
EjembiEmmanuel committed Aug 28, 2024
1 parent effad94 commit 6d99ce0
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 120 deletions.
6 changes: 6 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
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
6 changes: 4 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ 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
Expand All @@ -32,12 +32,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
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"]
4 changes: 2 additions & 2 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ 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
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
12 changes: 10 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 All @@ -51,6 +58,7 @@ func main() {
core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, false)

routes.InitBaseRoutes()
routes.InitColorsRoutes()
indexer.InitIndexerRoutes()
routes.InitWebsocketRoutes()
routes.InitNFTStaticRoutes()
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
141 changes: 68 additions & 73 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,94 @@
package config

import (
"encoding/json"
"fmt"
"os"
"strconv"
)

type BackendScriptsConfig struct {
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
ClaimTodayQuestDevnet string `json:"claim_today_quest_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
LikeNFTDevnet string `json:"like_nft_devnet"`
UnlikeNFTDevnet string `json:"unlike_nft_devnet"`
VoteColorDevnet string `json:"vote_color_devnet"`
NewUsernameDevnet string `json:"new_username_devnet"`
ChangeUsernameDevnet string `json:"change_username_devnet"`
IncreaseDayDevnet string `json:"increase_day_devnet"`
JoinChainFactionDevnet string `json:"join_chain_faction_devnet"`
JoinFactionDevnet string `json:"join_faction_devnet"`
LeaveFactionDevnet string `json:"leave_faction_devnet"`
AddFactionTemplateDevnet string `json:"add_faction_template_devnet"`
RemoveFactionTemplateDevnet string `json:"remove_faction_template_devnet"`
PlacePixelDevnet string
PlaceExtraPixelsDevnet string
AddTemplateDevnet string
ClaimTodayQuestDevnet string
MintNFTDevnet string
LikeNFTDevnet string
UnlikeNFTDevnet string
VoteColorDevnet string
NewUsernameDevnet string
ChangeUsernameDevnet string
IncreaseDayDevnet string
JoinChainFactionDevnet string
JoinFactionDevnet string
LeaveFactionDevnet string
AddFactionTemplateDevnet string
RemoveFactionTemplateDevnet string
}

type WebSocketConfig struct {
ReadBufferSize int `json:"read_buffer_size"`
WriteBufferSize int `json:"write_buffer_size"`
ReadBufferSize int
WriteBufferSize int
}

type HttpConfig struct {
AllowOrigin []string `json:"allow_origin"`
AllowMethods []string `json:"allow_methods"`
AllowHeaders []string `json:"allow_headers"`
AllowOrigin []string
AllowMethods []string
AllowHeaders []string
}

type BackendConfig struct {
Host string `json:"host"`
Port int `json:"port"`
ConsumerPort int `json:"consumer_port"`
Scripts BackendScriptsConfig `json:"scripts"`
Production bool `json:"production"`
WebSocket WebSocketConfig `json:"websocket"`
Http HttpConfig `json:"http_config"`
Host string
Port int
ConsumerPort int
Scripts BackendScriptsConfig
Production bool
WebSocket WebSocketConfig
Http HttpConfig
}

var DefaultBackendConfig = BackendConfig{
Host: "localhost",
Port: 8080,
ConsumerPort: 8081,
Scripts: BackendScriptsConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
ClaimTodayQuestDevnet: "../scripts/claim_today_quest.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
LikeNFTDevnet: "../scripts/like_nft.sh",
UnlikeNFTDevnet: "../scripts/unlike_nft.sh",
VoteColorDevnet: "../scripts/vote_color.sh",
NewUsernameDevnet: "../scripts/new_username.sh",
ChangeUsernameDevnet: "../scripts/change_username.sh",
IncreaseDayDevnet: "../scripts/increase_day_index.sh",
JoinChainFactionDevnet: "../scripts/join_chain_faction.sh",
JoinFactionDevnet: "../scripts/join_faction.sh",
LeaveFactionDevnet: "../scripts/leave_faction.sh",
AddFactionTemplateDevnet: "../scripts/add_faction_template.sh",
RemoveFactionTemplateDevnet: "../scripts/remove_faction_template.sh",
},
Production: false,
WebSocket: WebSocketConfig{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
Http: HttpConfig{
AllowOrigin: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Content-Type"},
},
}

var DefaultBackendConfigPath = "./configs/backend.config.json"

func LoadBackendConfig(backendConfigPath string) (*BackendConfig, error) {
file, err := os.Open(backendConfigPath)
func LoadBackendConfig() (*BackendConfig, error) {
backendPort, err := strconv.Atoi(os.Getenv("BACKEND_PORT"))
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid BACKEND_PORT: %v", err)
}
defer file.Close()

decoder := json.NewDecoder(file)
config := BackendConfig{}
err = decoder.Decode(&config)
consumerPort, err := strconv.Atoi(os.Getenv("CONSUMER_PORT"))
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid BACKEND_PORT: %v", err)
}

config := BackendConfig{
Host: os.Getenv("BACKEND_HOST"),
Port: backendPort,
ConsumerPort: consumerPort,
Scripts: BackendScriptsConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
ClaimTodayQuestDevnet: "../scripts/claim_today_quest.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
LikeNFTDevnet: "../scripts/like_nft.sh",
UnlikeNFTDevnet: "../scripts/unlike_nft.sh",
VoteColorDevnet: "../scripts/vote_color.sh",
NewUsernameDevnet: "../scripts/new_username.sh",
ChangeUsernameDevnet: "../scripts/change_username.sh",
IncreaseDayDevnet: "../scripts/increase_day_index.sh",
JoinChainFactionDevnet: "../scripts/join_chain_faction.sh",
JoinFactionDevnet: "../scripts/join_faction.sh",
LeaveFactionDevnet: "../scripts/leave_faction.sh",
AddFactionTemplateDevnet: "../scripts/add_faction_template.sh",
RemoveFactionTemplateDevnet: "../scripts/remove_faction_template.sh",
},
Production: false,
WebSocket: WebSocketConfig{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
Http: HttpConfig{
AllowOrigin: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Content-Type"},
},
}
return &config, nil
}
Loading

0 comments on commit 6d99ce0

Please sign in to comment.