Skip to content

Commit

Permalink
refactor: code restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainal21 committed Mar 20, 2024
1 parent 8af2960 commit 9d7f9f8
Show file tree
Hide file tree
Showing 101 changed files with 2,017 additions and 5,085 deletions.
37 changes: 0 additions & 37 deletions .air.toml

This file was deleted.

File renamed without changes.
43 changes: 31 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Application
APP_NAME=go-bone
APP_NAME=neo-dtb-skeleton
APP_ENV=development
APP_HOST=localhost
APP_PORT=3001

# Logger
APP_LOGGER_DEBUG=true
APP_LOGGER_LEVEL=info #debug | info | warn | error | fatal | panic

# Database
APP_OTEL_TRACE=true
APP_OTEL_EXPORTER=tempo

JAEGER_HOST=
TEMPO_HOST=0.0.0.0:4318

DB_HOST=localhost
DB_PORT=3307
DB_NAME=skeleton_db
Expand All @@ -23,12 +26,28 @@ DB_CA_CERT=
DB_CLIENT_CERT=
DB_CLIENT_KEY=

# Google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URL=
MONGODB_HOST=localhost
MONGODB_PORT=3307
MONGODB_NAME=skeleton_db
MONGODB_USER=root
MONGODB_PASSWORD=securepassword
MONGODB_MAX_OPEN_CONN=100
MONGODB_MAX_IDLE_CONN=10
MONGODB_CONN_LIFETIME=1
MONGODB_IDLE_TIME=1
MONGODB_TLS=false
MONGODB_CA_CERT=
MONGODB_CLIENT_CERT=
MONGODB_CLIENT_KEY=

RABBITMQ_HOST=
RABBITMQ_PORT=
RABBITMQ_USER=
RABBITMQ_PASS=
RABBITMQ_TLS=
RABBITMQ_CA_CERT=
RABBITMQ_CLIENT_CERT=
RABBITMQ_CLIENT_KEY=

# Redis
REDIS_HOST=localhost:6379
REDIS_PASSWORD=
REDIS_DB=0
PUBSUB_ACCOUNT_PATH=external/credentials/service_account.json
PROJECT_ID=
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
BINARY_NAME=go-bone-boilerplate
BINARY_NAME=skeleton
build:
@go build -o bin/${BINARY_NAME} main.go

run-http:
@./bin/${BINARY_NAME} http

run-rabbit:
@./bin/${BINARY_NAME} http

install:
@echo "Installing dependencies...."
Expand All @@ -15,12 +18,7 @@ install:
start-http:
@go run main.go http


start-rabbit-mq:
@go run main.go rabbit


start-rabbit-mq:
start-rabbit:
@go run main.go rabbit

migrate:
Expand Down
31 changes: 3 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Go Bone Boilerplate
# Neo Boilerplate

## Getting started

Expand All @@ -7,77 +7,65 @@ This is built on top of [Go Fiber](https://docs.gofiber.io) Golang Framework.
## Dependencies

There is some dependencies that we used in this skeleton:

- [Go Fiber](https://docs.gofiber.io/) [Go Framework]
- [Viper](https://github.com/spf13/viper) [Go Configuration]
- [Cobra](https://github.com/spf13/cobra) [Go Modern CLI]
- [Logrus Logger](https://github.com/sirupsen/logrus) [Go Logger]
- [Goose Migration](https://github.com/pressly/goose) [Go Migration]
- [Gobreaker](https://github.com/sony/gobreaker) [Go Circuit Breaker]
- [OpenTelemetry](https://pkg.go.dev/go.opentelemetry.io/otel) [OpenTelemetry Tracer]

## Requirement

- Golang version 1.21 or latest
- Database MySQL
- RabbitMQ

## Usage

### Installation

install required dependencies

```bash
make install
```

### Run Service

run current service after all dependencies installed

```bash
make start
```

## Database Migration

migration up

```bash
go run main.go db:migrate up
```

migration down

```bash
go run main.go db:migrate down
```

migration reset

```bash
go run main.go db:migrate reset
```

migration reset

```bash
go run main.go db:migrate reset
```

migration redo

```bash
go run main.go db:migrate redo
```

migration status

```bash
go run main.go db:migrate status
```

create migration table

```bash
go run main.go db:migrate create {table-name} sql

Expand All @@ -86,19 +74,6 @@ go run main.go db:migrate create users sql
```

to show all command

```bash
go run main.go db:migrate
```

run seeder

```bash
go run main.go db:seed

# example
go run main.go db:seed

# example spesific function
go run main.go db:seed {func-seeder-name}
```
30 changes: 23 additions & 7 deletions app/appctx/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package appctx

import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"sync"
"time"

"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

var (
rsp *Response
oneRsp sync.Once
)

type ErrorResp struct {
Key string `json:"key,omitempty"`
Messages []string `json:"messages,omitempty"`
}

// Response presentation contract object
type Response struct {
Code int `json:"-"`
Expand All @@ -24,11 +30,20 @@ type Response struct {
Meta interface{} `json:"meta,omitempty"`
Message interface{} `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
Errors interface{} `json:"errors,omitempty"`
Errors []ErrorResp `json:"errors,omitempty"`
lang string `json:"-"`
msgKey string
}

// MetaData represent meta data response for multi data
type MetaData struct {
TransactionID uuid.UUID `json:"transaction_id,omitempty"`
Page uint64 `json:"page,omitempty"`
Limit uint64 `json:"limit,omitempty"`
TotalPage uint64 `json:"total_page,omitempty"`
TotalCount uint64 `json:"total_count,omitempty"`
}

// WithCode setter response var name
func (r *Response) WithCode(c int) *Response {
r.Status = true
Expand All @@ -40,11 +55,6 @@ func (r *Response) WithCode(c int) *Response {
return r
}

func (r *Response) WithError(v interface{}) *Response {
r.Errors = v
return r
}

// WithEntity setter entity response
func (r *Response) WithEntity(e string) *Response {
r.Entity = e
Expand All @@ -63,6 +73,12 @@ func (r *Response) WithData(v interface{}) *Response {
return r
}

// WithError setter error messages
func (r *Response) WithError(v []ErrorResp) *Response {
r.Errors = v
return r
}

func (r *Response) WithMsgKey(v string) *Response {
r.msgKey = v
return r
Expand Down
10 changes: 10 additions & 0 deletions app/bootstrap/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/Zainal21/go-bone/pkg/config"
"github.com/Zainal21/go-bone/pkg/database/mongodb"
"github.com/Zainal21/go-bone/pkg/database/mysql"
"github.com/Zainal21/go-bone/pkg/logger"
)
Expand All @@ -17,3 +18,12 @@ func RegistryDatabase(cfg *config.Config) *mysql.DB {

return mysql.New(db, false, cfg.DatabaseConfig.DBName)
}

func RegistryMongoDB(cfg *config.Config) *mongodb.DB {
db, err := mongodb.Connect(cfg)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to connect to database: %v", err))
}

return mongodb.New(db, false, cfg.DatabaseConfig.DBName)
}
62 changes: 62 additions & 0 deletions app/bootstrap/open_telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package bootstrap

import (
"context"

"github.com/Zainal21/go-bone/pkg/tracer"

"github.com/Zainal21/go-bone/pkg/config"
"github.com/Zainal21/go-bone/pkg/util"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
)

type Provider struct {
provider trace.TracerProvider
}

// NewExporter returns a new `Provider` type. It uses Jaeger exporter and globally sets
// the tracer provider as well as the global tracer for spans.
func RegistryOpenTelemetry(cfg *config.Config) (Provider, error) {
if !cfg.AppOtelTrace {
return Provider{
provider: trace.NewNoopTracerProvider(),
}, nil
}

var exp sdktrace.SpanExporter

exp, err := tracer.NewExporter(cfg)
if err != nil {
return Provider{}, err
}
tp := sdktrace.NewTracerProvider(
// Always be sure to batch in production.
sdktrace.WithBatcher(exp),
// Record information about this application in a Resource.
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.AppName),
attribute.String("environment", util.EnvironmentTransform(cfg.AppEnv)),
// attribute.Int64("ID", id),
)),
)
otel.SetTracerProvider(tp)
return Provider{
provider: tp,
}, nil
}

// Close shuts down the tracer provider only if it was not "no operations"
// version.
func (p Provider) Close() error {
if prv, ok := p.provider.(*sdktrace.TracerProvider); ok {
return prv.Shutdown(context.TODO())
}

return nil
}
Loading

0 comments on commit 9d7f9f8

Please sign in to comment.