Skip to content

Commit

Permalink
added healthchecks, edited image generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussss1 committed Jun 7, 2023
1 parent 6391bcb commit 0ed510f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 64 deletions.
35 changes: 35 additions & 0 deletions docker-compose-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8081/health" ]
interval: 1s
timeout: 2s
retries: 2

chat:
build:
Expand All @@ -35,6 +40,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9000/health" ]
interval: 1s
timeout: 2s
retries: 2

user:
build:
Expand All @@ -52,6 +62,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9001/health" ]
interval: 1s
timeout: 2s
retries: 2

messages:
build:
Expand All @@ -71,6 +86,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9002/health" ]
interval: 1s
timeout: 2s
retries: 2

consumer:
build:
Expand All @@ -88,6 +108,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9003/health" ]
interval: 1s
timeout: 2s
retries: 2

producer:
build:
Expand All @@ -105,6 +130,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9004/health" ]
interval: 1s
timeout: 2s
retries: 2

auth:
build:
Expand All @@ -122,6 +152,11 @@ services:
restart: always
networks:
- backend-network
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9005/health" ]
interval: 1s
timeout: 2s
retries: 2

rabbitmq:
image: rabbitmq:latest
Expand Down
9 changes: 5 additions & 4 deletions internal/monolithic_services/images/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"project/internal/monolithic_services/images"
"project/internal/pkg/image_generation"
)

type usecase struct {
Expand All @@ -16,10 +17,10 @@ func NewImagesUsecase(imagesRepo images.Repository) images.Usecase {
}

func (u usecase) UploadGeneratedImage(ctx context.Context, bucketName string, filename string, firstCharacterName string) error {
//err := image_generation.GenerateAvatar(firstCharacterName)
//if err != nil {
// return err
//}
err := image_generation.GenerateAvatar(firstCharacterName)
if err != nil {
return err
}

file, err := os.Open("background.png")
if err != nil {
Expand Down
134 changes: 74 additions & 60 deletions internal/pkg/image_generation/image_generation.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
package image_generation

import (
"crypto/rand"
"github.com/fogleman/gg"
"github.com/labstack/gommon/log"
"image"
"image/color"
"image/draw"
"image/png"
"math"
"math/big"
"os"
"strings"
)

func GenerateAvatar(firstCharacterName string) error {
//firstCharacterName = strings.ToUpper(firstCharacterName)
//img := image.NewRGBA(image.Rect(0, 0, 1024, 1024))
//
//rBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
//if err != nil {
// return err
//}
//
//gBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
//if err != nil {
// return err
//}
//
//bBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
//if err != nil {
// return err
//}
//
//color := color.RGBA{uint8(rBig.Uint64()), uint8(gBig.Uint64()), uint8(bBig.Uint64()), 255}
//
//draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.Point{}, draw.Src)
//
//file, err := os.Create("background.png")
//if err != nil {
// return err
//}
//defer func() {
// err = file.Close()
// if err != nil {
// log.Error(err)
// }
//}()
//
//if err := png.Encode(file, img); err != nil {
// return err
//}
//
//const S = 1024
//im, err := gg.LoadImage("background.png")
//if err != nil {
// return err
//}
//
//dc := gg.NewContext(S, S)
//dc.SetRGB(1, 1, 1)
//dc.Clear()
//dc.SetRGB(1, 1, 1)
//if err := dc.LoadFontFace("str.ttf", 728); err != nil {
// return err
//}
//
//dc.DrawRoundedRectangle(0, 0, 512, 512, 0)
//dc.DrawImage(im, 0, 0)
//dc.DrawStringAnchored(firstCharacterName, S/2, S/2, 0.5, 0.5)
//dc.Clip()
//
//err = dc.SavePNG("background.png")
//if err != nil {
// return err
//}
firstCharacterName = strings.ToUpper(firstCharacterName)
img := image.NewRGBA(image.Rect(0, 0, 1024, 1024))

rBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
return err
}

gBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
return err
}

bBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
return err
}

color := color.RGBA{uint8(rBig.Uint64()), uint8(gBig.Uint64()), uint8(bBig.Uint64()), 255}

draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.Point{}, draw.Src)

file, err := os.Create("background.png")
if err != nil {
return err
}
defer func() {
err = file.Close()
if err != nil {
log.Error(err)
}
}()

if err := png.Encode(file, img); err != nil {
return err
}

const S = 1024
im, err := gg.LoadImage("background.png")
if err != nil {
return err
}

dc := gg.NewContext(S, S)
dc.SetRGB(1, 1, 1)
dc.Clear()
dc.SetRGB(1, 1, 1)
if err := dc.LoadFontFace("str.ttf", 728); err != nil {
return err
}

dc.DrawRoundedRectangle(0, 0, 512, 512, 0)
dc.DrawImage(im, 0, 0)
dc.DrawStringAnchored(firstCharacterName, S/2, S/2, 0.5, 0.5)
dc.Clip()

err = dc.SavePNG("background.png")
if err != nil {
return err
}

return nil
}

0 comments on commit 0ed510f

Please sign in to comment.