-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from andhikasamudra/feat/v2-update
feat: add some new utilities such as auth, grpc and other
- Loading branch information
Showing
66 changed files
with
2,506 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.