Skip to content

Commit

Permalink
Merge pull request #2 from andhikasamudra/feat/v2-update
Browse files Browse the repository at this point in the history
feat: add some new utilities such as auth, grpc and other
  • Loading branch information
andhikasamudra committed Jan 28, 2024
2 parents 149d1fb + 769f6ec commit 7b0a7e7
Show file tree
Hide file tree
Showing 66 changed files with 2,506 additions and 278 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
## Build
FROM golang:1.19.0-alpine AS builder
FROM golang:1.21.0-alpine AS builder

WORKDIR /build

COPY . .

RUN GOOS=linux go build -mod=vendor -ldflags="-s -w" -o app
RUN go mod vendor

RUN env GOOS=linux GOARCH=amd64 go build -mod=vendor -ldflags="-s -w" -o app

## Deploy
FROM alpine
Expand All @@ -16,6 +18,6 @@ COPY --from=builder /build .

EXPOSE 8080

USER nonroot:nonroot
RUN chmod +x ./app

CMD ["./app"]
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
module="github.com/andhikasamudra/fiber-starter-pack"

create-migration: ## Create new migration file. It takes parameter `file` as filename. Usage: `make create-migration file=add_column_time`
ls -x migrations/*.sql | tail -1 | awk -F"migrations/" '{print $$2}' | awk -F"_" '{print $$1}' | { read cur_v; expr $$cur_v + 1; } | { read new_v; printf "%06d" $$new_v; } | { read v; touch migrations/$$v"_$(file)".up.sql; touch migrations/$$v"_$(file)".down.sql; }
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

db-migrate:
migrate -database ${DBUrl} -lock-timeout 30 -path migrations up

generate-proto:
protoc --proto_path=protobuf/proto/ \
--go_out=protobuf \
--go_opt=module=$(module) \
--go-grpc_out=protobuf \
--go-grpc_opt=module=$(module) \
protobuf/proto/modules/error/*.proto \
protobuf/proto/modules/account/*.proto \
protobuf/proto/services/*.proto
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Fiber Starter Pack

build with [Fiber](https://gofiber.io/) and [Bun](https://bun.uptrace.dev/)
build with [Fiber](https://gofiber.io/) and [Bun](https://bun.uptrace.dev/) and [GRPC](https://grpc.io/)

setup to your local env first

Expand All @@ -21,6 +21,13 @@ to run
go run main.go
```

OR

using docker compose
```shell
docker-compose up
```

## Architecture

```shell
Expand All @@ -41,14 +48,7 @@ in `pkg` there is `domain` folder to grouped each related domain modules
such as `services`, `handler`, `models`, `dto`
the `route.go` file will be represent of each domain api routes.

#### why every domain should have different routes ?

the only i think that can do is whenever you need to separate this monolithic service into multiple services
you can just plug the `domain` folder and put it in new service and you ready to run
### Why every domain should have different routes ?

### Todo
- [ ] add new event driven connection
- [ ] add another domain layer and try communicate with event
- [ ] add dockerfile to allow run with docker
- [ ] add `docker-compose.yaml` to easier run with docker locally
- [ ] add `auth` domain to represent authorization example
well the perfect case is when you need to separate this domain into multiple services
you can just "put this out" into new services
15 changes: 15 additions & 0 deletions adapter/postgres/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package postgres

import (
"github.com/uptrace/bun"
)

type Interface interface {
HealthCheck() error
Commit() error
Conn() *bun.DB
Rollback() error
BeginTransaction() error
GetConnection() bun.IDB
Close()
}
100 changes: 100 additions & 0 deletions adapter/postgres/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package postgres

import (
"database/sql"
"fmt"
"os"

"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)

type Adapter struct {
Db *bun.DB
Tx *bun.Tx
}

func NewAdapter() *Adapter {
return &Adapter{}
}

// HealthCheck ...
func (a *Adapter) HealthCheck() error {
_, err := a.Db.Exec("SELECT 1")
if err != nil {
fmt.Println("PostgreSQL is down")

return err
}

return nil
}

func (a *Adapter) BeginTransaction() error {
tx, err := a.Db.Begin()
if err != nil {
return err
}

a.Tx = &tx

return nil
}

func (a *Adapter) Commit() error {
if a.Tx == nil {
return fmt.Errorf("transaction not ready")
}

err := a.Tx.Commit()
if err != nil {
return err
}

a.Tx = nil

return nil
}

func (a *Adapter) Rollback() error {
if a.Tx == nil {
return fmt.Errorf("transaction not ready")
}

err := a.Tx.Rollback()
if err != nil {
return err
}

return nil
}

func (a *Adapter) Conn() *bun.DB {
return a.Db
}

func (a *Adapter) GetConnection() bun.IDB {
if a.Tx != nil {
return a.Tx
}

return a.Db
}

func (a *Adapter) Connect() {
dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
os.Getenv("POSTGRES_DB_USER"),
os.Getenv("POSTGRES_DB_PASS"),
os.Getenv("POSTGRES_DB_HOST"),
os.Getenv("POSTGRES_DB_PORT"),
os.Getenv("POSTGRES_DB_NAME"),
)
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))

a.Db = bun.NewDB(sqldb, pgdialect.New())
}

func (a *Adapter) Close() {
a.Db.Close()
}
1 change: 1 addition & 0 deletions adapter/redis/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package redis
23 changes: 23 additions & 0 deletions adapter/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package redis

import (
"github.com/andhikasamudra/fiber-starter-pack/internal/env"
"github.com/redis/go-redis/v9"
)

type Adapter struct {
Client *redis.Client
}

func NewAdapter() *Adapter {
return &Adapter{}
}

func (a *Adapter) GetRedisConnection() {
conn := redis.NewClient(&redis.Options{
Addr: env.RedisURL(),
Password: "", // no password set
DB: 0, // use default DB
})
a.Client = conn
}
25 changes: 0 additions & 25 deletions config/database.go

This file was deleted.

50 changes: 50 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '3.8'
services:
app:
container_name: fsp_app
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
ports:
- "8080:8080"
- "50001:50001"
depends_on:
- db
restart: always
db:
container_name: fsp_db
image: postgres:alpine
environment:
- POSTGRES_USER=${POSTGRES_DB_USER}
- POSTGRES_PASSWORD=${POSTGRES_DB_PASS}
- POSTGRES_DB=${POSTGRES_DB_NAME}
ports:
- "5432:5432"
restart: always
# volumes:
# - db_data:/var/lib/postgresql/data

redis:
container_name: fsp_redis
image: redis:latest
ports:
- "6379:6379"
restart: always
# volumes:
# - redis_data:/data

migrate:
image: migrate/migrate
depends_on:
- db
env_file:
- .env
volumes:
- ./migrations/postgres:/migrations # Mount the directory containing migrations
command: [ "-path", "/migrations", "-database", "postgres://${POSTGRES_DB_USER}:${POSTGRES_DB_PASS}@fsp_db:5432/${POSTGRES_DB_NAME}?sslmode=disable", "up" ]

volumes:
db_data:
redis_data:
21 changes: 15 additions & 6 deletions env-example.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
export DB_USER=""
export DB_PASS=""
export DB_HOST=""
export DB_PORT=""
export DB_NAME=""
export DBUrl="postgres://username:password@localhost:5432/dbname?sslmode=disable"
export POSTGRES_DB_USER=""
export POSTGRES_DB_PASS=""
export POSTGRES_DB_HOST=""
export POSTGRES_DB_PORT=""
export POSTGRES_DB_NAME=""
export DBUrl="postgres://username:password@localhost:5432/dbname?sslmode=disable"
export SECRET_KEY=""
export REDIS_URL=""
export APP_PORT=""
export SMTP_HOST=""
export SMTP_PORT=""
export SMTP_USERNAME=""
export SMTP_PASSWORD=""
export MAIL_FROM=""
export BASE_URL=""
39 changes: 30 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/andhikasamudra/fiber-starter-pack
go 1.19

require (
github.com/gofiber/fiber/v2 v2.41.0
github.com/google/uuid v1.3.0
github.com/gofiber/fiber/v2 v2.51.0
github.com/google/uuid v1.4.0
github.com/pkg/errors v0.9.1
github.com/uptrace/bun v1.1.10
github.com/uptrace/bun/dialect/pgdialect v1.1.10
Expand All @@ -13,21 +13,42 @@ require (
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/gofiber/contrib/jwt v1.0.8 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/redis/go-redis/v9 v9.4.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.43.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
github.com/xhit/go-simple-mail/v2 v2.16.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
mellium.im/sasl v0.3.1 // indirect
)
Loading

0 comments on commit 7b0a7e7

Please sign in to comment.