Skip to content

Commit

Permalink
Merge pull request akmamun#6 from akmamun/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
akmamun authored Mar 10, 2022
2 parents c6295aa + c28eb8f commit 3eb0511
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Working directory
# . or absolute path, please note that the directories following must be under root
root = "."
tmp_dir = "tmp"
tmp_dir = "/tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/app/engine main.go"
# Binary file yields from `cmd`.
bin = "tmp/app"
bin = "/tmp/app"

# Customize binary.
# This is how you start to run your application. Since my application will works like CLI, so to run it, like to make a CLI call.
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Gin Gorm Boilerplate
An API boilerplate written in Golang with Gin Framework


### Docker Development with Live Reload
- run `make dev`

### Local Installation
- configuration manage from [config.yml](config.yml) file
### Local Instruction
Follow these steps:
- Configuration manage from [config.yml](config.yml) file
- To add all dependencies for a package in your module `go get .` in the current directory
- Locally run `go run main.go` or `go build main.go` and run `./main`
- The application should be available and running on [0.0.0.0:8000](http://0.0.0.0:8000)
Expand All @@ -16,5 +18,5 @@ An API boilerplate written in Golang with Gin Framework
### Under the hood
- [Viper](https://github.com/spf13/viper) - Go configuration with fangs.
- [Gorm](https://github.com/go-gorm/gorm) - The fantastic ORM library for Golang
- [Logger](github.com/sirupsen/logrus) - Structured, pluggable logging for Go.
- [Logger](https://github.com/sirupsen/logrus) - Structured, pluggable logging for Go.
- [Air](https://github.com/cosmtrek/air) - Live reload for Go apps (Docker Development)
6 changes: 3 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ database:
dbname: "test_pg_go"
username: "mamun"
password: "123"
host: "postgres_db"
host: "postgres_db" # "0.0.0.0" for local dev
port: "5432"
logmode: true
log_mode: true

server:
host: "0.0.0.0"
port: "8000"
secret: "mySecretKey"
secret: "secret"
environment: "dev" #prod
request:
timeout: 100
Expand Down
20 changes: 20 additions & 0 deletions src/config/db.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

import (
"fmt"
"github.com/spf13/viper"
)

type DatabaseConfiguration struct {
Driver string
Dbname string
Expand All @@ -9,3 +14,18 @@ type DatabaseConfiguration struct {
Port string
LogMode bool
}

func DbConfiguration() string {
dbname := viper.GetString("database.dbname")
username := viper.GetString("database.username")
password := viper.GetString("database.password")
host := viper.GetString("database.host")
port := viper.GetString("database.port")
sslMode := viper.GetString("database.sslmode")

dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
host, username, password, dbname, port, sslMode,
)
return dsn
}
28 changes: 6 additions & 22 deletions src/database/database.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package database

import (
"fmt"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"pkg/src/config"
)

var (
Expand All @@ -19,29 +19,13 @@ type Database struct {
*gorm.DB
}

func configuration() string {
dbname := viper.GetString("database.dbname")
username := viper.GetString("database.username")
password := viper.GetString("database.password")
host := viper.GetString("database.host")
port := viper.GetString("database.port")
sslMode := viper.GetString("database.sslmode")

dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
host, username, password, dbname, port, sslMode,
)
return dsn
}

// Connection create database connection
func Connection() error {
var db = DB
dsn := configuration()
dsn := config.DbConfiguration()

logMode := viper.GetBool("database.logmode")
logMode := viper.GetBool("database.log_mode")
loglevel := logger.Silent

if logMode {
loglevel = logger.Info
}
Expand All @@ -52,7 +36,7 @@ func Connection() error {

if err != nil {
DBErr = err
log.Println("DbConfiguration connection error")
log.Println("Db connection error")
return err
}

Expand All @@ -72,7 +56,7 @@ func GetDB() *gorm.DB {
return DB
}

// GetDBErr connection error
func GetDBErr() error {
// GetDBError connection error
func GetDBError() error {
return DBErr
}

0 comments on commit 3eb0511

Please sign in to comment.