Skip to content

Commit

Permalink
Feat/prevent reinit (#60)
Browse files Browse the repository at this point in the history
* feat: added check to prevent canvas re-init

* feat: format code

* feat: formatted file

* feat: added extra check to func

* nits and patch build

---------

Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
Ayoazeez26 and b-j-roberts authored Apr 19, 2024
1 parent 584b13f commit 3ce4077
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions backend/routes/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ 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
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
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 already initialized")
}

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")
}

func getCanvas(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 3ce4077

Please sign in to comment.