Skip to content

Commit

Permalink
✨ toggles for DB conenctions
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 26, 2022
1 parent 2f731ae commit 258c998
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ APP_ENV=dev
APP_DEBUG=true
WEB_URL=http://localhost:3000

DB_CONNECT=true
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=tonic
DB_USERNAME=root
DB_PASSWORD=
DB_LOGGING=true

OS_CONNECT=true
OS_HOST=http://localhost:9200
OS_USERNAME=admin
OS_PASSWORD=admin
OS_PASSWORD=admin
45 changes: 25 additions & 20 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,38 @@ func DURL() string {
func Setup() {
var err error
var logMode = logger.Error

if setting.Database.Logging == "true" {
logMode = logger.Info
}
Db, err = gorm.Open(
mysql.New(mysql.Config{
DSN: DSN()},
),
&gorm.Config{
Logger: logger.Default.LogMode(logMode),
},
)

if err != nil {
log.Fatalf("gorm.DB err: %v", err)
if setting.Database.Connect == "true" {
Db, err = gorm.Open(
mysql.New(mysql.Config{
DSN: DSN()},
),
&gorm.Config{
Logger: logger.Default.LogMode(logMode),
},
)
if err != nil {
log.Fatalf("gorm.DB err: %v", err)
}
}

Os, err = opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{setting.Opensearch.Address},
Username: setting.Opensearch.Username,
Password: setting.Opensearch.Password,
})
if setting.Opensearch.Connect == "true" {
Os, err = opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{setting.Opensearch.Address},
Username: setting.Opensearch.Username,
Password: setting.Opensearch.Password,
})

if err != nil {
log.Fatalf("opensearch.NewClient err: %v", err)
if err != nil {
log.Fatalf("opensearch.NewClient err: %v", err)
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Tonic is a web application framework that supplies tools and libraries on top of

## Getting Started

### environment variables

Take a look at [.env.example](https://github.com/fumeapp/tonic/blob/main/.env.example) for an example of how to set up environment variables. Copy it to `.env` and modify them to your needs
* Any ENV variable you do not set will default to what is in `.env.example`
* Make sure to set DB_CONNECT=true if you want to use the MySQL database
* Make sure to set OS_CONNECT=true if you want to use the Opensearch database


### Endpoint Configuration
Define an endpoint that you can call locally and remotely.
* In this example we bind `/` to a standard JSON response
Expand Down
4 changes: 2 additions & 2 deletions setting/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package setting

type DatabaseSetting struct {
Driver string
Connect string
Host string
Database string
Username string
Expand All @@ -14,8 +14,8 @@ var Database = &DatabaseSetting{}

func DatabaseSetup() *DatabaseSetting {

Database.Connect = env("DB_CONNECT", "false")
Database.Logging = env("DB_LOGGING", "false")
Database.Driver = env("DB_DRIVER", "mysql")
Database.Host = env("DB_HOST", "localhost")
Database.Database = env("DB_DATABASE", "tonic")
Database.Username = env("DB_USERNAME", "root")
Expand Down
2 changes: 2 additions & 0 deletions setting/opensearch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package setting

type OpensearchSetting struct {
Connect string
Address string
Username string
Password string
Expand All @@ -10,6 +11,7 @@ var Opensearch = &OpensearchSetting{}

func OpensearchSetup() *OpensearchSetting {

Opensearch.Connect = env("OS_CONNECT", "false")
Opensearch.Address = env("OS_HOST", "http://localhost:9200")
Opensearch.Username = env("OS_USERNAME", "admin")
Opensearch.Password = env("OS_PASSWORD", "admin")
Expand Down
7 changes: 5 additions & 2 deletions setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ func env(key string, config string) string {
}

func Setup() (*CoreSetting, *DatabaseSetting, *OpensearchSetting) {
if err := godotenv.Load(); err != nil {
}
_ = godotenv.Load()
return CoreSetup(), DatabaseSetup(), OpensearchSetup()
}

Expand All @@ -25,3 +24,7 @@ func IsDev() bool {
func IsDebug() bool {
return env("APP_DEBUG", "false") == "true"
}

func GetWebUrl() string {
return env("WEB_URL", "http://localhost:3000")
}

0 comments on commit 258c998

Please sign in to comment.