mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-14 16:37:56 -05:00
Co-authored-by: colinlyguo <colinlyguo@gmail.com> Co-authored-by: maskpp <maskpp266@gmail.com> Co-authored-by: Péter Garamvölgyi <peter@scroll.io> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: Lawliet-Chan <1576710154@qq.com> Co-authored-by: HAOYUatHZ <haoyu@protonmail.com>
40 lines
788 B
Go
40 lines
788 B
Go
package database
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"scroll-tech/common/utils"
|
|
)
|
|
|
|
// DBConfig db config
|
|
type DBConfig struct {
|
|
// data source name
|
|
DSN string `json:"dsn"`
|
|
DriverName string `json:"driver_name"`
|
|
|
|
MaxOpenNum int `json:"maxOpenNum" default:"200"`
|
|
MaxIdleNum int `json:"maxIdleNum" default:"20"`
|
|
}
|
|
|
|
// NewConfig returns a new instance of Config.
|
|
func NewConfig(file string) (*DBConfig, error) {
|
|
buf, err := os.ReadFile(filepath.Clean(file))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := &DBConfig{}
|
|
err = json.Unmarshal(buf, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// cover value by env fields
|
|
cfg.DSN = utils.GetEnvWithDefault("DB_DSN", cfg.DSN)
|
|
cfg.DriverName = utils.GetEnvWithDefault("DB_DRIVER", cfg.DriverName)
|
|
|
|
return cfg, nil
|
|
}
|