From 8dfe2ba954eeb8df952c7e4941b1d09b9f078110 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Tue, 27 Aug 2024 15:08:39 +0100 Subject: [PATCH 01/15] refac: change `go.mod` module url --- backend/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/go.mod b/backend/go.mod index 72f1c2ed..d440a9b9 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,4 +1,4 @@ -module github.com/keep-starknet-strange/art-peace/backend +module github.com/AFK-AlignedFamKernel/afk_monorepo/backend go 1.22.0 From 90b49001aebf41389a4f8be66432a870ccc813a3 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Tue, 27 Aug 2024 15:48:02 +0100 Subject: [PATCH 02/15] refac: refactor Dockerfile.consumer.prod refactor Dockerfile.consumer.prod to use ENV variables for Redis and Postgres connection --- backend/Dockerfile.consumer.prod | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/Dockerfile.consumer.prod b/backend/Dockerfile.consumer.prod index 24716bf9..1ae468fd 100644 --- a/backend/Dockerfile.consumer.prod +++ b/backend/Dockerfile.consumer.prod @@ -12,6 +12,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 From 04502954f8edfa6f3598e3feef329af2038088f7 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Tue, 27 Aug 2024 16:08:22 +0100 Subject: [PATCH 03/15] fix: fix github imports --- backend/cmd/backend/backend.go | 6 +++--- backend/cmd/consumer/consumer.go | 8 ++++---- backend/cmd/video-gen/video.go | 8 ++++---- backend/core/backend.go | 2 +- backend/core/databases.go | 2 +- backend/quests/claim.go | 2 +- backend/quests/quests.go | 2 +- backend/quests/status.go | 2 +- backend/routes/canvas.go | 4 ++-- backend/routes/colors.go | 4 ++-- backend/routes/contract.go | 4 ++-- backend/routes/factions.go | 4 ++-- backend/routes/indexer/color.go | 2 +- backend/routes/indexer/day.go | 2 +- backend/routes/indexer/faction.go | 2 +- backend/routes/indexer/nft.go | 4 ++-- backend/routes/indexer/nftTransfer.go | 2 +- backend/routes/indexer/pixel.go | 4 ++-- backend/routes/indexer/quest.go | 2 +- backend/routes/indexer/route.go | 2 +- backend/routes/indexer/template.go | 2 +- backend/routes/indexer/username.go | 2 +- backend/routes/indexer/votableColors.go | 2 +- backend/routes/indexer/vote.go | 2 +- backend/routes/nft.go | 4 ++-- backend/routes/pixel.go | 4 ++-- backend/routes/quests.go | 6 +++--- backend/routes/routes.go | 2 +- backend/routes/templates.go | 4 ++-- backend/routes/user.go | 4 ++-- backend/routes/utils/middleware.go | 2 +- backend/routes/utils/responses.go | 2 +- backend/routes/votableColors.go | 4 ++-- backend/routes/websocket.go | 2 +- backend/video/video.go | 2 +- 35 files changed, 56 insertions(+), 56 deletions(-) diff --git a/backend/cmd/backend/backend.go b/backend/cmd/backend/backend.go index 1821d2ee..5d567ec1 100644 --- a/backend/cmd/backend/backend.go +++ b/backend/cmd/backend/backend.go @@ -3,9 +3,9 @@ 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/AFK-AlignedFamKernel/afk_monorepo/backend/config" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes" ) func isFlagSet(name string) bool { diff --git a/backend/cmd/consumer/consumer.go b/backend/cmd/consumer/consumer.go index 68795959..9c63e556 100644 --- a/backend/cmd/consumer/consumer.go +++ b/backend/cmd/consumer/consumer.go @@ -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 { diff --git a/backend/cmd/video-gen/video.go b/backend/cmd/video-gen/video.go index 6620761d..9ee5b24b 100644 --- a/backend/cmd/video-gen/video.go +++ b/backend/cmd/video-gen/video.go @@ -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 main() { diff --git a/backend/core/backend.go b/backend/core/backend.go index 62c460db..77c28f35 100644 --- a/backend/core/backend.go +++ b/backend/core/backend.go @@ -7,7 +7,7 @@ import ( "github.com/gorilla/websocket" - "github.com/keep-starknet-strange/art-peace/backend/config" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/config" ) type Backend struct { diff --git a/backend/core/databases.go b/backend/core/databases.go index 25bbfdd6..af8d4ee3 100644 --- a/backend/core/databases.go +++ b/backend/core/databases.go @@ -10,7 +10,7 @@ import ( "github.com/jackc/pgx/v5/pgxpool" "github.com/redis/go-redis/v9" - "github.com/keep-starknet-strange/art-peace/backend/config" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/config" ) type Databases struct { diff --git a/backend/quests/claim.go b/backend/quests/claim.go index b716f39e..226d89e3 100644 --- a/backend/quests/claim.go +++ b/backend/quests/claim.go @@ -1,6 +1,6 @@ package quests -import "github.com/keep-starknet-strange/art-peace/backend/core" +import "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" var QuestClaimData = map[int]func(*Quest, string) []int{ NFTMintQuestType: NFTMintQuestClaimData, diff --git a/backend/quests/quests.go b/backend/quests/quests.go index 34714786..f5c5dcc8 100644 --- a/backend/quests/quests.go +++ b/backend/quests/quests.go @@ -1,6 +1,6 @@ package quests -import "github.com/keep-starknet-strange/art-peace/backend/core" +import "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" // Quest types const ( diff --git a/backend/quests/status.go b/backend/quests/status.go index b39b4453..df959f05 100644 --- a/backend/quests/status.go +++ b/backend/quests/status.go @@ -1,7 +1,7 @@ package quests import ( - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) var QuestChecks = map[int]func(*Quest, string) (int, int){ diff --git a/backend/routes/canvas.go b/backend/routes/canvas.go index a6d46858..bc908046 100644 --- a/backend/routes/canvas.go +++ b/backend/routes/canvas.go @@ -4,8 +4,8 @@ import ( "context" "net/http" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitCanvasRoutes() { diff --git a/backend/routes/colors.go b/backend/routes/colors.go index 956d0660..a94f7b90 100644 --- a/backend/routes/colors.go +++ b/backend/routes/colors.go @@ -4,8 +4,8 @@ import ( "context" "net/http" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitColorsRoutes() { diff --git a/backend/routes/contract.go b/backend/routes/contract.go index 2b857cb5..b39a67b1 100644 --- a/backend/routes/contract.go +++ b/backend/routes/contract.go @@ -7,8 +7,8 @@ import ( "os" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitContractRoutes() { diff --git a/backend/routes/factions.go b/backend/routes/factions.go index 76f36eb4..cd331116 100644 --- a/backend/routes/factions.go +++ b/backend/routes/factions.go @@ -9,8 +9,8 @@ import ( "os/exec" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitFactionRoutes() { diff --git a/backend/routes/indexer/color.go b/backend/routes/indexer/color.go index 171492d1..960ab535 100644 --- a/backend/routes/indexer/color.go +++ b/backend/routes/indexer/color.go @@ -4,7 +4,7 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processColorAddedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/day.go b/backend/routes/indexer/day.go index f34e034e..79b5ebbe 100644 --- a/backend/routes/indexer/day.go +++ b/backend/routes/indexer/day.go @@ -4,7 +4,7 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processNewDayEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/faction.go b/backend/routes/indexer/faction.go index 054f8ba6..21c82f4e 100644 --- a/backend/routes/indexer/faction.go +++ b/backend/routes/indexer/faction.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processFactionCreatedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/nft.go b/backend/routes/indexer/nft.go index c93ecca2..078f49ac 100644 --- a/backend/routes/indexer/nft.go +++ b/backend/routes/indexer/nft.go @@ -11,8 +11,8 @@ import ( "os" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func processNFTMintedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/nftTransfer.go b/backend/routes/indexer/nftTransfer.go index 47cf1c02..25145653 100644 --- a/backend/routes/indexer/nftTransfer.go +++ b/backend/routes/indexer/nftTransfer.go @@ -3,7 +3,7 @@ package indexer import ( "context" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processNFTTransferEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/pixel.go b/backend/routes/indexer/pixel.go index d660321a..4915d7cc 100644 --- a/backend/routes/indexer/pixel.go +++ b/backend/routes/indexer/pixel.go @@ -4,8 +4,8 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func processPixelPlacedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/quest.go b/backend/routes/indexer/quest.go index c6d9929a..d53d392f 100644 --- a/backend/routes/indexer/quest.go +++ b/backend/routes/indexer/quest.go @@ -4,7 +4,7 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processDailyQuestClaimedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/route.go b/backend/routes/indexer/route.go index 3687b1f0..82490e56 100644 --- a/backend/routes/indexer/route.go +++ b/backend/routes/indexer/route.go @@ -6,7 +6,7 @@ import ( "sync" "time" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitIndexerRoutes() { diff --git a/backend/routes/indexer/template.go b/backend/routes/indexer/template.go index 17fc7cd7..4849422a 100644 --- a/backend/routes/indexer/template.go +++ b/backend/routes/indexer/template.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processTemplateAddedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/username.go b/backend/routes/indexer/username.go index 912ecf0c..3ad62f31 100644 --- a/backend/routes/indexer/username.go +++ b/backend/routes/indexer/username.go @@ -4,7 +4,7 @@ import ( "context" "encoding/hex" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processUsernameClaimedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/votableColors.go b/backend/routes/indexer/votableColors.go index 584ab277..d0a4deda 100644 --- a/backend/routes/indexer/votableColors.go +++ b/backend/routes/indexer/votableColors.go @@ -4,7 +4,7 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processVotableColorAddedEvent(event IndexerEvent) { diff --git a/backend/routes/indexer/vote.go b/backend/routes/indexer/vote.go index a7ae6077..b865eeca 100644 --- a/backend/routes/indexer/vote.go +++ b/backend/routes/indexer/vote.go @@ -4,7 +4,7 @@ import ( "context" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func processVoteColorEvent(event IndexerEvent) { diff --git a/backend/routes/nft.go b/backend/routes/nft.go index f754c139..3a6abed5 100644 --- a/backend/routes/nft.go +++ b/backend/routes/nft.go @@ -7,8 +7,8 @@ import ( "os/exec" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitNFTRoutes() { diff --git a/backend/routes/pixel.go b/backend/routes/pixel.go index 223e42fb..76770728 100644 --- a/backend/routes/pixel.go +++ b/backend/routes/pixel.go @@ -7,8 +7,8 @@ import ( "os/exec" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitPixelRoutes() { diff --git a/backend/routes/quests.go b/backend/routes/quests.go index 8df85bda..2bdc6aee 100644 --- a/backend/routes/quests.go +++ b/backend/routes/quests.go @@ -10,9 +10,9 @@ import ( "strconv" "time" - "github.com/keep-starknet-strange/art-peace/backend/core" - "github.com/keep-starknet-strange/art-peace/backend/quests" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/quests" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) type DailyUserQuest struct { diff --git a/backend/routes/routes.go b/backend/routes/routes.go index 6c6bf116..27f0fe82 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go @@ -3,7 +3,7 @@ package routes import ( "net/http" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitBaseRoutes() { diff --git a/backend/routes/templates.go b/backend/routes/templates.go index e36a5d17..40f7ac69 100644 --- a/backend/routes/templates.go +++ b/backend/routes/templates.go @@ -15,8 +15,8 @@ import ( "strconv" "strings" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitTemplateRoutes() { diff --git a/backend/routes/user.go b/backend/routes/user.go index 33ecbd20..ffd8ae36 100644 --- a/backend/routes/user.go +++ b/backend/routes/user.go @@ -8,8 +8,8 @@ import ( "strconv" "time" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) func InitUserRoutes() { diff --git a/backend/routes/utils/middleware.go b/backend/routes/utils/middleware.go index a52822c6..6a8b1737 100644 --- a/backend/routes/utils/middleware.go +++ b/backend/routes/utils/middleware.go @@ -3,7 +3,7 @@ package routeutils import ( "net/http" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) // Middleware functions for routes diff --git a/backend/routes/utils/responses.go b/backend/routes/utils/responses.go index 35e1837d..f3f4c571 100644 --- a/backend/routes/utils/responses.go +++ b/backend/routes/utils/responses.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/gorilla/websocket" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func SetupAccessHeaders(w http.ResponseWriter) { diff --git a/backend/routes/votableColors.go b/backend/routes/votableColors.go index 7fa93be5..a6a76936 100644 --- a/backend/routes/votableColors.go +++ b/backend/routes/votableColors.go @@ -7,8 +7,8 @@ import ( "os/exec" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" - routeutils "github.com/keep-starknet-strange/art-peace/backend/routes/utils" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" + routeutils "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/routes/utils" ) type VotableColor struct { diff --git a/backend/routes/websocket.go b/backend/routes/websocket.go index b65e14dc..d2067b95 100644 --- a/backend/routes/websocket.go +++ b/backend/routes/websocket.go @@ -6,7 +6,7 @@ import ( "github.com/gorilla/websocket" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func InitWebsocketRoutes() { diff --git a/backend/video/video.go b/backend/video/video.go index d98ded8f..e2751c1d 100644 --- a/backend/video/video.go +++ b/backend/video/video.go @@ -9,7 +9,7 @@ import ( "os" "strconv" - "github.com/keep-starknet-strange/art-peace/backend/core" + "github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core" ) func GenerateImageFromCanvas(orderId int) { From 1e3c2758cfc1d9ac65e4582bbe0476903309e2b0 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Tue, 27 Aug 2024 21:30:13 +0100 Subject: [PATCH 04/15] refac: use ENV variables in `consumer.go` --- backend/cmd/consumer/consumer.go | 3 +-- backend/config/database.go | 33 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/backend/cmd/consumer/consumer.go b/backend/cmd/consumer/consumer.go index 9c63e556..c65db8fd 100644 --- a/backend/cmd/consumer/consumer.go +++ b/backend/cmd/consumer/consumer.go @@ -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") @@ -32,7 +31,7 @@ func main() { panic(err) } - databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename) + databaseConfig, err := config.LoadDatabaseConfig() if err != nil { panic(err) } diff --git a/backend/config/database.go b/backend/config/database.go index 5c444eee..92d90e57 100644 --- a/backend/config/database.go +++ b/backend/config/database.go @@ -1,8 +1,9 @@ package config import ( - "encoding/json" "os" + "strconv" + "fmt" ) type RedisConfig struct { @@ -30,25 +31,35 @@ var DefaultDatabaseConfig = DatabaseConfig{ Postgres: PostgresConfig{ Host: "localhost", Port: 5432, - User: "art-peace-user", - Database: "art-peace-db", + User: "afk-user", + Database: "afk-db", }, } var DefaultDatabaseConfigPath = "./configs/database.config.json" -func LoadDatabaseConfig(databaseConfigPath string) (*DatabaseConfig, error) { - file, err := os.Open(databaseConfigPath) +func LoadDatabaseConfig() (*DatabaseConfig, error) { + redisPort, err := strconv.Atoi(os.Getenv("REDIS_PORT")) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid REDIS_PORT: %v", err) } - defer file.Close() - decoder := json.NewDecoder(file) - config := DatabaseConfig{} - err = decoder.Decode(&config) + postgresPort, err := strconv.Atoi(os.Getenv("POSTGRES_PORT")) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid POSTGRES_PORT: %v", err) + } + + config := DatabaseConfig{ + Redis: RedisConfig{ + Host: os.Getenv("REDIS_HOST"), + Port: redisPort, + }, + Postgres: PostgresConfig{ + Host: os.Getenv("POSTGRES_HOST"), + Port: postgresPort, + User: os.Getenv("POSTGRES_USER"), + Database: os.Getenv("POSTGRES_DATABASE"), + }, } return &config, nil From 825f723dff8c1d66b6a6030c585ebba2e80ff080 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 12:14:28 +0100 Subject: [PATCH 05/15] refac: replace all ArtPeaceBackend with AFKBackend --- backend/cmd/backend/backend.go | 4 ++-- backend/cmd/consumer/consumer.go | 4 ++-- backend/cmd/video-gen/video.go | 4 ++-- backend/config/database.go | 2 -- backend/core/backend.go | 2 +- backend/core/databases.go | 4 ++-- backend/routes/canvas.go | 8 +++---- backend/routes/colors.go | 2 +- backend/routes/factions.go | 10 ++++----- backend/routes/indexer/color.go | 4 ++-- backend/routes/indexer/day.go | 4 ++-- backend/routes/indexer/faction.go | 22 +++++++++--------- backend/routes/indexer/nft.go | 28 +++++++++++------------ backend/routes/indexer/nftTransfer.go | 4 ++-- backend/routes/indexer/pixel.go | 30 ++++++++++++------------- backend/routes/indexer/quest.go | 8 +++---- backend/routes/indexer/template.go | 20 ++++++++--------- backend/routes/indexer/username.go | 6 ++--- backend/routes/indexer/votableColors.go | 4 ++-- backend/routes/indexer/vote.go | 4 ++-- backend/routes/nft.go | 12 +++++----- backend/routes/pixel.go | 26 ++++++++++----------- backend/routes/quests.go | 20 ++++++++--------- backend/routes/templates.go | 12 +++++----- backend/routes/user.go | 6 ++--- backend/routes/utils/middleware.go | 4 ++-- backend/routes/utils/responses.go | 14 ++++++------ backend/routes/votableColors.go | 8 +++---- backend/routes/websocket.go | 10 ++++----- backend/video/video.go | 10 ++++----- 30 files changed, 147 insertions(+), 149 deletions(-) diff --git a/backend/cmd/backend/backend.go b/backend/cmd/backend/backend.go index 5d567ec1..02cb6457 100644 --- a/backend/cmd/backend/backend.go +++ b/backend/cmd/backend/backend.go @@ -49,9 +49,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) } diff --git a/backend/cmd/consumer/consumer.go b/backend/cmd/consumer/consumer.go index c65db8fd..5c8f2f32 100644 --- a/backend/cmd/consumer/consumer.go +++ b/backend/cmd/consumer/consumer.go @@ -48,7 +48,7 @@ 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() @@ -56,5 +56,5 @@ func main() { routes.InitNFTStaticRoutes() indexer.StartMessageProcessor() - core.ArtPeaceBackend.Start(core.ArtPeaceBackend.BackendConfig.ConsumerPort) + core.AFKBackend.Start(core.AFKBackend.BackendConfig.ConsumerPort) } diff --git a/backend/cmd/video-gen/video.go b/backend/cmd/video-gen/video.go index 9ee5b24b..3bc132ab 100644 --- a/backend/cmd/video-gen/video.go +++ b/backend/cmd/video-gen/video.go @@ -34,12 +34,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) } diff --git a/backend/config/database.go b/backend/config/database.go index 92d90e57..8e08889f 100644 --- a/backend/config/database.go +++ b/backend/config/database.go @@ -36,8 +36,6 @@ var DefaultDatabaseConfig = DatabaseConfig{ }, } -var DefaultDatabaseConfigPath = "./configs/database.config.json" - func LoadDatabaseConfig() (*DatabaseConfig, error) { redisPort, err := strconv.Atoi(os.Getenv("REDIS_PORT")) if err != nil { diff --git a/backend/core/backend.go b/backend/core/backend.go index 77c28f35..afe1fc3b 100644 --- a/backend/core/backend.go +++ b/backend/core/backend.go @@ -21,7 +21,7 @@ type Backend struct { AdminMode bool } -var ArtPeaceBackend *Backend +var AFKBackend *Backend func NewBackend(databases *Databases, canvasConfig *config.CanvasConfig, backendConfig *config.BackendConfig, adminMode bool) *Backend { return &Backend{ diff --git a/backend/core/databases.go b/backend/core/databases.go index af8d4ee3..3b9ec6e7 100644 --- a/backend/core/databases.go +++ b/backend/core/databases.go @@ -50,7 +50,7 @@ func (d *Databases) Close() { func PostgresQuery[RowType any](query string, args ...interface{}) ([]RowType, error) { var result []RowType - err := pgxscan.Select(context.Background(), ArtPeaceBackend.Databases.Postgres, &result, query, args...) + err := pgxscan.Select(context.Background(), AFKBackend.Databases.Postgres, &result, query, args...) if err != nil { return nil, err } @@ -60,7 +60,7 @@ func PostgresQuery[RowType any](query string, args ...interface{}) ([]RowType, e func PostgresQueryOne[RowType any](query string, args ...interface{}) (*RowType, error) { var result RowType - err := pgxscan.Get(context.Background(), ArtPeaceBackend.Databases.Postgres, &result, query, args...) + err := pgxscan.Get(context.Background(), AFKBackend.Databases.Postgres, &result, query, args...) if err != nil { return nil, err } diff --git a/backend/routes/canvas.go b/backend/routes/canvas.go index bc908046..c6957ad5 100644 --- a/backend/routes/canvas.go +++ b/backend/routes/canvas.go @@ -19,8 +19,8 @@ func initCanvas(w http.ResponseWriter, r *http.Request) { return } - if core.ArtPeaceBackend.Databases.Redis.Exists(context.Background(), "canvas").Val() == 0 { - totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth + if core.AFKBackend.Databases.Redis.Exists(context.Background(), "canvas").Val() == 0 { + totalBitSize := core.AFKBackend.CanvasConfig.Canvas.Width * core.AFKBackend.CanvasConfig.Canvas.Height * core.AFKBackend.CanvasConfig.ColorsBitWidth totalByteSize := (totalBitSize / 8) if totalBitSize%8 != 0 { // Round up to nearest byte @@ -30,7 +30,7 @@ func initCanvas(w http.ResponseWriter, r *http.Request) { // Create canvas canvas := make([]byte, totalByteSize) ctx := context.Background() - err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err() + err := core.AFKBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err() if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to initialize canvas") return @@ -46,7 +46,7 @@ func getCanvas(w http.ResponseWriter, r *http.Request) { routeutils.SetupAccessHeaders(w) ctx := context.Background() - val, err := core.ArtPeaceBackend.Databases.Redis.Get(ctx, "canvas").Result() + val, err := core.AFKBackend.Databases.Redis.Get(ctx, "canvas").Result() if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to get canvas") return diff --git a/backend/routes/colors.go b/backend/routes/colors.go index a94f7b90..b646a9ef 100644 --- a/backend/routes/colors.go +++ b/backend/routes/colors.go @@ -30,7 +30,7 @@ func InitColors(w http.ResponseWriter, r *http.Request) { } for _, color := range *colors { - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO colors (hex) VALUES ($1)", color) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO colors (hex) VALUES ($1)", color) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert color: "+color) return diff --git a/backend/routes/factions.go b/backend/routes/factions.go index cd331116..03f4e3f8 100644 --- a/backend/routes/factions.go +++ b/backend/routes/factions.go @@ -24,7 +24,7 @@ func InitFactionRoutes() { http.HandleFunc("/get-faction-members", getFactionMembers) // Create a static file server for the nft images http.Handle("/faction-images/", http.StripPrefix("/faction-images/", http.FileServer(http.Dir("./factions")))) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/join-chain-faction-devnet", joinChainFactionDevnet) http.HandleFunc("/join-faction-devnet", joinFactionDevnet) http.HandleFunc("/leave-faction-devnet", leaveFactionDevnet) @@ -102,7 +102,7 @@ func initFactions(w http.ResponseWriter, r *http.Request) { // Insert factions info into the database for _, faction := range factionJson.Factions { - _, err := core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionLinks (faction_id, icon, telegram, twitter, github, site) VALUES ($1, $2, $3, $4, $5, $6)", faction.Id, faction.Icon, faction.Links.Telegram, faction.Links.Twitter, faction.Links.Github, faction.Links.Site) + _, err := core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionLinks (faction_id, icon, telegram, twitter, github, site) VALUES ($1, $2, $3, $4, $5, $6)", faction.Id, faction.Icon, faction.Links.Telegram, faction.Links.Twitter, faction.Links.Github, faction.Links.Site) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert factions") return @@ -376,7 +376,7 @@ func joinChainFactionDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.JoinChainFactionDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.JoinChainFactionDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "join_chain_faction", chainId) @@ -407,7 +407,7 @@ func joinFactionDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.JoinFactionDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.JoinFactionDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "join_faction", factionId) @@ -426,7 +426,7 @@ func leaveFactionDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.LeaveFactionDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.LeaveFactionDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "leave_faction") diff --git a/backend/routes/indexer/color.go b/backend/routes/indexer/color.go index 960ab535..48139323 100644 --- a/backend/routes/indexer/color.go +++ b/backend/routes/indexer/color.go @@ -20,7 +20,7 @@ func processColorAddedEvent(event IndexerEvent) { color := colorHex[len(colorHex)-6:] // Set color in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Colors (color_key, hex) VALUES ($1, $2)", colorKey, color) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Colors (color_key, hex) VALUES ($1, $2)", colorKey, color) if err != nil { PrintIndexerError("processColorAddedEvent", "Error inserting color into postgres", colorKey, color) return @@ -37,7 +37,7 @@ func revertColorAddedEvent(event IndexerEvent) { } // Delete color from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM colors WHERE color_key = $1", colorIdx) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM colors WHERE color_key = $1", colorIdx) if err != nil { PrintIndexerError("revertColorAddedEvent", "Error deleting color from postgres", colorIdx) return diff --git a/backend/routes/indexer/day.go b/backend/routes/indexer/day.go index 79b5ebbe..f729bb53 100644 --- a/backend/routes/indexer/day.go +++ b/backend/routes/indexer/day.go @@ -24,7 +24,7 @@ func processNewDayEvent(event IndexerEvent) { } // Set day in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Days (day_index, day_start) VALUES ($1, to_timestamp($2))", dayIdx, dayStartTime) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Days (day_index, day_start) VALUES ($1, to_timestamp($2))", dayIdx, dayStartTime) if err != nil { PrintIndexerError("processNewDayEvent", "Error inserting day into postgres", dayIdx, dayStartTime) return @@ -41,7 +41,7 @@ func revertNewDayEvent(event IndexerEvent) { } // Delete day from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Days WHERE day_index = $1", dayIdx) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Days WHERE day_index = $1", dayIdx) if err != nil { PrintIndexerError("revertNewDayEvent", "Error deleting day from postgres", dayIdx) return diff --git a/backend/routes/indexer/faction.go b/backend/routes/indexer/faction.go index 21c82f4e..95e3b804 100644 --- a/backend/routes/indexer/faction.go +++ b/backend/routes/indexer/faction.go @@ -52,7 +52,7 @@ func processFactionCreatedEvent(event IndexerEvent) { } // Add faction info into postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Factions (faction_id, name, leader, joinable, allocation) VALUES ($1, $2, $3, $4, $5)", factionId, name, leader, joinable, allocation) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Factions (faction_id, name, leader, joinable, allocation) VALUES ($1, $2, $3, $4, $5)", factionId, name, leader, joinable, allocation) if err != nil { PrintIndexerError("processFactionCreatedEvent", "Failed to insert faction into postgres", factionIdHex, nameHex, leader, joinableHex, allocationHex) return @@ -68,7 +68,7 @@ func revertFactionCreatedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Factions WHERE id = $1", factionId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Factions WHERE id = $1", factionId) if err != nil { PrintIndexerError("revertFactionCreatedEvent", "Failed to delete faction from postgres", factionIdHex) return @@ -85,7 +85,7 @@ func processFactionLeaderChangedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE Factions SET leader = $1 WHERE faction_id = $2", newLeader, factionId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE Factions SET leader = $1 WHERE faction_id = $2", newLeader, factionId) if err != nil { PrintIndexerError("processFactionLeaderChangedEvent", "Failed to update faction leader in postgres", factionIdHex, newLeader) return @@ -106,7 +106,7 @@ func processFactionJoinedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) if err != nil { PrintIndexerError("processFactionJoinedEvent", "Failed to insert faction member into postgres", factionIdHex, userAddress) return @@ -123,7 +123,7 @@ func revertFactionJoinedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) if err != nil { PrintIndexerError("revertFactionJoinedEvent", "Failed to delete faction member from postgres", factionIdHex, userAddress) return @@ -140,7 +140,7 @@ func processFactionLeftEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) if err != nil { PrintIndexerError("processFactionLeftEvent", "Failed to delete faction member from postgres", factionIdHex, userAddress) return @@ -158,7 +158,7 @@ func revertFactionLeftEvent(event IndexerEvent) { } // TODO: Stash the last_placed_time and member_pixels in the event data - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) if err != nil { PrintIndexerError("revertFactionLeftEvent", "Failed to insert faction member into postgres", factionIdHex, userAddress) return @@ -193,7 +193,7 @@ func processChainFactionCreatedEvent(event IndexerEvent) { name := string(trimmedName) // Add faction info into postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactions (faction_id, name) VALUES ($1, $2)", factionId, name) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactions (faction_id, name) VALUES ($1, $2)", factionId, name) if err != nil { PrintIndexerError("processChainFactionCreatedEvent", "Failed to insert faction into postgres", factionIdHex, nameHex) return @@ -209,7 +209,7 @@ func revertChainFactionCreatedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactions WHERE faction_id = $1", factionId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactions WHERE faction_id = $1", factionId) if err != nil { PrintIndexerError("revertChainFactionCreatedEvent", "Failed to delete faction from postgres", factionIdHex) return @@ -226,7 +226,7 @@ func processChainFactionJoinedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactionMembersInfo (faction_id, user_address, last_placed_time, member_pixels) VALUES ($1, $2, TO_TIMESTAMP($3), $4)", factionId, userAddress, 0, 0) if err != nil { PrintIndexerError("processChainFactionJoinedEvent", "Failed to insert faction member into postgres", factionIdHex, userAddress) return @@ -243,7 +243,7 @@ func revertChainFactionJoinedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactionMembersInfo WHERE faction_id = $1 AND user_address = $2", factionId, userAddress) if err != nil { PrintIndexerError("revertChainFactionJoinedEvent", "Failed to delete faction member from postgres", factionIdHex, userAddress) return diff --git a/backend/routes/indexer/nft.go b/backend/routes/indexer/nft.go index 078f49ac..eb3f3594 100644 --- a/backend/routes/indexer/nft.go +++ b/backend/routes/indexer/nft.go @@ -82,7 +82,7 @@ func processNFTMintedEvent(event IndexerEvent) { } // Set NFT in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTs (token_id, position, width, height, name, image_hash, block_number, day_index, minter, owner) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", tokenId, position, width, height, name, imageHashHex, blockNumber, dayIndex, minter, minter) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTs (token_id, position, width, height, name, image_hash, block_number, day_index, minter, owner) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", tokenId, position, width, height, name, imageHashHex, blockNumber, dayIndex, minter, minter) if err != nil { PrintIndexerError("processNFTMintedEvent", "Error inserting NFT into postgres", tokenIdLowHex, tokenIdHighHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter) return @@ -90,7 +90,7 @@ func processNFTMintedEvent(event IndexerEvent) { // Load image from redis ctx := context.Background() - canvas, err := core.ArtPeaceBackend.Databases.Redis.Get(ctx, "canvas").Result() + canvas, err := core.AFKBackend.Databases.Redis.Get(ctx, "canvas").Result() if err != nil { PrintIndexerError("processNFTMintedEvent", "Error getting canvas from redis", tokenIdLowHex, tokenIdHighHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter) return @@ -121,15 +121,15 @@ func processNFTMintedEvent(event IndexerEvent) { } colorPalette[idx] = color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: 255} } - bitWidth := int64(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth) - startX := int64(position % int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width)) - startY := int64(position / int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width)) + bitWidth := int64(core.AFKBackend.CanvasConfig.ColorsBitWidth) + startX := int64(position % int64(core.AFKBackend.CanvasConfig.Canvas.Width)) + startY := int64(position / int64(core.AFKBackend.CanvasConfig.Canvas.Width)) oneByteBitOffset := int64(8 - bitWidth) twoByteBitOffset := int64(16 - bitWidth) generatedImage := image.NewRGBA(image.Rect(0, 0, int(width), int(height))) for y := startY; y < startY+height; y++ { for x := startX; x < startX+width; x++ { - pos := y*int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width) + x + pos := y*int64(core.AFKBackend.CanvasConfig.Canvas.Width) + x bitPos := pos * bitWidth bytePos := bitPos / 8 bitOffset := bitPos % 8 @@ -184,12 +184,12 @@ func processNFTMintedEvent(event IndexerEvent) { // Create a NFT JSON metadata file - x := position % int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width) - y := position / int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width) + x := position % int64(core.AFKBackend.CanvasConfig.Canvas.Width) + y := position / int64(core.AFKBackend.CanvasConfig.Canvas.Width) metadata := map[string]interface{}{ "name": name, "description": "User minted art/peace NFT from the canvas.", - "image": fmt.Sprintf("%s/nft-images/nft-%d.png", core.ArtPeaceBackend.GetBackendUrl(), tokenId), + "image": fmt.Sprintf("%s/nft-images/nft-%d.png", core.AFKBackend.GetBackendUrl(), tokenId), "attributes": []map[string]interface{}{ { "trait_type": "Width", @@ -252,7 +252,7 @@ func revertNFTMintedEvent(event IndexerEvent) { } tokenId := tokenIdU256.Uint64() - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTs WHERE token_id = $1", tokenId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTs WHERE token_id = $1", tokenId) if err != nil { PrintIndexerError("reverseNFTMintedEvent", "Error deleting NFT from postgres", tokenIdLowHex) return @@ -273,7 +273,7 @@ func processNFTLikedEvent(event IndexerEvent) { } tokenId := tokenIdU256.Uint64() - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2) ON CONFLICT DO NOTHING", tokenId, liker) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2) ON CONFLICT DO NOTHING", tokenId, liker) if err != nil { PrintIndexerError("processNFTLikedEvent", "Error inserting NFT like into postgres", tokenIdLowHex, liker) return @@ -295,7 +295,7 @@ func revertNFTLikedEvent(event IndexerEvent) { tokenId := tokenIdU256.Uint64() // TODO: Check if like exists before event - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTLikes WHERE nftKey = $1 AND liker = $2", tokenId, liker) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTLikes WHERE nftKey = $1 AND liker = $2", tokenId, liker) if err != nil { PrintIndexerError("revertNFTLikedEvent", "Error deleting NFT like from postgres", tokenIdLowHex, liker) return @@ -314,7 +314,7 @@ func processNFTUnlikedEvent(event IndexerEvent) { } tokenId := tokenIdU256.Uint64() - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTLikes WHERE nftKey = $1 AND liker = $2", tokenId, unliker) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM NFTLikes WHERE nftKey = $1 AND liker = $2", tokenId, unliker) if err != nil { PrintIndexerError("processNFTUnlikedEvent", "Error deleting NFT like from postgres", tokenIdLowHex, unliker) return @@ -335,7 +335,7 @@ func revertNFTUnlikedEvent(event IndexerEvent) { } tokenId := tokenIdU256.Uint64() - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2) ON CONFLICT DO NOTHING", tokenId, unliker) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2) ON CONFLICT DO NOTHING", tokenId, unliker) if err != nil { PrintIndexerError("revertNFTUnlikedEvent", "Error inserting NFT like into postgres", tokenIdLowHex, unliker) return diff --git a/backend/routes/indexer/nftTransfer.go b/backend/routes/indexer/nftTransfer.go index 25145653..975624a3 100644 --- a/backend/routes/indexer/nftTransfer.go +++ b/backend/routes/indexer/nftTransfer.go @@ -20,7 +20,7 @@ func processNFTTransferEvent(event IndexerEvent) { tokenId := tokenIdU256.Uint64() // Set owner - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE NFTs SET owner = $1 WHERE token_id = $2", to, tokenId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE NFTs SET owner = $1 WHERE token_id = $2", to, tokenId) if err != nil { PrintIndexerError("processNFTTransferEvent", "Error updating owner in postgres", to, tokenIdLowHex, tokenIdHighHex) return @@ -41,7 +41,7 @@ func revertNFTTransferEvent(event IndexerEvent) { tokenId := tokenIdU256.Uint64() // Set owner - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE NFTs SET owner = $1 WHERE token_id = $2", from, tokenId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE NFTs SET owner = $1 WHERE token_id = $2", from, tokenId) if err != nil { PrintIndexerError("revertNFTTransferEvent", "Error updating owner in postgres", from, tokenIdLowHex, tokenIdHighHex) return diff --git a/backend/routes/indexer/pixel.go b/backend/routes/indexer/pixel.go index 4915d7cc..424ebf62 100644 --- a/backend/routes/indexer/pixel.go +++ b/backend/routes/indexer/pixel.go @@ -22,7 +22,7 @@ func processPixelPlacedEvent(event IndexerEvent) { } //validate position - maxPosition := int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Width) * int64(core.ArtPeaceBackend.CanvasConfig.Canvas.Height) + maxPosition := int64(core.AFKBackend.CanvasConfig.Canvas.Width) * int64(core.AFKBackend.CanvasConfig.Canvas.Height) // Perform comparison with maxPosition if position < 0 || position >= maxPosition { @@ -42,18 +42,18 @@ func processPixelPlacedEvent(event IndexerEvent) { } // Set pixel in redis - bitfieldType := "u" + strconv.Itoa(int(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth)) - pos := uint(position) * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth + bitfieldType := "u" + strconv.Itoa(int(core.AFKBackend.CanvasConfig.ColorsBitWidth)) + pos := uint(position) * core.AFKBackend.CanvasConfig.ColorsBitWidth ctx := context.Background() - err = core.ArtPeaceBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, color).Err() + err = core.AFKBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, color).Err() if err != nil { PrintIndexerError("processPixelPlacedEvent", "Error setting pixel in redis", address, posHex, dayIdxHex, colorHex) return } // Set pixel in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Pixels (address, position, day, color) VALUES ($1, $2, $3, $4)", address, position, dayIdx, color) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Pixels (address, position, day, color) VALUES ($1, $2, $3, $4)", address, position, dayIdx, color) if err != nil { // TODO: Reverse redis operation? PrintIndexerError("processPixelPlacedEvent", "Error inserting pixel into postgres", address, posHex, dayIdxHex, colorHex) @@ -81,7 +81,7 @@ func revertPixelPlacedEvent(event IndexerEvent) { } // Delete pixel from postgres ( last one ) - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Pixels WHERE address = $1 AND position = $2 ORDER BY time limit 1", address, position) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Pixels WHERE address = $1 AND position = $2 ORDER BY time limit 1", address, position) if err != nil { PrintIndexerError("revertPixelPlacedEvent", "Error deleting pixel from postgres", address, posHex) return @@ -94,11 +94,11 @@ func revertPixelPlacedEvent(event IndexerEvent) { return } // Reset pixel in redis - bitfieldType := "u" + strconv.Itoa(int(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth)) - pos := uint(position) * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth + bitfieldType := "u" + strconv.Itoa(int(core.AFKBackend.CanvasConfig.ColorsBitWidth)) + pos := uint(position) * core.AFKBackend.CanvasConfig.ColorsBitWidth ctx := context.Background() - err = core.ArtPeaceBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, oldColor).Err() + err = core.AFKBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, oldColor).Err() if err != nil { PrintIndexerError("revertPixelPlacedEvent", "Error resetting pixel in redis", address, posHex) return @@ -123,7 +123,7 @@ func processBasicPixelPlacedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO LastPlacedTime (address, time) VALUES ($1, TO_TIMESTAMP($2)) ON CONFLICT (address) DO UPDATE SET time = TO_TIMESTAMP($2)", address, timestamp) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO LastPlacedTime (address, time) VALUES ($1, TO_TIMESTAMP($2)) ON CONFLICT (address) DO UPDATE SET time = TO_TIMESTAMP($2)", address, timestamp) if err != nil { PrintIndexerError("processBasicPixelPlacedEvent", "Error inserting last placed time into postgres", address, timestampHex) return @@ -134,7 +134,7 @@ func revertBasicPixelPlacedEvent(event IndexerEvent) { address := event.Event.Keys[1][2:] // Remove 0x prefix // Reset last placed time to time of last pixel placed - _, err := core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE LastPlacedTime SET time = (SELECT time FROM Pixels WHERE address = $1 ORDER BY time DESC LIMIT 1) WHERE address = $1", address) + _, err := core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE LastPlacedTime SET time = (SELECT time FROM Pixels WHERE address = $1 ORDER BY time DESC LIMIT 1) WHERE address = $1", address) if err != nil { PrintIndexerError("revertBasicPixelPlacedEvent", "Error resetting last placed time in postgres", address) return @@ -161,7 +161,7 @@ func processFactionPixelsPlacedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionMembersInfo SET last_placed_time = TO_TIMESTAMP($1), member_pixels = $2 WHERE user_address = $3", timestamp, memberPixels, userAddress) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionMembersInfo SET last_placed_time = TO_TIMESTAMP($1), member_pixels = $2 WHERE user_address = $3", timestamp, memberPixels, userAddress) if err != nil { PrintIndexerError("processMemberPixelsPlacedEvent", "Error updating faction member info in postgres", userAddress, timestampHex, memberPixelsHex) return @@ -190,7 +190,7 @@ func processChainFactionPixelsPlacedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionMembersInfo SET last_placed_time = TO_TIMESTAMP($1), member_pixels = $2 WHERE user_address = $3", timestamp, memberPixels, userAddress) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionMembersInfo SET last_placed_time = TO_TIMESTAMP($1), member_pixels = $2 WHERE user_address = $3", timestamp, memberPixels, userAddress) if err != nil { PrintIndexerError("processChainFactionMemberPixelsPlacedEvent", "Error updating chain faction member info in postgres", userAddress, timestampHex, memberPixelsHex) return @@ -211,7 +211,7 @@ func processExtraPixelsPlacedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ExtraPixels SET available = available - $1, used = used + $1 WHERE address = $2", extraPixels, address) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ExtraPixels SET available = available - $1, used = used + $1 WHERE address = $2", extraPixels, address) if err != nil { PrintIndexerError("processExtraPixelsPlacedEvent", "Error updating extra pixels in postgres", address, extraPixelsHex) return @@ -228,7 +228,7 @@ func revertExtraPixelsPlacedEvent(event IndexerEvent) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ExtraPixels SET available = available + $1, used = used - $1 WHERE address = $2", extraPixels, address) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ExtraPixels SET available = available + $1, used = used - $1 WHERE address = $2", extraPixels, address) if err != nil { PrintIndexerError("revertExtraPixelsPlacedEvent", "Error updating extra pixels in postgres", address, extraPixelsHex) return diff --git a/backend/routes/indexer/quest.go b/backend/routes/indexer/quest.go index d53d392f..13ae635a 100644 --- a/backend/routes/indexer/quest.go +++ b/backend/routes/indexer/quest.go @@ -52,14 +52,14 @@ func processDailyQuestClaimedEvent(event IndexerEvent) { // TODO: Add calldata field & completed_at field // Add daily quest info into postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO UserDailyQuests (user_address, day_index, quest_id, completed) VALUES ($1, $2, $3, $4)", user, dayIndex, questId, true) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO UserDailyQuests (user_address, day_index, quest_id, completed) VALUES ($1, $2, $3, $4)", user, dayIndex, questId, true) if err != nil { PrintIndexerError("processDailyQuestClaimedEvent", "Failed to insert daily quest into postgres", dayIndexHex, questIdHex, user, rewardHex, calldataLenHex, calldata) return } // Update user's extra pixels - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ExtraPixels (address, available, used) VALUES ($1, $2, 0) ON CONFLICT (address) DO UPDATE SET available = ExtraPixels.available + $2", user, reward) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ExtraPixels (address, available, used) VALUES ($1, $2, 0) ON CONFLICT (address) DO UPDATE SET available = ExtraPixels.available + $2", user, reward) if err != nil { PrintIndexerError("processDailyQuestClaimedEvent", "Failed to update user's extra pixels", dayIndexHex, questIdHex, user, rewardHex, calldataLenHex, calldata) return @@ -100,14 +100,14 @@ func processMainQuestClaimedEvent(event IndexerEvent) { } // Add main quest info into postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO UserMainQuests (user_address, quest_id, completed) VALUES ($1, $2, $3)", user, questId, true) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO UserMainQuests (user_address, quest_id, completed) VALUES ($1, $2, $3)", user, questId, true) if err != nil { PrintIndexerError("processMainQuestClaimedEvent", "Failed to insert main quest into postgres", questIdHex, user, rewardHex, calldataLenHex, calldata) return } // Update user's extra pixels - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ExtraPixels (address, available, used) VALUES ($1, $2, 0) ON CONFLICT (address) DO UPDATE SET available = ExtraPixels.available + $2", user, reward) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ExtraPixels (address, available, used) VALUES ($1, $2, 0) ON CONFLICT (address) DO UPDATE SET available = ExtraPixels.available + $2", user, reward) if err != nil { PrintIndexerError("processMainQuestClaimedEvent", "Failed to update user's extra pixels", questIdHex, user, rewardHex, calldataLenHex, calldata) return diff --git a/backend/routes/indexer/template.go b/backend/routes/indexer/template.go index 4849422a..a9e0aadd 100644 --- a/backend/routes/indexer/template.go +++ b/backend/routes/indexer/template.go @@ -70,7 +70,7 @@ func processTemplateAddedEvent(event IndexerEvent) { } // Add template to postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Templates (key, name, hash, position, width, height, reward, reward_token) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", templateId, templateName, templateHashHex, templatePosition, templateWidth, templateHeight, templateReward, templateRewardToken) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Templates (key, name, hash, position, width, height, reward, reward_token) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", templateId, templateName, templateHashHex, templatePosition, templateWidth, templateHeight, templateReward, templateRewardToken) if err != nil { PrintIndexerError("processTemplateAddedEvent", "Error inserting template into postgres", templateIdHex, templateHashHex, templateNameHex, templatePositionHex, templateWidthHex, templateHeightHex, templateRewardLowHex, templateRewardToken) return @@ -89,7 +89,7 @@ func revertTemplateAddedEvent(event IndexerEvent) { } // Remove template from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Templates WHERE key = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Templates WHERE key = $1", templateId) if err != nil { PrintIndexerError("reverseTemplateAddedEvent", "Error deleting template from postgres", templateIdHex) return @@ -137,7 +137,7 @@ func processFactionTemplateAddedEvent(event IndexerEvent) { } // Add faction template to postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionTemplates (template_id, faction_id, hash, position, width, height, stale) VALUES ($1, $2, $3, $4, $5, $6, $7)", templateId, factionId, imageHashLowercase, factionTemplatePosition, factionTemplateWidth, factionTemplateHeight, false) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO FactionTemplates (template_id, faction_id, hash, position, width, height, stale) VALUES ($1, $2, $3, $4, $5, $6, $7)", templateId, factionId, imageHashLowercase, factionTemplatePosition, factionTemplateWidth, factionTemplateHeight, false) if err != nil { PrintIndexerError("processFactionTemplateAddedEvent", "Error inserting faction template into postgres", templateIdHex, factionIdHex, factionTemplateHashHex, factionTemplatePositionHex, factionTemplateWidthHex, factionTemplateHeightHex) return @@ -154,7 +154,7 @@ func revertFactionTemplateAddedEvent(event IndexerEvent) { } // Remove faction template from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionTemplates WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM FactionTemplates WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("reverseFactionTemplateAddedEvent", "Error deleting faction template from postgres", templateIdHex) return @@ -171,7 +171,7 @@ func processFactionTemplateRemovedEvent(event IndexerEvent) { } // Mark faction template as stale in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionTemplates SET stale = true WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionTemplates SET stale = true WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("processFactionTemplateRemovedEvent", "Error marking faction template as stale in postgres", templateIdHex) return @@ -188,7 +188,7 @@ func revertFactionTemplateRemovedEvent(event IndexerEvent) { } // Unmark faction template as stale in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionTemplates SET stale = false WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE FactionTemplates SET stale = false WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("reverseFactionTemplateRemovedEvent", "Error unmarking faction template as stale in postgres", templateIdHex) return @@ -236,7 +236,7 @@ func processChainFactionTemplateAddedEvent(event IndexerEvent) { } // Add chain template to postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactionTemplates (template_id, faction_id, hash, position, width, height, stale) VALUES ($1, $2, $3, $4, $5, $6, $7)", templateId, chainId, imageHashLowercase, chainTemplatePosition, chainTemplateWidth, chainTemplateHeight, false) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ChainFactionTemplates (template_id, faction_id, hash, position, width, height, stale) VALUES ($1, $2, $3, $4, $5, $6, $7)", templateId, chainId, imageHashLowercase, chainTemplatePosition, chainTemplateWidth, chainTemplateHeight, false) if err != nil { PrintIndexerError("processChainTemplateAddedEvent", "Error inserting chain template into postgres", templateIdHex, chainIdHex, chainTemplateHashHex, chainTemplatePositionHex, chainTemplateWidthHex, chainTemplateHeightHex) return @@ -253,7 +253,7 @@ func revertChainFactionTemplateAddedEvent(event IndexerEvent) { } // Remove chain template from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactionTemplates WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ChainFactionTemplates WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("reverseChainTemplateAddedEvent", "Error deleting chain template from postgres", templateIdHex) return @@ -270,7 +270,7 @@ func processChainFactionTemplateRemovedEvent(event IndexerEvent) { } // Mark chain template as stale in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionTemplates SET stale = true WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionTemplates SET stale = true WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("processChainTemplateRemovedEvent", "Error marking chain template as stale in postgres", templateIdHex) return @@ -287,7 +287,7 @@ func revertChainFactionTemplateRemovedEvent(event IndexerEvent) { } // Unmark chain template as stale in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionTemplates SET stale = false WHERE template_id = $1", templateId) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE ChainFactionTemplates SET stale = false WHERE template_id = $1", templateId) if err != nil { PrintIndexerError("reverseChainTemplateRemovedEvent", "Error unmarking chain template as stale in postgres", templateIdHex) return diff --git a/backend/routes/indexer/username.go b/backend/routes/indexer/username.go index 3ad62f31..445b0686 100644 --- a/backend/routes/indexer/username.go +++ b/backend/routes/indexer/username.go @@ -30,7 +30,7 @@ func processUsernameClaimedEvent(event IndexerEvent) { username := string(trimmedUsername) // Set username in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Users (address, name) VALUES ($1, $2)", address, username) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO Users (address, name) VALUES ($1, $2)", address, username) if err != nil { PrintIndexerError("processUsernameClaimedEvent", "Error inserting username into postgres", address, username) return @@ -41,7 +41,7 @@ func revertUsernameClaimedEvent(event IndexerEvent) { address := event.Event.Keys[1][2:] // Remove 0x prefix // Remove username from postgres - _, err := core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Users WHERE address = $1", address) + _, err := core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM Users WHERE address = $1", address) if err != nil { PrintIndexerError("revertUsernameClaimedEvent", "Error deleting username from postgres", address, "") return @@ -71,7 +71,7 @@ func processUsernameChangedEvent(event IndexerEvent) { username := string(trimmedUsername) // Set username in postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "UPDATE Users SET name = $1 WHERE address = $2", username, address) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "UPDATE Users SET name = $1 WHERE address = $2", username, address) if err != nil { PrintIndexerError("processUsernameChangedEvent", "Error updating username in postgres", address, username) return diff --git a/backend/routes/indexer/votableColors.go b/backend/routes/indexer/votableColors.go index d0a4deda..299cef3b 100644 --- a/backend/routes/indexer/votableColors.go +++ b/backend/routes/indexer/votableColors.go @@ -27,7 +27,7 @@ func processVotableColorAddedEvent(event IndexerEvent) { color := colorHex[len(colorHex)-6:] // Set votable color in postgres ( or update if already exists ) - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO VotableColors (day_index, color_key, hex) VALUES ($1, $2, $3)", dayIdx, colorKey, color) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO VotableColors (day_index, color_key, hex) VALUES ($1, $2, $3)", dayIdx, colorKey, color) if err != nil { PrintIndexerError("processVotableColorAddedEvent", "Error inserting color vote into postgres", dayIdxHex, colorHex) return @@ -51,7 +51,7 @@ func revertVotableColorAddedEvent(event IndexerEvent) { } // Remove vote from postgres - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM VotableColors WHERE day_index = $1 AND color_key = $2", dayIdx, colorKey) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM VotableColors WHERE day_index = $1 AND color_key = $2", dayIdx, colorKey) if err != nil { PrintIndexerError("revertVotableColorAddedEvent", "Error deleting votable color from postgres", dayIdxHex, colorKeyHex) return diff --git a/backend/routes/indexer/vote.go b/backend/routes/indexer/vote.go index b865eeca..edb313c7 100644 --- a/backend/routes/indexer/vote.go +++ b/backend/routes/indexer/vote.go @@ -25,7 +25,7 @@ func processVoteColorEvent(event IndexerEvent) { } // Set vote in postgres ( or update if already exists ) - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ColorVotes (user_address, day_index, color_key) VALUES ($1, $2, $3) ON CONFLICT (user_address, day_index) DO UPDATE SET color_key = $3", voter, dayIdx, color) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO ColorVotes (user_address, day_index, color_key) VALUES ($1, $2, $3) ON CONFLICT (user_address, day_index) DO UPDATE SET color_key = $3", voter, dayIdx, color) if err != nil { PrintIndexerError("processVoteColorEvent", "Error inserting color vote into postgres", voter, dayIdxHex, colorHex) return @@ -44,7 +44,7 @@ func revertVoteColorEvent(event IndexerEvent) { // Remove vote from postgres // TODO: Revert to old vote if it existed before the vote being reverted - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ColorVotes WHERE user_address = $1 AND day_index = $2", voter, dayIdx) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM ColorVotes WHERE user_address = $1 AND day_index = $2", voter, dayIdx) if err != nil { PrintIndexerError("revertVoteColorEvent", "Error deleting color vote from postgres", voter, dayIdxHex) return diff --git a/backend/routes/nft.go b/backend/routes/nft.go index 3a6abed5..81151b12 100644 --- a/backend/routes/nft.go +++ b/backend/routes/nft.go @@ -23,7 +23,7 @@ func InitNFTRoutes() { // http.HandleFunc("/unlike-nft", UnLikeNFT) http.HandleFunc("/get-top-nfts", getTopNFTs) http.HandleFunc("/get-hot-nfts", getHotNFTs) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/mint-nft-devnet", mintNFTDevnet) http.HandleFunc("/like-nft-devnet", likeNFTDevnet) http.HandleFunc("/unlike-nft-devnet", unlikeNFTDevnet) @@ -254,7 +254,7 @@ func mintNFTDevnet(w http.ResponseWriter, r *http.Request) { name := (*jsonBody)["name"] - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.MintNFTDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.MintNFTDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "mint_nft", strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height), name) @@ -281,7 +281,7 @@ func mintNFTDevnet(w http.ResponseWriter, r *http.Request) { // } // // // TODO: ensure that the nft exists -// _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2)", nftlikeReq.NFTKey, nftlikeReq.UserAddress) +// _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO NFTLikes (nftKey, liker) VALUES ($1, $2)", nftlikeReq.NFTKey, nftlikeReq.UserAddress) // if err != nil { // routeutils.WriteErrorJson(w, http.StatusBadRequest, "NFT already liked by user") // return @@ -302,7 +302,7 @@ func mintNFTDevnet(w http.ResponseWriter, r *http.Request) { // return // } // -// _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM nftlikes WHERE nftKey = $1 AND liker = $2", nftlikeReq.NFTKey, nftlikeReq.UserAddress) +// _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "DELETE FROM nftlikes WHERE nftKey = $1 AND liker = $2", nftlikeReq.NFTKey, nftlikeReq.UserAddress) // if err != nil { // routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to unlike NFT") // return @@ -387,7 +387,7 @@ func likeNFTDevnet(w http.ResponseWriter, r *http.Request) { // TODO: Read tokenId into a big.Int tokenId := (*jsonBody)["tokenId"] - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.LikeNFTDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.LikeNFTDevnet contract := os.Getenv("CANVAS_NFT_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "like_nft", tokenId, "0") @@ -414,7 +414,7 @@ func unlikeNFTDevnet(w http.ResponseWriter, r *http.Request) { tokenId := (*jsonBody)["tokenId"] - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.UnlikeNFTDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.UnlikeNFTDevnet contract := os.Getenv("CANVAS_NFT_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "unlike_nft", tokenId, "0") diff --git a/backend/routes/pixel.go b/backend/routes/pixel.go index 76770728..57db67d9 100644 --- a/backend/routes/pixel.go +++ b/backend/routes/pixel.go @@ -14,7 +14,7 @@ import ( func InitPixelRoutes() { http.HandleFunc("/get-pixel", getPixel) http.HandleFunc("/get-pixel-info", getPixelInfo) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/place-pixel-devnet", placePixelDevnet) http.HandleFunc("/place-extra-pixels-devnet", placeExtraPixelsDevnet) } @@ -30,16 +30,16 @@ func getPixel(w http.ResponseWriter, r *http.Request) { } // Check if position is within canvas bounds - if position < 0 || position >= (int(core.ArtPeaceBackend.CanvasConfig.Canvas.Width)*int(core.ArtPeaceBackend.CanvasConfig.Canvas.Height)) { + if position < 0 || position >= (int(core.AFKBackend.CanvasConfig.Canvas.Width)*int(core.AFKBackend.CanvasConfig.Canvas.Height)) { http.Error(w, "Position out of range", http.StatusBadRequest) return } - bitfieldType := "u" + strconv.Itoa(int(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth)) - pos := uint(position) * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth + bitfieldType := "u" + strconv.Itoa(int(core.AFKBackend.CanvasConfig.ColorsBitWidth)) + pos := uint(position) * core.AFKBackend.CanvasConfig.ColorsBitWidth ctx := context.Background() - val, err := core.ArtPeaceBackend.Databases.Redis.BitField(ctx, "canvas", "GET", bitfieldType, pos).Result() + val, err := core.AFKBackend.Databases.Redis.BitField(ctx, "canvas", "GET", bitfieldType, pos).Result() if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Error getting pixel") return @@ -109,7 +109,7 @@ func placePixelDevnet(w http.ResponseWriter, r *http.Request) { } // Validate position range - if position < 0 || position >= int(core.ArtPeaceBackend.CanvasConfig.Canvas.Width*core.ArtPeaceBackend.CanvasConfig.Canvas.Height) { + if position < 0 || position >= int(core.AFKBackend.CanvasConfig.Canvas.Width*core.AFKBackend.CanvasConfig.Canvas.Height) { routeutils.WriteErrorJson(w, http.StatusBadRequest, "Position out of range") return } @@ -125,7 +125,7 @@ func placePixelDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.PlacePixelDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.PlacePixelDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "place_pixel", strconv.Itoa(position), strconv.Itoa(color), strconv.Itoa(timestamp)) @@ -155,7 +155,7 @@ func placeExtraPixelsDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.PlaceExtraPixelsDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.PlaceExtraPixelsDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") positions := strconv.Itoa(len(jsonBody.ExtraPixels)) @@ -190,8 +190,8 @@ func placePixelRedis(w http.ResponseWriter, r *http.Request) { position := (*jsonBody)["position"] color := (*jsonBody)["color"] - canvasWidth := core.ArtPeaceBackend.CanvasConfig.Canvas.Width - canvasHeight := core.ArtPeaceBackend.CanvasConfig.Canvas.Height + canvasWidth := core.AFKBackend.CanvasConfig.Canvas.Width + canvasHeight := core.AFKBackend.CanvasConfig.Canvas.Height // Validate position range if position >= canvasWidth*canvasHeight { @@ -211,11 +211,11 @@ func placePixelRedis(w http.ResponseWriter, r *http.Request) { return } - bitfieldType := "u" + strconv.Itoa(int(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth)) - pos := position * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth + bitfieldType := "u" + strconv.Itoa(int(core.AFKBackend.CanvasConfig.ColorsBitWidth)) + pos := position * core.AFKBackend.CanvasConfig.ColorsBitWidth ctx := context.Background() - err = core.ArtPeaceBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, color).Err() + err = core.AFKBackend.Databases.Redis.BitField(ctx, "canvas", "SET", bitfieldType, pos, color).Err() if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Error setting pixel on redis") return diff --git a/backend/routes/quests.go b/backend/routes/quests.go index 2bdc6aee..0d9d01ab 100644 --- a/backend/routes/quests.go +++ b/backend/routes/quests.go @@ -126,7 +126,7 @@ func InitQuestsRoutes() { http.HandleFunc("/get-daily-quest-progress", GetDailyQuestProgress) http.HandleFunc("/get-today-quest-progress", GetTodayQuestProgress) http.HandleFunc("/get-main-quest-progress", GetMainQuestProgress) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/claim-today-quest-devnet", ClaimTodayQuestDevnet) http.HandleFunc("/claim-main-quest-devnet", ClaimMainQuestDevnet) http.HandleFunc("/increase-day-devnet", IncreaseDayDevnet) @@ -149,7 +149,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { // Setup daily quests tables for _, dailyQuestConfig := range questJson.DailyQuests.Quests { for idx, questConfig := range dailyQuestConfig.Quests { - _, err := core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuests (name, description, reward, day_index, quest_id, quest_type) VALUES ($1, $2, $3, $4, $5, $6)", questConfig.Name, questConfig.Description, questConfig.Reward, dailyQuestConfig.Day-1, idx, questConfig.ContractConfig.Type) + _, err := core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuests (name, description, reward, day_index, quest_id, quest_type) VALUES ($1, $2, $3, $4, $5, $6)", questConfig.Name, questConfig.Description, questConfig.Reward, dailyQuestConfig.Day-1, idx, questConfig.ContractConfig.Type) if err != nil { fmt.Println("Error inserting daily quest, ", idx, err) routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert daily quest") @@ -170,7 +170,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuestsInput (day_index, quest_id, input_key, input_value) VALUES ($1, $2, $3, $4)", dailyQuestConfig.Day-1, idx, paramIdx, param) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuestsInput (day_index, quest_id, input_key, input_value) VALUES ($1, $2, $3, $4)", dailyQuestConfig.Day-1, idx, paramIdx, param) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert daily quest input") return @@ -181,7 +181,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { claimParamIdx := 0 for _, claimParam := range questConfig.ContractConfig.ClaimParams { - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuestsClaimParams (day_index, quest_id, claim_key, claim_type, name, example, input) VALUES ($1, $2, $3, $4, $5, $6, $7)", dailyQuestConfig.Day-1, idx, claimParamIdx, claimParam.Type, claimParam.Name, claimParam.Example, claimParam.Input) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO DailyQuestsClaimParams (day_index, quest_id, claim_key, claim_type, name, example, input) VALUES ($1, $2, $3, $4, $5, $6, $7)", dailyQuestConfig.Day-1, idx, claimParamIdx, claimParam.Type, claimParam.Name, claimParam.Example, claimParam.Input) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert daily quest claim param") return @@ -194,7 +194,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { // Setup main quests tables for idx, questConfig := range questJson.MainQuests.Quests { - _, err := core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuests (name, description, reward, quest_type) VALUES ($1, $2, $3, $4)", questConfig.Name, questConfig.Description, questConfig.Reward, questConfig.ContractConfig.Type) + _, err := core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuests (name, description, reward, quest_type) VALUES ($1, $2, $3, $4)", questConfig.Name, questConfig.Description, questConfig.Reward, questConfig.ContractConfig.Type) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert main quest") return @@ -209,7 +209,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { return } - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuestsInput (quest_id, input_key, input_value) VALUES ($1, $2, $3)", idx, paramIdx, param) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuestsInput (quest_id, input_key, input_value) VALUES ($1, $2, $3)", idx, paramIdx, param) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert main quest input") return @@ -220,7 +220,7 @@ func InitQuests(w http.ResponseWriter, r *http.Request) { claimParamIdx := 0 for _, claimParam := range questConfig.ContractConfig.ClaimParams { - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuestsClaimParams (quest_id, claim_key, claim_type, name, example, input) VALUES ($1, $2, $3, $4, $5, $6)", idx, claimParamIdx, claimParam.Type, claimParam.Name, claimParam.Example, claimParam.Input) + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO MainQuestsClaimParams (quest_id, claim_key, claim_type, name, example, input) VALUES ($1, $2, $3, $4, $5, $6)", idx, claimParamIdx, claimParam.Type, claimParam.Name, claimParam.Example, claimParam.Input) if err != nil { routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to insert main quest claim param") return @@ -609,7 +609,7 @@ func ClaimTodayQuestDevnet(w http.ResponseWriter, r *http.Request) { calldata = "0" } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.ClaimTodayQuestDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.ClaimTodayQuestDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "claim_today_quest", strconv.Itoa(questId), calldata) @@ -649,7 +649,7 @@ func ClaimMainQuestDevnet(w http.ResponseWriter, r *http.Request) { calldata = "0" } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.ClaimTodayQuestDevnet // TODO + shellCmd := core.AFKBackend.BackendConfig.Scripts.ClaimTodayQuestDevnet // TODO contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "claim_main_quest", strconv.Itoa(questId), calldata) @@ -668,7 +668,7 @@ func IncreaseDayDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.IncreaseDayDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.IncreaseDayDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "increase_day_index") diff --git a/backend/routes/templates.go b/backend/routes/templates.go index 40f7ac69..ae92bc33 100644 --- a/backend/routes/templates.go +++ b/backend/routes/templates.go @@ -25,7 +25,7 @@ func InitTemplateRoutes() { http.HandleFunc("/get-chain-faction-templates", getChainFactionTemplates) http.HandleFunc("/add-template-img", addTemplateImg) http.HandleFunc("/add-template-data", addTemplateData) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { // http.HandleFunc("/add-template-devnet", addTemplateDevnet) http.HandleFunc("/add-faction-template-devnet", addFactionTemplateDevnet) http.HandleFunc("/remove-faction-template-devnet", removeFactionTemplateDevnet) @@ -394,7 +394,7 @@ func addTemplateDevnet(w http.ResponseWriter, r *http.Request) { rewardToken := (*jsonBody)["rewardToken"] - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.AddTemplateDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.AddTemplateDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "add_template", hash, nameHex, strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height), strconv.Itoa(reward), rewardToken) _, err = cmd.Output() @@ -444,7 +444,7 @@ func addFactionTemplateDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.AddFactionTemplateDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.AddFactionTemplateDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "add_faction_template", strconv.Itoa(factionId), hash, strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height)) _, err = cmd.Output() @@ -474,7 +474,7 @@ func removeFactionTemplateDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.RemoveFactionTemplateDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.RemoveFactionTemplateDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "remove_faction_template", strconv.Itoa(templateId)) _, err = cmd.Output() @@ -524,7 +524,7 @@ func addChainFactionTemplateDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.AddFactionTemplateDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.AddFactionTemplateDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "add_chain_faction_template", strconv.Itoa(factionId), hash, strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height)) _, err = cmd.Output() @@ -554,7 +554,7 @@ func removeChainFactionTemplateDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.RemoveFactionTemplateDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.RemoveFactionTemplateDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "remove_chain_faction_template", strconv.Itoa(templateId)) _, err = cmd.Output() diff --git a/backend/routes/user.go b/backend/routes/user.go index ffd8ae36..6be39f71 100644 --- a/backend/routes/user.go +++ b/backend/routes/user.go @@ -24,7 +24,7 @@ func InitUserRoutes() { http.HandleFunc("/get-pixel-count", getPixelCount) http.HandleFunc("/check-username-unique", checkUsernameUnique) http.HandleFunc("/get-user-rewards", getUserRewards) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/new-username-devnet", newUsernameDevnet) http.HandleFunc("/change-username-devnet", changeUsernameDevnet) } @@ -178,7 +178,7 @@ func newUsernameDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.NewUsernameDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.NewUsernameDevnet contract := os.Getenv("USERNAME_STORE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "claim_username", username) @@ -215,7 +215,7 @@ func changeUsernameDevnet(w http.ResponseWriter, r *http.Request) { return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.ChangeUsernameDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.ChangeUsernameDevnet contract := os.Getenv("USERNAME_STORE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "change_username", username) diff --git a/backend/routes/utils/middleware.go b/backend/routes/utils/middleware.go index 6a8b1737..ce0fcfa6 100644 --- a/backend/routes/utils/middleware.go +++ b/backend/routes/utils/middleware.go @@ -10,7 +10,7 @@ import ( // Return true if middleware stops the request func NonProductionMiddleware(w http.ResponseWriter, r *http.Request) bool { - if core.ArtPeaceBackend.BackendConfig.Production { + if core.AFKBackend.BackendConfig.Production { WriteErrorJson(w, http.StatusNotImplemented, "Route is disabled in production") return true } @@ -25,7 +25,7 @@ func AuthMiddleware(w http.ResponseWriter, r *http.Request) bool { func AdminMiddleware(w http.ResponseWriter, r *http.Request) bool { // TODO: Implement admin authentication - if core.ArtPeaceBackend.AdminMode { + if core.AFKBackend.AdminMode { return false } else { WriteErrorJson(w, http.StatusUnauthorized, "Admin is required") diff --git a/backend/routes/utils/responses.go b/backend/routes/utils/responses.go index f3f4c571..4de8d2fb 100644 --- a/backend/routes/utils/responses.go +++ b/backend/routes/utils/responses.go @@ -11,7 +11,7 @@ import ( ) func SetupAccessHeaders(w http.ResponseWriter) { - config := core.ArtPeaceBackend.BackendConfig.Http + config := core.AFKBackend.BackendConfig.Http // TODO: Process multiple origins in the future. if len(config.AllowOrigin) > 0 { @@ -65,18 +65,18 @@ func SendWebSocketMessage(message map[string]interface{}) { fmt.Println("Failed to marshal websocket message") return } - core.ArtPeaceBackend.WSConnectionsLock.Lock() - for idx, conn := range core.ArtPeaceBackend.WSConnections { + core.AFKBackend.WSConnectionsLock.Lock() + for idx, conn := range core.AFKBackend.WSConnections { if err := conn.WriteMessage(websocket.TextMessage, messageBytes); err != nil { fmt.Println(err) // Remove problematic connection conn.Close() - if idx < len(core.ArtPeaceBackend.WSConnections) { - core.ArtPeaceBackend.WSConnections = append(core.ArtPeaceBackend.WSConnections[:idx], core.ArtPeaceBackend.WSConnections[idx+1:]...) + if idx < len(core.AFKBackend.WSConnections) { + core.AFKBackend.WSConnections = append(core.AFKBackend.WSConnections[:idx], core.AFKBackend.WSConnections[idx+1:]...) } else { - core.ArtPeaceBackend.WSConnections = core.ArtPeaceBackend.WSConnections[:idx] + core.AFKBackend.WSConnections = core.AFKBackend.WSConnections[:idx] } } } - core.ArtPeaceBackend.WSConnectionsLock.Unlock() + core.AFKBackend.WSConnectionsLock.Unlock() } diff --git a/backend/routes/votableColors.go b/backend/routes/votableColors.go index a6a76936..05458a18 100644 --- a/backend/routes/votableColors.go +++ b/backend/routes/votableColors.go @@ -20,7 +20,7 @@ type VotableColor struct { func InitVotableColorsRoutes() { http.HandleFunc("/init-votable-colors", InitVotableColors) http.HandleFunc("/votable-colors", GetVotableColorsWithVoteCount) - if !core.ArtPeaceBackend.BackendConfig.Production { + if !core.AFKBackend.BackendConfig.Production { http.HandleFunc("/vote-color-devnet", voteColorDevnet) } } @@ -51,7 +51,7 @@ func InitVotableColors(w http.ResponseWriter, r *http.Request) { // Proceed with inserting unique colors into the VotableColors table for idx, colorHex := range *colors { - _, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), ` + _, err = core.AFKBackend.Databases.Postgres.Exec(context.Background(), ` INSERT INTO VotableColors (day_index, color_key, hex) VALUES ($1, $2, $3) `, 0, idx+1, colorHex) @@ -111,13 +111,13 @@ func voteColorDevnet(w http.ResponseWriter, r *http.Request) { // Validate color format // TODO: Get length from postgres - votableColorsLength := len(core.ArtPeaceBackend.CanvasConfig.VotableColors) + votableColorsLength := len(core.AFKBackend.CanvasConfig.VotableColors) if colorIndex <= 0 || colorIndex > votableColorsLength { routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid colorIndex, out of range") return } - shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.VoteColorDevnet + shellCmd := core.AFKBackend.BackendConfig.Scripts.VoteColorDevnet contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "vote_color", strconv.Itoa(colorIndex)) diff --git a/backend/routes/websocket.go b/backend/routes/websocket.go index d2067b95..1a566436 100644 --- a/backend/routes/websocket.go +++ b/backend/routes/websocket.go @@ -28,8 +28,8 @@ func wsReader(conn *websocket.Conn) { func wsEndpoint(w http.ResponseWriter, r *http.Request) { upgrader := websocket.Upgrader{ - ReadBufferSize: core.ArtPeaceBackend.BackendConfig.WebSocket.ReadBufferSize, - WriteBufferSize: core.ArtPeaceBackend.BackendConfig.WebSocket.WriteBufferSize, + ReadBufferSize: core.AFKBackend.BackendConfig.WebSocket.ReadBufferSize, + WriteBufferSize: core.AFKBackend.BackendConfig.WebSocket.WriteBufferSize, } upgrader.CheckOrigin = func(r *http.Request) bool { return true } @@ -39,8 +39,8 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { return } - core.ArtPeaceBackend.WSConnectionsLock.Lock() - core.ArtPeaceBackend.WSConnections = append(core.ArtPeaceBackend.WSConnections, ws) - core.ArtPeaceBackend.WSConnectionsLock.Unlock() + core.AFKBackend.WSConnectionsLock.Lock() + core.AFKBackend.WSConnections = append(core.AFKBackend.WSConnections, ws) + core.AFKBackend.WSConnectionsLock.Unlock() wsReader(ws) } diff --git a/backend/video/video.go b/backend/video/video.go index e2751c1d..38185622 100644 --- a/backend/video/video.go +++ b/backend/video/video.go @@ -15,9 +15,9 @@ import ( func GenerateImageFromCanvas(orderId int) { ctx := context.Background() - colorWidth := core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth - canvasWidth := int(core.ArtPeaceBackend.CanvasConfig.Canvas.Width) - canvasHeight := int(core.ArtPeaceBackend.CanvasConfig.Canvas.Height) + colorWidth := core.AFKBackend.CanvasConfig.ColorsBitWidth + canvasWidth := int(core.AFKBackend.CanvasConfig.Canvas.Width) + canvasHeight := int(core.AFKBackend.CanvasConfig.Canvas.Height) // TODO: Make generic & initialize only once colorPalette := make([]color.RGBA, 0) @@ -41,12 +41,12 @@ func GenerateImageFromCanvas(orderId int) { colorPalette = append(colorPalette, color.RGBA{uint8(r), uint8(g), uint8(b), 255}) } generatedImage := image.NewRGBA(image.Rect(0, 0, canvasWidth, canvasHeight)) - bitfieldType := "u" + strconv.Itoa(int(core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth)) + bitfieldType := "u" + strconv.Itoa(int(core.AFKBackend.CanvasConfig.ColorsBitWidth)) for y := 0; y < canvasHeight; y++ { for x := 0; x < canvasWidth; x++ { position := y*canvasWidth + x pos := position * int(colorWidth) - val, err := core.ArtPeaceBackend.Databases.Redis.BitField(ctx, "canvas", "GET", bitfieldType, pos).Result() + val, err := core.AFKBackend.Databases.Redis.BitField(ctx, "canvas", "GET", bitfieldType, pos).Result() if err != nil { fmt.Println("Failed to get bitfield value. Error: ", err) return From d73d7fd1b65034f932bf67145dde61f4a4f29fc3 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 12:23:05 +0100 Subject: [PATCH 06/15] refac: remove unused database configs --- backend/Dockerfile.consumer.prod | 1 - backend/cmd/backend/backend.go | 3 +-- backend/cmd/video-gen/video.go | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/backend/Dockerfile.consumer.prod b/backend/Dockerfile.consumer.prod index 1ae468fd..118c266f 100644 --- a/backend/Dockerfile.consumer.prod +++ b/backend/Dockerfile.consumer.prod @@ -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 diff --git a/backend/cmd/backend/backend.go b/backend/cmd/backend/backend.go index 02cb6457..993074c2 100644 --- a/backend/cmd/backend/backend.go +++ b/backend/cmd/backend/backend.go @@ -20,7 +20,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") admin := flag.Bool("admin", false, "Admin mode") @@ -32,7 +31,7 @@ func main() { panic(err) } - databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename) + databaseConfig, err := config.LoadDatabaseConfig() if err != nil { panic(err) } diff --git a/backend/cmd/video-gen/video.go b/backend/cmd/video-gen/video.go index 3bc132ab..8d8a4e14 100644 --- a/backend/cmd/video-gen/video.go +++ b/backend/cmd/video-gen/video.go @@ -11,7 +11,6 @@ import ( 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() @@ -21,7 +20,7 @@ func main() { panic(err) } - databaseConfig, err := config.LoadDatabaseConfig(*databaseConfigFilename) + databaseConfig, err := config.LoadDatabaseConfig() if err != nil { panic(err) } From d44a27636de594a2307e11ea1b86827ae6c7f30b Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 14:36:30 +0100 Subject: [PATCH 07/15] feat: add .env.example --- backend/.env.example | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 backend/.env.example diff --git a/backend/.env.example b/backend/.env.example new file mode 100644 index 00000000..2fce1fe3 --- /dev/null +++ b/backend/.env.example @@ -0,0 +1,6 @@ +REDIS_HOST=localhost +REDIS_PORT=6379 +POSTGRES_HOST=localhost +POSTGRES_PORT=5432 +POSTGRES_USER=postgres +POSTGRES_DATABASE=postgres From 2fbed775aba973bfc0b5d745ed6574b19927f37e Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 15:19:17 +0100 Subject: [PATCH 08/15] feat: load env variables --- backend/cmd/backend/backend.go | 8 ++++++++ backend/go.mod | 1 + backend/go.sum | 2 ++ 3 files changed, 11 insertions(+) diff --git a/backend/cmd/backend/backend.go b/backend/cmd/backend/backend.go index 993074c2..0c91fbec 100644 --- a/backend/cmd/backend/backend.go +++ b/backend/cmd/backend/backend.go @@ -2,10 +2,13 @@ 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/joho/godotenv" ) func isFlagSet(name string) bool { @@ -19,6 +22,11 @@ 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") diff --git a/backend/go.mod b/backend/go.mod index d440a9b9..3bf2e11e 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -18,6 +18,7 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect golang.org/x/crypto v0.18.0 // indirect golang.org/x/net v0.20.0 // indirect diff --git a/backend/go.sum b/backend/go.sum index 1886f2d9..440c1979 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -25,6 +25,8 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E= github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= From 787768a877313b6a858c5db7489bd4e2bb95ea07 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 15:19:37 +0100 Subject: [PATCH 09/15] chore: update README --- backend/README.md | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/backend/README.md b/backend/README.md index 8d6f3c33..1f88f975 100644 --- a/backend/README.md +++ b/backend/README.md @@ -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 @@ -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 \ No newline at end of file From b35d5913cc101f0dd39a404fe080a31fd680b19c Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 16:40:16 +0100 Subject: [PATCH 10/15] feat: use ENV variables in Dockerfile --- backend/Dockerfile | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 7e972195..038fd5f6 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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 @@ -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 From 21e5003471322c0c7c89969b4e9f684fced7875f Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 16:41:05 +0100 Subject: [PATCH 11/15] feat: use ENV variables in Dockerfile.consumer --- backend/Dockerfile.consumer | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/Dockerfile.consumer b/backend/Dockerfile.consumer index a3d72aac..835f246b 100644 --- a/backend/Dockerfile.consumer +++ b/backend/Dockerfile.consumer @@ -12,6 +12,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 From effad946063ebe971713ce3f5d35eebc372736f0 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 16:42:47 +0100 Subject: [PATCH 12/15] feat: use ENV variables in Dockerfile.prod --- backend/Dockerfile.consumer | 1 - backend/Dockerfile.prod | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/Dockerfile.consumer b/backend/Dockerfile.consumer index 835f246b..38e4303a 100644 --- a/backend/Dockerfile.consumer +++ b/backend/Dockerfile.consumer @@ -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 diff --git a/backend/Dockerfile.prod b/backend/Dockerfile.prod index a0e2ae7e..430edf6a 100644 --- a/backend/Dockerfile.prod +++ b/backend/Dockerfile.prod @@ -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 @@ -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 From 6d99ce016be3b5050f874d033a66ed1be55d6683 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 28 Aug 2024 22:37:18 +0100 Subject: [PATCH 13/15] fix: xix backend Golang --- backend/.env.example | 6 ++ backend/Dockerfile | 6 +- backend/Dockerfile.consumer | 2 + backend/Dockerfile.consumer.prod | 21 +++-- backend/Dockerfile.prod | 4 +- backend/README.md | 5 +- backend/cmd/backend/backend.go | 11 ++- backend/cmd/consumer/consumer.go | 12 ++- backend/cmd/video-gen/video.go | 3 +- backend/config/backend.go | 141 +++++++++++++++---------------- backend/config/database.go | 35 +++----- backend/core/backend.go | 2 +- 12 files changed, 128 insertions(+), 120 deletions(-) diff --git a/backend/.env.example b/backend/.env.example index 2fce1fe3..23c92e74 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -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 \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile index 038fd5f6..2ed96e61 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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 @@ -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 diff --git a/backend/Dockerfile.consumer b/backend/Dockerfile.consumer index 38e4303a..84914ba8 100644 --- a/backend/Dockerfile.consumer +++ b/backend/Dockerfile.consumer @@ -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 diff --git a/backend/Dockerfile.consumer.prod b/backend/Dockerfile.consumer.prod index 118c266f..966929ce 100644 --- a/backend/Dockerfile.consumer.prod +++ b/backend/Dockerfile.consumer.prod @@ -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 ./ @@ -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"] diff --git a/backend/Dockerfile.prod b/backend/Dockerfile.prod index 430edf6a..19d5ef7c 100644 --- a/backend/Dockerfile.prod +++ b/backend/Dockerfile.prod @@ -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 diff --git a/backend/README.md b/backend/README.md index 1f88f975..aa0bd7c3 100644 --- a/backend/README.md +++ b/backend/README.md @@ -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 ``` @@ -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 diff --git a/backend/cmd/backend/backend.go b/backend/cmd/backend/backend.go index 0c91fbec..33210d89 100644 --- a/backend/cmd/backend/backend.go +++ b/backend/cmd/backend/backend.go @@ -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") @@ -44,7 +43,7 @@ func main() { panic(err) } - backendConfig, err := config.LoadBackendConfig(*backendConfigFilename) + backendConfig, err := config.LoadBackendConfig() if err != nil { panic(err) } diff --git a/backend/cmd/consumer/consumer.go b/backend/cmd/consumer/consumer.go index 5c8f2f32..b4260194 100644 --- a/backend/cmd/consumer/consumer.go +++ b/backend/cmd/consumer/consumer.go @@ -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 { @@ -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() @@ -36,7 +43,7 @@ func main() { panic(err) } - backendConfig, err := config.LoadBackendConfig(*backendConfigFilename) + backendConfig, err := config.LoadBackendConfig() if err != nil { panic(err) } @@ -51,6 +58,7 @@ func main() { core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, false) routes.InitBaseRoutes() + routes.InitColorsRoutes() indexer.InitIndexerRoutes() routes.InitWebsocketRoutes() routes.InitNFTStaticRoutes() diff --git a/backend/cmd/video-gen/video.go b/backend/cmd/video-gen/video.go index 8d8a4e14..f1d87216 100644 --- a/backend/cmd/video-gen/video.go +++ b/backend/cmd/video-gen/video.go @@ -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() @@ -25,7 +24,7 @@ func main() { panic(err) } - backendConfig, err := config.LoadBackendConfig(*backendConfigFilename) + backendConfig, err := config.LoadBackendConfig() if err != nil { panic(err) } diff --git a/backend/config/backend.go b/backend/config/backend.go index f523337d..43d95bf4 100644 --- a/backend/config/backend.go +++ b/backend/config/backend.go @@ -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 } diff --git a/backend/config/database.go b/backend/config/database.go index 8e08889f..61f7fc35 100644 --- a/backend/config/database.go +++ b/backend/config/database.go @@ -1,39 +1,26 @@ package config import ( + "fmt" "os" "strconv" - "fmt" ) type RedisConfig struct { - Host string `json:"host"` - Port int `json:"port"` + Host string + Port int } type PostgresConfig struct { - Host string `json:"host"` - Port int `json:"port"` - User string `json:"user"` - Database string `json:"database"` + Host string + Port int + User string + Database string } type DatabaseConfig struct { - Redis RedisConfig `json:"redis"` - Postgres PostgresConfig `json:"postgres"` -} - -var DefaultDatabaseConfig = DatabaseConfig{ - Redis: RedisConfig{ - Host: "localhost", - Port: 6379, - }, - Postgres: PostgresConfig{ - Host: "localhost", - Port: 5432, - User: "afk-user", - Database: "afk-db", - }, + Redis RedisConfig + Postgres PostgresConfig } func LoadDatabaseConfig() (*DatabaseConfig, error) { @@ -49,8 +36,8 @@ func LoadDatabaseConfig() (*DatabaseConfig, error) { config := DatabaseConfig{ Redis: RedisConfig{ - Host: os.Getenv("REDIS_HOST"), - Port: redisPort, + Host: os.Getenv("REDIS_HOST"), + Port: redisPort, }, Postgres: PostgresConfig{ Host: os.Getenv("POSTGRES_HOST"), diff --git a/backend/core/backend.go b/backend/core/backend.go index afe1fc3b..9b734fe4 100644 --- a/backend/core/backend.go +++ b/backend/core/backend.go @@ -40,7 +40,7 @@ func (b *Backend) Start(port int) { func (b *Backend) GetBackendUrl() string { if b.BackendConfig.Production { - return "https://api.art-peace.net" + return "https://backend-pixel.onrender.com/" } else { return fmt.Sprintf("http://%s:%d", b.BackendConfig.Host, b.BackendConfig.Port) } From e58da422fdcaa6000d20c306f0f8a0f551eeabb6 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Thu, 29 Aug 2024 07:49:58 +0100 Subject: [PATCH 14/15] refac: modify consumer.go --- backend/cmd/consumer/consumer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/cmd/consumer/consumer.go b/backend/cmd/consumer/consumer.go index b4260194..77332ba7 100644 --- a/backend/cmd/consumer/consumer.go +++ b/backend/cmd/consumer/consumer.go @@ -58,7 +58,6 @@ func main() { core.AFKBackend = core.NewBackend(databases, canvasConfig, backendConfig, false) routes.InitBaseRoutes() - routes.InitColorsRoutes() indexer.InitIndexerRoutes() routes.InitWebsocketRoutes() routes.InitNFTStaticRoutes() From 358bb817bd84102b9fce91f0a0edc6c7f507a40e Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Thu, 29 Aug 2024 10:36:38 +0100 Subject: [PATCH 15/15] refac: move production mode to env --- backend/.env.example | 4 ++++ backend/config/backend.go | 7 ++++++- backend/core/backend.go | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/.env.example b/backend/.env.example index 44f12674..deb0192a 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -10,3 +10,7 @@ POSTGRES_DATABASE=postgres BACKEND_HOST=localhost BACKEND_PORT=8082 CONSUMER_PORT=8081 + +PRODUCTION=false + +BACKEND_URL=https://backend-pixel.onrender.com/ \ No newline at end of file diff --git a/backend/config/backend.go b/backend/config/backend.go index 43d95bf4..63d22a6c 100644 --- a/backend/config/backend.go +++ b/backend/config/backend.go @@ -57,6 +57,11 @@ func LoadBackendConfig() (*BackendConfig, error) { return nil, fmt.Errorf("invalid BACKEND_PORT: %v", err) } + production, err := strconv.ParseBool(os.Getenv("PRODUCTION")) + if err != nil { + return nil, fmt.Errorf("invalid PRODUCTION mode: %v", err) + } + config := BackendConfig{ Host: os.Getenv("BACKEND_HOST"), Port: backendPort, @@ -79,7 +84,7 @@ func LoadBackendConfig() (*BackendConfig, error) { AddFactionTemplateDevnet: "../scripts/add_faction_template.sh", RemoveFactionTemplateDevnet: "../scripts/remove_faction_template.sh", }, - Production: false, + Production: production, WebSocket: WebSocketConfig{ ReadBufferSize: 1024, WriteBufferSize: 1024, diff --git a/backend/core/backend.go b/backend/core/backend.go index 9b734fe4..5ab79d23 100644 --- a/backend/core/backend.go +++ b/backend/core/backend.go @@ -3,6 +3,7 @@ package core import ( "fmt" "net/http" + "os" "sync" "github.com/gorilla/websocket" @@ -40,7 +41,7 @@ func (b *Backend) Start(port int) { func (b *Backend) GetBackendUrl() string { if b.BackendConfig.Production { - return "https://backend-pixel.onrender.com/" + return os.Getenv("BACKEND_URL") } else { return fmt.Sprintf("http://%s:%d", b.BackendConfig.Host, b.BackendConfig.Port) }