Skip to content

Commit

Permalink
Merge pull request #12 from dung13890/generate_project
Browse files Browse the repository at this point in the history
Update setting config once
  • Loading branch information
dung13890 authored May 31, 2023
2 parents 13ca522 + 488bc27 commit c3172ec
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ USAGE:
go-base-gen [global options] command [command options] [arguments...]

VERSION:
v1.0.6
v1.0.8

COMMANDS:
project Generate base code for go project use clean architecture
Expand Down Expand Up @@ -95,14 +95,14 @@ make dev
│   ├── app
│   ├── constants
│   ├── domain
│   │   └── auth
│   │   ├── delivery
│   │   ├── repository
│   │   └── usecase
│   ├── impl
│   │   ├── pubsub
│   │   └── service
│   ├── modules
│   │   └── auth
│   │   │   ├── delivery
│   │   │   ├── repository
│   │   │   └── usecase
│   └── registry
└── pkg
```
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ services:
stdin_open: true
tty: true
volumes:
- ./:/var/www/app
working_dir: /var/www/app
- ./:/go/src/app
working_dir: /go/src/app
networks:
go-app-net:
driver: bridge
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM golang:1.20.3-alpine3.16 as base
RUN apk update && apk upgrade && \
apk add --no-cache gcc musl-dev make git bash openssh

WORKDIR /var/www/app
WORKDIR /go/src/app

COPY go.mod go.sum ./
RUN go mod download
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
version string = "v1.0.6"
version string = "v1.0.8"
)

func main() {
Expand Down
18 changes: 13 additions & 5 deletions template/tmpl/config/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ Mit License (MIT)
{{define "config/app.go"}}package config

import (
"sync"

"{{.Project}}/pkg/errors"
"{{.Project}}/pkg/logger"

"github.com/spf13/viper"
)

var (
once sync.Once
appConf AppConfig
)

// AppConfig App Common
type AppConfig struct {
App string `mapstructure:"APP_ENV"`
Expand All @@ -34,11 +41,12 @@ func LoadConfig() error {

// GetAppConfig Unmarshal App Config from env
func GetAppConfig() AppConfig {
c := AppConfig{}
if err := viper.Unmarshal(&c); err != nil {
logger.Error().Fatal(err)
}
once.Do(func() {
if err := viper.Unmarshal(&appConf); err != nil {
logger.Error().Fatal(err)
}
})

return c
return appConf
}
{{end}}
18 changes: 13 additions & 5 deletions template/tmpl/config/database.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ Mit License (MIT)
{{define "config/database.go"}}package config

import (
"sync"

"{{.Project}}/pkg/logger"

"github.com/spf13/viper"
)

var (
onceDB sync.Once
dbConf Database
)

// Database config struct
type Database struct {
Connection string `mapstructure:"DB_CONNECTION"`
Expand All @@ -25,11 +32,12 @@ type Database struct {

// GetDBConfig Unmarshal Database Config from env
func GetDBConfig() Database {
c := Database{}
if err := viper.Unmarshal(&c); err != nil {
logger.Error().Fatal(err)
}
onceDB.Do(func() {
if err := viper.Unmarshal(&dbConf); err != nil {
logger.Error().Fatal(err)
}
})

return c
return dbConf
}
{{end}}
19 changes: 14 additions & 5 deletions template/tmpl/config/redis.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ Mit License (MIT)
{{define "config/redis.go"}}package config

import (
"sync"

"{{.Project}}/pkg/logger"

"github.com/spf13/viper"
)

var (
onceRedis sync.Once
redisConf Redis
)

// Redis config struct
type Redis struct {
Host string `mapstructure:"REDIS_HOST"`
Expand All @@ -20,11 +27,13 @@ type Redis struct {

// GetRedisConfig Unmarshal Redis Config from env
func GetRedisConfig() Redis {
c := Redis{}
if err := viper.Unmarshal(&c); err != nil {
logger.Error().Fatal(err)
}
onceRedis.Do(func() {
if err := viper.Unmarshal(&redisConf); err != nil {
logger.Error().Fatal(err)
}
})

return c
return redisConf
}

{{end}}

0 comments on commit c3172ec

Please sign in to comment.