Go Echo Kit provides useful tools for go-echo development.
go get -u github.com/rakutentech/go-echo-kit
DB can be started like below
import "github.com/rakutentech/go-echo-kit/db"
m := db.New()
m.AddConnString(connectStringMaster) // Add single connect string
m.AddConnStrings(connectStringSlaves) // Add mutliple connect string
m.Open()
defer m.Close()
m.MasterConn().Create(&user) // Connect to master
m.SlaveConn().Find(&user) // Connect to slave
You can add mulitple connect strings, and the first one will be the master. SlaveConn will return one of slave randomly, and it can fail over to other slaves and master.
Regarding DB queries and how to generate connect string, please refer to GORM guide
This package also provides useful tool to build connection strings
import "github.com/rakutentech/go-echo-kit/db"
builder := db.ConnStringBuilder{}
connStr := builder.
SetFormat()
SetHost("YourHost").
SetUsername("YourUsername").
SetPort("YourPort").
SetPassword("YourPwd").
SetOptions(optionMap)
Build()
There is one more option to build string from config
import "github.com/rakutentech/go-echo-kit/config"
cfg := config.New()
connStr := builder.SetWithConfig(cfg.Sub("databases.master")).Build())
Check testdata/config.yaml to get more details
Variable name | Description | Default |
---|---|---|
DB_DRIVER | Db drivers which Gorm supports | mysql |
DB_CONN_MAX_LIFETIME | Maximum connection life time(seconds) | unlimited |
DB_MAX_IDLE_CONNS | Maximum number of idle connection | 2 |
DB_MAX_OPEN_CONNS | Maximum number of open connection | unlimited |
import "github.com/rakutentech/go-echo-kit/db"
var dbConfig[] db.MultiDbConf
db1Config := db.MultiDbConf {
Master: db1MasterDsn, // master db1 connection string
Slaves: db1SlaveDsns, // salve db1 connection strings
DbName: "db1",
}
db2Config := db.MultiDbConf {
Master: db2MasterDsn, // master db2 connection string
Slaves: db2SlaveDsns, // slave db2 connection strings
DbName: "db2",
}
dbConfig = append(dbConfig, db1Config, db2Config)
dbConn = db.OpenDBConn(dbConfig)
defer db.CloseDBConn(dbConn)
import "github.com/rakutentech/go-echo-kit/db"
query := db.GetInstance("db1", db.ConnTypeSlave).
Select("user_id, name, email, created_at").
Table("user").
Limit(2)
Configuration can be started like below
import "github.com/rakutentech/go-echo-kit/config"
cfg := config.New()
secret := cfg.GetString("app.secret")
Default config file type is yaml
, but can be changed whatever viper supports.
app:
env: stg
secret: ${APP_SECRET}
For ${APP_SECRET}, go-echo-kit will automatically find .env
(dotenv) file from CONFIG_PATH
or find it from environment variable.
APP_SECRET=secret
DB_DRIVER=sqlite3
DB_PORT=4306
Logger can be started like below
import "github.com/rakutentech/go-echo-kit/logger"
logger.SetLogFile(filepath) // for simple use
logger.Notice("Your notice")
logger.Error("Your error")
logger.Crit("Your critical error that app should stop")
logger.SetRotatingLogFile(filepathPattern, options...) // For log rotation
Check file-rotatelogs to get information about log rotation
# For test
cp config/.env.testing config/.env
go test ./...
# For build/run
cp config/.env.stg config/.env
go run main.go
You can create seprate dotenv files and replace them when you want.
Variable name | Description | Default |
---|---|---|
CONFIG_TYPE | Config file type viper supports | Yaml |
CONFIG_PATH | Where config file exists | ./config(for local) |
For more information about configuration, please refer to Viper