Skip to content

Commit

Permalink
feat: added check to prevent canvas re-init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayoazeez26 committed Apr 18, 2024
1 parent 5c5a31f commit e9cbf38
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions backend/routes/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ func InitCanvasRoutes() {
}

func initCanvas(w http.ResponseWriter, r *http.Request) {
// TODO: Check if canvas already exists
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
}

canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
// Check if the 'canvas' property exists in core.ArtPeaceBackend.CanvasConfig
if core.ArtPeaceBackend.CanvasConfig != nil && core.ArtPeaceBackend.CanvasConfig.Canvas != nil {
// Calculate totalByteSize
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
}

// Create canvas
canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
} else {
fmt.Println("Canvas config not found")
// Handle the case where 'canvas' property does not exist
}
}


func getCanvas(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
val, err := core.ArtPeaceBackend.Databases.Redis.Get(ctx, "canvas").Result()
Expand Down

0 comments on commit e9cbf38

Please sign in to comment.