-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from kcmvp/v013
#10: apply cobra template for pretty print
- Loading branch information
Showing
56 changed files
with
1,692 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,5 @@ | |
go.work | ||
.idea | ||
gob | ||
*.sql | ||
11.json | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# "postgres://username:password@localhost:5432/database_name" | ||
# username:password@protocol(address)/dbname?param=value | ||
datasource: | ||
ds1: | ||
driver: sqlite3 | ||
user: usera | ||
password: passwd1 | ||
Host: localhost | ||
url: file:test1.db?cache=shared&mode=memory | ||
ds2: | ||
driver: sqlite3 | ||
user: userb | ||
password: passwd2 | ||
url: file:test2.db?cache=shared&mode=memory | ||
scripts: | ||
- sqlite3-schema.sql | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# "postgres://username:password@localhost:5432/database_name" | ||
# username:password@protocol(address)/dbname?param=value | ||
datasource: | ||
ds1: | ||
driver: sqlite3 | ||
user: abc | ||
password: 123 | ||
url: file:test3.db?cache=shared&mode=memory | ||
scripts: | ||
- sqlite3-schema.sql | ||
- sqlite3-init.sql | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package boot | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/kcmvp/gob/internal" | ||
"github.com/kcmvp/gob/utils" | ||
|
||
"github.com/samber/do/v2" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
DefaultCfg = "application" | ||
) | ||
|
||
var ( | ||
cfg *viper.Viper | ||
once sync.Once | ||
) | ||
|
||
func RootDir() string { | ||
return internal.RootDir | ||
} | ||
|
||
func Container() *do.RootScope { | ||
return internal.Container | ||
} | ||
|
||
func InitApp() { | ||
InitAppWith(DefaultCfg) | ||
} | ||
|
||
func InitAppWith(cfgName string) { | ||
if cfg == nil { | ||
once.Do(func() { | ||
cfg = viper.New() | ||
cfg.SetConfigName(cfgName) // name of cfg file (without extension) | ||
cfg.SetConfigType("yaml") // REQUIRED if the cfg file does not have the extension in the name | ||
cfg.AddConfigPath(internal.RootDir) // optionally look for cfg in the working directory | ||
if err := cfg.ReadInConfig(); err != nil { // Find and read the cfg file | ||
panic(fmt.Errorf("fatal error cfg file: %w", err)) | ||
} | ||
if test, _ := utils.TestCaller(); test { | ||
if testCfg, err := os.Open(filepath.Join(internal.RootDir, fmt.Sprintf("%s_test.yaml", cfgName))); err == nil { | ||
if err = cfg.MergeConfig(testCfg); err != nil { | ||
panic(fmt.Errorf("failed to merge test configuration file: %w", err)) | ||
} | ||
} | ||
} | ||
setupDb() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package boot | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/kcmvp/gob/internal" | ||
|
||
//"github.com/kcmvp/gob/internal" | ||
|
||
"github.com/samber/do/v2" | ||
typetostring "github.com/samber/go-type-to-string" | ||
"github.com/samber/lo" | ||
) | ||
|
||
const ( | ||
DSKey = "datasource" | ||
UserKey = "${user}" | ||
PasswordKey = "${password}" | ||
HostKey = "${host}" | ||
DefaultDS = "DefaultDS" | ||
) | ||
|
||
type dataSource struct { | ||
Driver string `mapstructure:"driver"` | ||
User string `mapstructure:"user"` | ||
Password string `mapstructure:"password"` | ||
Host string `mapstructure:"host"` | ||
URL string `mapstructure:"url"` | ||
Scripts []string `mapstructure:"scripts"` | ||
} | ||
|
||
func (ds dataSource) DSN() string { | ||
dsn := strings.ReplaceAll(ds.URL, UserKey, ds.User) | ||
dsn = strings.ReplaceAll(dsn, PasswordKey, ds.Password) | ||
return strings.ReplaceAll(dsn, HostKey, ds.Host) | ||
} | ||
|
||
func dsMap() map[string]dataSource { | ||
// single data source | ||
if v := cfg.Get(fmt.Sprintf("%s.%s", DSKey, "driver")); v != nil { | ||
var ds dataSource | ||
if err := cfg.UnmarshalKey(DSKey, &ds); err != nil { | ||
panic(fmt.Errorf("failed parse datasource: %w", err)) | ||
} | ||
return map[string]dataSource{DefaultDS: ds} | ||
// multiple data sources | ||
} else if v = cfg.Get(DSKey); v != nil { | ||
dss := v.(map[string]any) | ||
return lo.MapValues(dss, func(_ any, key string) dataSource { | ||
var ds dataSource | ||
key = fmt.Sprintf("%s.%s", DSKey, key) | ||
if err := cfg.UnmarshalKey(key, &ds); err != nil { | ||
panic(fmt.Errorf("failed parse datasource: %w", err)) | ||
} | ||
return ds | ||
}) | ||
} | ||
return map[string]dataSource{} | ||
} | ||
|
||
func setupDb() { | ||
for name, ds := range dsMap() { | ||
if db, err := sql.Open(ds.Driver, ds.DSN()); err == nil { | ||
if err = db.Ping(); err != nil { | ||
_ = db.Close() | ||
panic(fmt.Errorf("failed to initialize %s: %w", name, err)) | ||
} | ||
lo.ForEach(ds.Scripts, func(script string, _ int) { | ||
if data, err := os.ReadFile(filepath.Join(internal.RootDir, script)); err == nil { | ||
if _, err = db.Exec(string(data)); err != nil { | ||
panic(fmt.Errorf("failed to execute %s: %w", script, err)) | ||
} | ||
} else { | ||
panic(fmt.Errorf("failed to read %s: %w", script, err)) | ||
} | ||
}) | ||
do.ProvideNamedValue[*sql.DB](internal.Container, fmt.Sprintf("%s_%s", name, typetostring.GetType[*sql.DB]()), db) | ||
} else { | ||
panic(fmt.Errorf("failed to connect to datasource %s: %w", name, err)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package boot | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/kcmvp/gob/internal" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/samber/do/v2" | ||
typetostring "github.com/samber/go-type-to-string" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"testing" | ||
) | ||
|
||
type DBTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestDBTestSuite(t *testing.T) { | ||
suite.Run(t, &DBTestSuite{}) | ||
} | ||
|
||
func (dbs *DBTestSuite) SetupSuite() { | ||
InitApp() | ||
} | ||
|
||
func (dbs *DBTestSuite) TestMultipleDB() { | ||
ds := dsMap() | ||
assert.Equal(dbs.T(), 2, len(ds)) | ||
db := do.MustInvokeNamed[*sql.DB](internal.Container, fmt.Sprintf("%s_%s", "ds1", typetostring.GetType[*sql.DB]())) | ||
assert.NotNil(dbs.T(), db) | ||
rs, err := db.Exec("select * from Product") | ||
assert.NoError(dbs.T(), err) | ||
cnt, _ := rs.RowsAffected() | ||
assert.Equal(dbs.T(), int64(2), cnt) | ||
db = do.MustInvokeNamed[*sql.DB](internal.Container, fmt.Sprintf("%s_%s", "ds2", typetostring.GetType[*sql.DB]())) | ||
assert.NotNil(dbs.T(), db) | ||
rs, err = db.Exec("select * from Product") | ||
assert.NoError(dbs.T(), err) | ||
cnt, _ = rs.RowsAffected() | ||
assert.Equal(dbs.T(), int64(0), cnt) | ||
} |
Oops, something went wrong.