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

Make monolith #39

Merged
merged 32 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/main.go"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[proxy]
app_port = 0
enabled = false
proxy_port = 0

[screen]
clear_on_rebuild = false
keep_scroll = true
26 changes: 23 additions & 3 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ APP_PORT=3001
APP_ENV=development
APP_MAX_FILE_SIZE=10

SERVICE_AUTH=localhost:3002
SERVICE_BACKEND=localhost:3003
SERVICE_FILE=localhost:3004
DB_URL=postgres://root:root@localhost:5432/johnjud_db

JWT_SECRET=secret
JWT_EXPIRES_IN=3600
JWT_REFRESH_TOKEN_TTL=604800
JWT_ISSUER=issuer
JWT_RESET_TOKEN_TTL=900

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

AUTH_CLIENT_URL=http://localhost:3000

SENDGRID_API_KEY=api_key
SENDGRID_NAME=johnjud
[email protected]

BUCKET_ENDPOINT=BUCKET_ENDPOINT
BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY
BUCKET_SECRET_KEY=BUCKET_SECRET_KEY
BUCKET_NAME=johnjud-pet-images
BUCKET_USE_SSL=true
40 changes: 20 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ jobs:
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max

update-gitops:
name: Update gitops repository
runs-on: ubuntu-latest
# update-gitops:
# name: Update gitops repository
# runs-on: ubuntu-latest

steps:
- name: Checkout gitops repository
uses: actions/checkout@v4
with:
repository: isd-sgcu/isd-gitops
token: ${{ secrets.GITOPS_TOKEN }}
# steps:
# - name: Checkout gitops repository
# uses: actions/checkout@v4
# with:
# repository: isd-sgcu/isd-gitops
# token: ${{ secrets.GITOPS_TOKEN }}

- name: Update image tag
uses: mikefarah/yq@master
env:
NEW_TAG: ${{ github.ref_type == 'tag' && github.ref_name || env.IMAGE_TAG }}
with:
cmd: yq -i '.gateway.imageTag = strenv(NEW_TAG)' projects/johnjud/values/gateway-dev.values.yaml
# - name: Update image tag
# uses: mikefarah/yq@master
# env:
# NEW_TAG: ${{ github.ref_type == 'tag' && github.ref_name || env.IMAGE_TAG }}
# with:
# cmd: yq -i '.gateway.imageTag = strenv(NEW_TAG)' projects/johnjud/values/gateway-dev.values.yaml

- name: Commit & Push changes
uses: actions-js/[email protected]
with:
github_token: ${{ secrets.GITOPS_TOKEN }}
repository: isd-sgcu/isd-gitops
# - name: Commit & Push changes
# uses: actions-js/[email protected]
# with:
# github_token: ${{ secrets.GITOPS_TOKEN }}
# repository: isd-sgcu/isd-gitops
2 changes: 1 addition & 1 deletion .github/workflows/run-unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ jobs:
- name: Test
run: |
go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/...
go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/...
go tool cover -func="./coverage.out"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ github.com
coverage.out
coverage.html

token.txt
token.txt
tmp
docker-compose.qa.yaml
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
COPY . .

# Build the application
RUN go build -o server ./src/.
RUN go build -o server ./cmd/main.go

# Create master image
FROM alpine AS master
Expand Down
25 changes: 15 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ publish:

test:
go vet ./...
go test -v -coverpkg ./src/app/... -coverprofile coverage.out -covermode count ./src/app/...
go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/...
go tool cover -func=coverage.out
go tool cover -html=coverage.out -o coverage.html

server:
. ./tools/export-env.sh ; go run ./src/.
go run ./cmd/.

docker-qa:
docker-compose -f docker-compose.qa.yaml up

mock-gen:
mockgen -source ./src/pkg/service/auth/auth.service.go -destination ./src/mocks/service/auth/auth.mock.go
mockgen -source ./src/pkg/service/user/user.service.go -destination ./src/mocks/service/user/user.mock.go
mockgen -source ./src/pkg/service/pet/pet.service.go -destination ./src/mocks/service/pet/pet.mock.go
mockgen -source ./src/pkg/service/like/like.service.go -destination ./src/mocks/service/like/like.mock.go
mockgen -source ./src/pkg/service/image/image.service.go -destination ./src/mocks/service/image/image.mock.go
mockgen -source ./src/app/validator/validator.go -destination ./src/mocks/validator/validator.mock.go
mockgen -source ./src/app/router/context.go -destination ./src/mocks/router/context.mock.go
mockgen -source ./internal/cache/cache.repository.go -destination ./mocks/repository/cache/cache.mock.go
mockgen -source ./internal/auth/auth.repository.go -destination ./mocks/repository/auth/auth.mock.go
mockgen -source ./internal/auth/auth.service.go -destination ./mocks/service/auth/auth.mock.go
mockgen -source ./internal/user/user.service.go -destination ./mocks/service/user/user.mock.go
mockgen -source ./internal/pet/pet.service.go -destination ./mocks/service/pet/pet.mock.go
mockgen -source ./client/bucket/bucket.client.go -destination ./mocks/client/bucket/bucket.mock.go
mockgen -source ./internal/image/image.service.go -destination ./mocks/service/image/image.mock.go
mockgen -source ./internal/validator/validator.go -destination ./mocks/validator/validator.mock.go
mockgen -source ./internal/router/context.go -destination ./mocks/router/context.mock.go

create-doc:
swag init -d ./src -o ./src/docs -md ./src/docs/markdown
swag init -d ./internal -g ../cmd/main.go -o ./docs -md ./docs/markdown --parseDependency --parseInternal
23 changes: 8 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Johnjud-gateway
# Johnjud-backend

Johnjud-gateway is a routing and request handling service for the Johnjud project.
Johnjud-backend is a routing and request handling service 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
Expand All @@ -22,22 +21,16 @@ Johnjud is a pet adoption web application of the [CUVET For Animal Welfare Club]
### Installation

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

2. Copy `.env.template` in root directory and paste it in the same directory as `.env` with proper values.
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/.`
1. Run `docker-compose -f docker-compose.example.yaml up`
2. Run `make server` or `go run ./cmd/.`

### 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
## Other repositories of Johnjud
- [Johnjud-backend](https://github.com/isd-sgcu/johnjud-backend)
- [Johnjud-frontend](https://github.com/isd-sgcu/johnjud-frontend)
99 changes: 99 additions & 0 deletions client/bucket/bucket.client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package bucket

import (
"bytes"
"context"
"time"

"github.com/isd-sgcu/johnjud-backend/config"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

type Client interface {
Upload([]byte, string) (string, string, error)
Delete(string) error
DeleteMany([]string) error
}

type clientImpl struct {
conf config.Bucket
minio *minio.Client
}

func NewClient(conf config.Bucket, minioClient *minio.Client) Client {
return &clientImpl{conf: conf, minio: minioClient}
}

func (c *clientImpl) Upload(file []byte, objectKey string) (string, string, error) {
ctx := context.Background()
_, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()

buffer := bytes.NewReader(file)

uploadOutput, err := c.minio.PutObject(context.Background(), c.conf.BucketName, objectKey, buffer,
buffer.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
log.Error().
Err(err).
Str("service", "file").
Str("module", "bucket client").
Msgf("Couldn't upload object to %v:%v.", c.conf.BucketName, objectKey)

return "", "", errors.Wrap(err, "Error while uploading the object")
}

return c.getURL(objectKey), uploadOutput.Key, nil
}

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

opts := minio.RemoveObjectOptions{
GovernanceBypass: true,
}
err := c.minio.RemoveObject(context.Background(), c.conf.BucketName, objectKey, opts)
if err != nil {
log.Error().
Err(err).
Str("service", "file").
Str("module", "bucket client").
Msgf("Couldn't delete object from bucket %v:%v.", c.conf.BucketName, objectKey)

return errors.Wrap(err, "Error while deleting the object")
}

return nil
}

func (c *clientImpl) DeleteMany(objectKeys []string) error {
ctx := context.Background()
_, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()

opts := minio.RemoveObjectOptions{
GovernanceBypass: true,
}
for _, objectKey := range objectKeys {
err := c.minio.RemoveObject(context.Background(), c.conf.BucketName, objectKey, opts)
if err != nil {
log.Error().
Err(err).
Str("service", "file").
Str("module", "bucket client").
Msgf("Couldn't delete object from bucket %v:%v.", c.conf.BucketName, objectKey)

return errors.Wrap(err, "Error while deleting the object")
}
}

return nil
}

func (c *clientImpl) getURL(objectKey string) string {
return "https://" + c.conf.Endpoint + "/" + c.conf.BucketName + "/" + objectKey
}
Loading
Loading