Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image service #3

Merged
merged 39 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b49a35c
feat: image repo update
bookpanda Dec 31, 2023
ea77379
fix: delete, findbypetid logging
bookpanda Dec 31, 2023
c391695
feat: add objectkey
bookpanda Dec 31, 2023
77c3ce2
feat: image create
bookpanda Dec 31, 2023
dd698da
feat: image delete
bookpanda Dec 31, 2023
fe922cc
feat: assignpet
bookpanda Dec 31, 2023
d965d4b
feat: test upload success
bookpanda Dec 31, 2023
1ad9dca
fix: use auto gen client mock
bookpanda Dec 31, 2023
a411967
fix: remove logs
bookpanda Dec 31, 2023
e1ca716
feat: standard error messages
bookpanda Jan 1, 2024
960d811
feat: upload test
bookpanda Jan 1, 2024
4dc03c3
fix: image repo handwritten mock
bookpanda Jan 1, 2024
3876459
feat: findbypetid success test
bookpanda Jan 1, 2024
a7a217e
feat: findbypetid error test
bookpanda Jan 1, 2024
a4deb02
feat: assignpet test
bookpanda Jan 1, 2024
831c4d9
feat: delete test
bookpanda Jan 1, 2024
1dbefa6
refactor: remove unecessary pkg files
bookpanda Jan 1, 2024
7311131
fix: makefile path
bookpanda Jan 1, 2024
ce052d8
fix: docker port 5432
bookpanda Jan 1, 2024
a29e3b1
feat: auto build packages
bookpanda Jan 1, 2024
51e1d1a
feat: image repo add findone
bookpanda Jan 1, 2024
4192711
feat: deleteNotInDb and uploadNoPetId test
bookpanda Jan 1, 2024
cde3d60
feat: migrate table
bookpanda Jan 1, 2024
e65a089
feat: image upload random name (key)
bookpanda Jan 1, 2024
6e81634
feat: random utils
bookpanda Jan 1, 2024
5371a33
feat: random suffix for file name key
bookpanda Jan 1, 2024
c86c5de
fix: s3 url
bookpanda Jan 1, 2024
0492265
fix: delete image always remove row
bookpanda Jan 1, 2024
f3ac8a2
fix: error logs
bookpanda Jan 1, 2024
6e21fba
fix: delete in repo
bookpanda Jan 1, 2024
94edf19
fix: bucket delete
bookpanda Jan 1, 2024
e8cd01f
fix: use AWS url
bookpanda Jan 2, 2024
5120950
fix: assignPet, uuid -> primaryKey error
bookpanda Jan 2, 2024
ab63869
fix: imageservice delete remove objectkey
bookpanda Jan 2, 2024
1737c6e
feat: imageservice upload check if petid uuid
bookpanda Jan 2, 2024
f9b6448
refactor: remove redundant logs
bookpanda Jan 3, 2024
407eea0
feat: readme
bookpanda Jan 3, 2024
09b4733
fix: error msg
bookpanda Jan 3, 2024
2a8907d
fix: assginpet check if there is pet
bookpanda Jan 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Build

on:
workflow_dispatch:
push:
branches:
- main
- beta
- dev
- master

env:
IMAGE_NAME: ghcr.io/${{ github.repository }}
IMAGE_TAG: ${{ github.sha }}

jobs:
build:
name: Build
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

outputs:
BRANCH: ${{ steps.branch.outputs.BRANCH }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Set branch
id: branch
run: |
echo "::set-output name=BRANCH::${GITHUB_REF#refs/heads/}"

- name: Log in to the Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }},${{ env.IMAGE_NAME }}:latest
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ publish:
docker push ghcr.io/isd-sgcu/johnjud-file

mock-gen:
mockgen -source ./pkg/repository/image/image.repository.go -destination ./mocks/repository/image/image.mock.go
mockgen -source ./pkg/client/bucket/bucket.client.go -destination ./mocks/client/bucket/bucket.mock.go

test:
Expand All @@ -17,4 +16,4 @@ test:
go tool cover -html=coverage.out -o coverage.html

server:
go run ./src/.
go run ./cmd/.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# johnjud-file
# Johnjud-file

Johnjud-file is a file service that can upload, download, and delete files for the Johnjud project.

### What is Johnjud?
Johnjud is a pet adoption web application of the [CUVET For Animal Welfare Club](https://www.facebook.com/CUVETforAnimalWelfareClub)

## Stack

- golang
- gRPC
- go-fiber

## Getting Started

### Prerequisites

- golang 1.21 or [later](https://go.dev)
- docker
- makefile

### Installation

1. Clone this repo
2. Copy `config.example.yaml` in `config` and paste it in the same directory with `.example` removed from its name.

3. Run `go mod download` to download all the dependencies.

### Running
1. Run `docker-compose up -d`
2. Run `make server` or `go run ./src/.`

### Testing
1. Run `make test` or `go test -v -coverpkg ./... -coverprofile coverage.out -covermode count ./...`

## Other microservices/repositories of Johnjud
- [Johnjud-gateway](https://github.com/isd-sgcu/johnjud-gateway): Routing and request handling
- [Johnjud-auth](https://github.com/isd-sgcu/johnjud-auth): Authentication and authorization
- [Johnjud-backend](https://github.com/isd-sgcu/johnjud-backend): Main business logic
- [Johnjud-file](https://github.com/isd-sgcu/johnjud-file): File management service
- [Johnjud-proto](https://github.com/isd-sgcu/johnjud-proto): Protobuf files generator
- [Johnjud-go-proto](https://github.com/isd-sgcu/johnjud-go-proto): Generated protobuf files for golang
- [Johnjud-frontend](https://github.com/isd-sgcu/johnjud-frontend): Frontend web application
15 changes: 6 additions & 9 deletions client/bucket/bucket.client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package bucket
import (
"bytes"
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/isd-sgcu/johnjud-file/cfgldr"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -51,21 +49,20 @@ func (c *Client) Upload(file []byte, objectKey string) (string, string, error) {
return "", "", errors.Wrap(err, "Error while uploading the object")
}

return fmt.Sprintf("https://%v.s3.%v.amazonaws.com/%v", c.conf.Region, c.conf.BucketName, uploadOutput.Key), *uploadOutput.Key, nil
return uploadOutput.Location, *uploadOutput.Key, nil
}

func (c *Client) Delete(objectKey string) error {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()

var objectIds []types.ObjectIdentifier
objectIds = append(objectIds, types.ObjectIdentifier{Key: aws.String(objectKey)})

_, err := c.s3.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
input := &s3.DeleteObjectInput{
Bucket: aws.String(c.conf.BucketName),
Delete: &types.Delete{Objects: objectIds},
})
Key: aws.String(objectKey),
}

_, err := c.s3.DeleteObject(context.TODO(), input)

if err != nil {
log.Error().
Expand Down
8 changes: 5 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/isd-sgcu/johnjud-file/cfgldr"
"github.com/isd-sgcu/johnjud-file/database"
imageRepo "github.com/isd-sgcu/johnjud-file/internal/repository/image"
imageSvc "github.com/isd-sgcu/johnjud-file/internal/service/image"
"github.com/isd-sgcu/johnjud-file/internal/utils"
"github.com/isd-sgcu/johnjud-file/pkg/client/bucket"
imageRepo "github.com/isd-sgcu/johnjud-file/pkg/repository/image"
imageSvc "github.com/isd-sgcu/johnjud-file/pkg/service/image"
imagePb "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
Expand Down Expand Up @@ -120,9 +121,10 @@ func main() {
awsClient := s3.NewFromConfig(sdkConfig)
bucketClient := bucket.NewClient(conf.S3, awsClient)

randomUtils := utils.NewRandomUtil()
imageRepository := imageRepo.NewRepository(db)

imageService := imageSvc.NewService(bucketClient, imageRepository)
imageService := imageSvc.NewService(bucketClient, imageRepository, randomUtils)

grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
imagePb.RegisterImageServiceServer(grpcServer, imageService)
Expand Down
13 changes: 13 additions & 0 deletions constant/error.constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constant

const InternalServerErrorMessage = "Internal server error"

const UploadToBucketErrorMessage = "Error uploading to bucket client"
const DeleteFromBucketErrorMessage = "Error deleting from bucket client"

const ImageNotFoundErrorMessage = "Image not found"
const CreateImageErrorMessage = "Error creating image in db"
const DeleteImageErrorMessage = "Error deleting image from db"
const PrimaryKeyRequiredErrorMessage = "UUID Primary key (petId) required"
const PetIdNotUUIDErrorMessage = "Pet id is not uuid"
const PetIdNotFoundErrorMessage = "Pet id not found"
6 changes: 6 additions & 0 deletions database/postgresql.connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/isd-sgcu/johnjud-file/cfgldr"
"github.com/isd-sgcu/johnjud-file/internal/model"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
Expand All @@ -24,5 +25,10 @@ func InitPostgresDatabase(conf *cfgldr.Database, isDebug bool) (db *gorm.DB, err
return nil, err
}

err = db.AutoMigrate(&model.Image{}, &model.Pet{})
if err != nil {
return nil, err
}

return
}
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
volumes:
- postgres:/var/lib/postgresql/data
ports:
- "5433:5432"
- "5432:5432"

volumes:
postgres:
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.5
require (
github.com/golang/mock v1.6.0
github.com/google/uuid v1.5.0
github.com/isd-sgcu/johnjud-go-proto v0.1.5
github.com/isd-sgcu/johnjud-go-proto v0.2.4
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
Expand Down Expand Up @@ -36,7 +36,9 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-faker/faker/v4 v4.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -50,12 +52,15 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-faker/faker/v4 v4.2.0 h1:dGebOupKwssrODV51E0zbMrv5e2gO9VWSLNC1WDCpWg=
github.com/go-faker/faker/v4 v4.2.0/go.mod h1:F/bBy8GH9NxOxMInug5Gx4WYeG6fHJZ8Ol/dhcpRub4=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
Expand All @@ -60,6 +62,16 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/isd-sgcu/johnjud-go-proto v0.1.5 h1:ZhMb73xXOSM2OlsfQ8wv6rmbfSIrXwbBumIx/vtdgHc=
github.com/isd-sgcu/johnjud-go-proto v0.1.5/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.2.0 h1:jefDbCD3lzwcm+4VK5eemghmlSqa6lV+S6FMGlIcXtY=
github.com/isd-sgcu/johnjud-go-proto v0.2.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.2.1 h1:0hsqV2PsYCAdVFglsRipiTWxZOJzWnBqttepOagr/tE=
github.com/isd-sgcu/johnjud-go-proto v0.2.1/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.2.2 h1:TOAmbwy/I+8/J5LPZH0ZN7lSLczBiZe1fs88gH8XrhY=
github.com/isd-sgcu/johnjud-go-proto v0.2.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.2.3 h1:PaT9x4hurtBE7eR2X9/eczhxcDiVgA6z4YZamx233Qo=
github.com/isd-sgcu/johnjud-go-proto v0.2.3/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.2.4 h1:amYofKCZGMKc+VQARmsZSPgmpxEJwQjv6VfbCxI9wLw=
github.com/isd-sgcu/johnjud-go-proto v0.2.4/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down Expand Up @@ -114,6 +126,7 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
7 changes: 4 additions & 3 deletions internal/model/image.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

type Image struct {
Base
PetID *uuid.UUID `json:"pet_id" gorm:"index:idx_name,unique"`
Pet *Pet `json:"pet" gorm:"foreignKey:PetID;constraint:OnUpdate:CASCADE;OnDelete:SET NULL;"`
ImageUrl string `json:"image_url" gorm:"mediumtext"`
PetID *uuid.UUID `json:"pet_id" gorm:"index:idx_name,unique"`
Pet *Pet `json:"pet" gorm:"foreignKey:PetID;constraint:OnUpdate:CASCADE;OnDelete:SET NULL;"`
ImageUrl string `json:"image_url" gorm:"mediumtext"`
ObjectKey string `json:"object_key" gorm:"mediumtext"`
}
13 changes: 11 additions & 2 deletions internal/repository/image/image.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package image

import (
"github.com/isd-sgcu/johnjud-file/internal/model"
"github.com/isd-sgcu/johnjud-file/pkg/repository/image"
"gorm.io/gorm"
)

type repositoryImpl struct {
db *gorm.DB
}

func NewRepository(db *gorm.DB) *repositoryImpl {
func NewRepository(db *gorm.DB) image.Repository {
return &repositoryImpl{db: db}
}

func (r *repositoryImpl) FindOne(id string, result *model.Image) error {
return r.db.Model(&model.Image{}).First(result, "id = ?", id).Error
}

func (r *repositoryImpl) FindByPetId(id string, result *[]*model.Image) error {
return r.db.Model(&model.Image{}).Find(&result, "pet_id = ?", id).Error
}
Expand All @@ -21,6 +26,10 @@ func (r *repositoryImpl) Create(in *model.Image) error {
return r.db.Create(&in).Error
}

func (r *repositoryImpl) Update(id string, in *model.Image) error {
return r.db.Where(id, "id = ?", id).Updates(&in).First(&in, "id = ?", id).Error
}

func (r *repositoryImpl) Delete(id string) error {
return r.db.First(id).Delete(&model.Image{}).Error
return r.db.Where("id = ?", id).Delete(&model.Image{}).Error
}
Loading
Loading