Skip to content

Commit

Permalink
initial commmit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainal21 committed Jan 9, 2024
0 parents commit aedfd65
Show file tree
Hide file tree
Showing 106 changed files with 8,895 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
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"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true

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

[log]
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
17 changes: 17 additions & 0 deletions .build/go-bone.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21 AS builder

WORKDIR /build

COPY . .

RUN go mod tidy && go mod download && go mod vendor

ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN go build -ldflags="-s -w" -o skeleton .

FROM scratch

COPY --from=builder /build/skeleton /
COPY --from=builder /build/config /config

EXPOSE 3001
36 changes: 36 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
APP_NAME=neo-dtb-skeleton
APP_ENV=development
APP_HOST=localhost
APP_PORT=3001

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

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
DB_USER=root
DB_PASSWORD=securepassword
DB_MAX_OPEN_CONN=100
DB_MAX_IDLE_CONN=10
DB_CONN_LIFETIME=1
DB_IDLE_TIME=1
DB_TLS=false
DB_CA_CERT=
DB_CLIENT_CERT=
DB_CLIENT_KEY=

RABBITMQ_HOST=
RABBITMQ_PORT=
RABBITMQ_USER=
RABBITMQ_PASS=
RABBITMQ_TLS=
RABBITMQ_CA_CERT=
RABBITMQ_CLIENT_CERT=
RABBITMQ_CLIENT_KEY=
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dugong
bin
.env
.idea
.DS_Store
vendor
go.sum
.vscode
docs/
storage/
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BINARY_NAME=go-bone-boilerplate
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...."
@rm -rf vendor
@rm -f Gopkg.lock
@rm -f glide.lock
@go mod tidy && go mod download && go mod vendor

start-http:
@go run main.go http

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

migrate:
@go run main.go db:migrate
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Go Bone Boilerplate

## Getting started

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

# example
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}
```
73 changes: 73 additions & 0 deletions cmd/broker/rabbitmq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package broker

import (
"flag"
"fmt"
"os"
"strings"

"github.com/Zainal21/go-bone/internal/bootstrap"
"github.com/Zainal21/go-bone/internal/controller"
"github.com/Zainal21/go-bone/pkg/config"
"github.com/Zainal21/go-bone/pkg/logger"
)

var (
flags = flag.NewFlagSet("rabbit", flag.ExitOnError)
name = flags.String("name", "", "queue and exchange name")
topics = flags.String("topics", "", "topic to subscribes")
help = flags.Bool("guide", false, "Print Help")
)

func ServeRabbitMQ() {
flags.Usage = usage
_ = flags.Parse(os.Args[2:])

args := flags.Args()

if (len(args) == 0 && (*name == "" || *topics == "")) || *help {
flags.Usage()
return
}

var topicList []string
for _, t := range strings.Split(*topics, "|") {
if t != "" {
topicList = append(topicList, t)
}
}

logger.SetJSONFormatter()
cnf, err := config.LoadAllConfigs()
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to load configuration file: %v", err))
}
mController := controller.NewLogController()

subs := bootstrap.RegistryRabbitMQSubscriber(*name, cnf, mController)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed connect to RabbitMQ Cause: %v", err))
}

err = subs.Listen(topicList)

if err != nil {
logger.Fatal(fmt.Sprintf("Failed to Listen Topic Cause: %v", err))
}
}

func usage() {
fmt.Println(usageCommands)
}

var (
usageCommands = `
Usage:
go run main.go rabbit [flags]
Flags:
--name string name of queue and exchange
--topics string separate with pipeline "|" if want multiple bindings
--guide print help
`
)
24 changes: 24 additions & 0 deletions cmd/http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package http

import (
"fmt"

"github.com/Zainal21/go-bone/pkg/app"
"github.com/Zainal21/go-bone/pkg/config"
"github.com/Zainal21/go-bone/pkg/logger"
)

func Start() {
logger.SetJSONFormatter()
cnf, err := config.LoadAllConfigs()
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to load configuration file: %v", err))
}

app.InitializeApp(cnf)
application := app.GetServer()

if err := application.StartServer(); err != nil {
logger.Fatal(fmt.Sprintf("Failed to start server: %v", err))
}
}
49 changes: 49 additions & 0 deletions cmd/migration/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package migration

import (
"flag"
"fmt"
"log"
"os"

"github.com/Zainal21/go-bone/database/seeders"
"github.com/Zainal21/go-bone/pkg/database/mysql"

"github.com/Zainal21/go-bone/pkg/config"
"github.com/Zainal21/go-bone/pkg/logger"
)

func MigrateDatabase() {
cfg, err := config.LoadAllConfigs()

if err != nil {
logger.Fatal(fmt.Sprintf("Failed to load configuration file: %v", err))
}

mysql.DatabaseMigration(cfg)
}


func SeedDatabase() {
cfg, err := config.LoadAllConfigs()

if err != nil {
logger.Fatal(fmt.Sprintf("Failed to load configuration file: %v", err))
}
flag.Parse()
args := flag.Args()
if len(args) >= 1 {
log.Println(args[0])
switch args[0] {
case "db:seed":
db, err := mysql.ConnectDatabase(cfg)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to load configuration file: %v", err))
}

seeders.Execute(db, args[1:]...)
os.Exit(0)
}
}
}

Loading

0 comments on commit aedfd65

Please sign in to comment.