Skip to content

Commit

Permalink
refactor code && make code readable
Browse files Browse the repository at this point in the history
  • Loading branch information
fanchann committed Aug 26, 2023
1 parent 7d19bfd commit e2b9edc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
17 changes: 13 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func GoStarter(app, extension, driver, host string, port int, username, password
"app/routers",
"app/middlewares",
"app/services",
"lib",
"utils",
"config",
}
Expand All @@ -45,18 +44,21 @@ func GoStarter(app, extension, driver, host string, port int, username, password

appStructure := []AppStructure{
{Path: "cmd/", FileName: "main.go", Code: writeFileCodeSelection(extension, code.MainCodeEnvConfig, code.MainCode)},
{Path: "config/", FileName: namingFileSelection(extension), Code: writeFileCodeSelection(extension, code.WriteAppConfiguration("env", host, driver, username, password, dbname, port), code.LoadConfigCode)},
{Path: "lib/", FileName: fmt.Sprintf("%s.go", driver), Code: writeFileCodeSelection(extension, code.DBLibWithEnvSetting, code.DBLib)},
{Path: "config/", FileName: fmt.Sprintf("%s.go", driver), Code: writeFileCodeSelection(extension, code.DBConfigWithEnvSetting, code.DBLib)},
{Path: pathSelection(extension), FileName: namingFileSelection(extension), Code: writeFileCodeSelection(extension, code.WriteAppConfiguration("env", host, driver, username, password, dbname, port), code.LoadConfigCode)},
{Path: "/", FileName: "go.mod", Code: writeFileCodeSelection(extension, code.GoModEnv, code.GoMod)},
{Path: "/", FileName: "docker-compose.yaml", Code: code.GenerateDockerCompose(driver, username, password, dbname, strconv.Itoa(port))},
}

if extension != "env" {
if extension == "env" {
appStructure = append(appStructure, AppStructure{Path: "config/", FileName: "config.go", Code: writeFileCodeSelection(extension, code.ConfigCodeEnv, "")})
} else {
appStructure = append(appStructure, AppStructure{
Path: "/",
FileName: fmt.Sprintf("config.%s", extension),
Code: code.WriteAppConfiguration(extension, host, driver, username, password, dbname, port),
})

}

for _, structure := range appStructure {
Expand All @@ -83,3 +85,10 @@ func namingFileSelection(extension string) string {
}
return fmt.Sprintf("%s_reader.go", extension)
}

func pathSelection(extension string) string {
if extension != "env" {
return "config/"
}
return "/"
}
34 changes: 30 additions & 4 deletions common/code/config_reader_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"{{.Package}}/lib"
)
func InitDatabase() *lib.DB {
return &lib.DB{
func InitDatabase() *DB {
return &DB{
Driver: GetString("database.driver"),
Host: GetString("database.host"),
Username: GetString("database.username"),
Expand Down Expand Up @@ -57,7 +56,7 @@ func (c *Configuration) Initialize() error {
return err
}
var dbStructure lib.DB
var dbStructure DB
err = viper.Unmarshal(&dbStructure, func(c *mapstructure.DecoderConfig) {
c.TagName = "mapstructure"
})
Expand Down Expand Up @@ -94,3 +93,30 @@ func GetBool(key string) bool {
return viper.GetBool(key)
}
`
var ConfigCodeEnv = `package config
import (
"os"
"github.com/joho/godotenv"
)
type Config interface {
Get(key string) string
}
type configImpl struct {
}
func (config *configImpl) Get(key string) string {
return os.Getenv(key)
}
func New(filenames ...string) Config {
err := godotenv.Load(filenames...)
if err != nil {
panic(err)
}
return &configImpl{}
}
`
22 changes: 5 additions & 17 deletions common/code/db_code.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package code

var DBLib = `package lib
var DBLib = `package config
import (
"fmt"
Expand Down Expand Up @@ -51,15 +51,13 @@ func (d *DB) DatabaseConnection() *gorm.DB {
}
`

var DBLibWithEnvSetting = `package lib
var DBConfigWithEnvSetting = `package config
import (
"fmt"
"log"
"os"
"path"
"github.com/joho/godotenv"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand All @@ -70,22 +68,16 @@ var (
POSTGRES_CONFIG = "host=%s user=%s password=%s dbname=%s port=%v sslmode=%s TimeZone=Asia/Shanghai"
)
func DatabaseConnection(configFile string) *gorm.DB {
func DatabaseConnection(configuration Config) *gorm.DB {
var dialect gorm.Dialector
var dbConnection string
filePath := path.Join("config", configFile)
if err := godotenv.Load(filePath); err != nil {
log.Fatalf("error while read configuration, %s", err.Error())
}
switch os.Getenv("db_driver") {
case "postgres":
dbConnection = fmt.Sprintf(POSTGRES_CONFIG, readEnvironment("db_host"), readEnvironment("db_username"), readEnvironment("db_password"), readEnvironment("db_name"), readEnvironment("db_port"), readEnvironment("db_sslmode"))
dbConnection = fmt.Sprintf(POSTGRES_CONFIG, configuration.Get("db_host"), configuration.Get("db_username"), configuration.Get("db_password"), configuration.Get("db_name"), configuration.Get("db_port"), configuration.Get("db_sslmode"))
dialect = postgres.Open(dbConnection)
case "mysql":
dbConnection = fmt.Sprintf(MYSQL_CONFIG, readEnvironment("db_username"), readEnvironment("db_password"), readEnvironment("db_host"), readEnvironment("db_port"), readEnvironment("db_name"))
dbConnection = fmt.Sprintf(MYSQL_CONFIG, configuration.Get("db_username"), configuration.Get("db_password"), configuration.Get("db_host"), configuration.Get("db_port"), configuration.Get("db_name"))
dialect = mysql.Open(dbConnection)
}
Expand All @@ -98,8 +90,4 @@ func DatabaseConnection(configFile string) *gorm.DB {
return db
}
func readEnvironment(key string) string {
return os.Getenv(key)
}
`
5 changes: 3 additions & 2 deletions common/code/main_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"log"
"os"
"{{.Package}}/lib"
"{{.Package}}/config"
)
var fileConfiguration *string
Expand All @@ -64,8 +64,9 @@ func init() {
}
func main() {
dbConnection := lib.DatabaseConnection(*fileConfiguration)
configuration := config.New(*fileConfiguration)
dbConnection := config.DatabaseConnection(configuration)
// ping to database
responseDB, _ := dbConnection.DB()
if err := responseDB.Ping(); err != nil {
Expand Down

0 comments on commit e2b9edc

Please sign in to comment.