Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
passing dbname to postgres module + remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Dec 7, 2019
1 parent 6c8a7fe commit 9ab3e73
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Example of typical and scalable RESTful API Server for Go
| Name | Type | Default | Required |
|---|---|---|:---:|
|APP_ADDRESS|string|:8089|Yes|
|PG_DBNAME|string|typical-rest|Yes|
|PG_DBNAME|string||Yes|
|PG_HOST|string|localhost||
|PG_PASSWORD|string|pgpass|Yes|
|PG_PORT|int|5432||
Expand Down
18 changes: 11 additions & 7 deletions pkg/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ const (

// Config is postgres configuration
type Config struct {
DBName string `required:"true" default:"typical-rest"`
DBName string `required:"true"`
User string `required:"true" default:"postgres"`
Password string `required:"true" default:"pgpass"`
Host string `default:"localhost"`
Port int `default:"5432"`
}

// Module for postgres
func Module() interface{} {
return &module{}
func Module(dbname string) interface{} {
return &module{
dbname: dbname,
}
}

type module struct{}
type module struct {
dbname string
}

// Command of module
func (p module) Commands(c *typcli.ModuleCli) []*cli.Command {
Expand Down Expand Up @@ -78,7 +82,9 @@ func (p module) Prepare() []interface{} {

func (p module) Configure() (prefix string, spec, loadFn interface{}) {
prefix = "PG"
spec = &Config{}
spec = &Config{
DBName: p.dbname,
}
loadFn = func(loader typcfg.Loader) (cfg Config, err error) {
err = loader.Load(prefix, &cfg)
return
Expand Down Expand Up @@ -142,11 +148,9 @@ func (p module) createDB(cfg Config) (err error) {
return
}
defer conn.Close()
fmt.Println("1")
if err = conn.Ping(); err != nil {
return
}
fmt.Println("2")
_, err = conn.Exec(query)
return
}
Expand Down
2 changes: 1 addition & 1 deletion typical/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Context = &typctx.Context{
Modules: []interface{}{
typdocker.Module(),
typserver.Module(),
typpostgres.Module(),
typpostgres.Module("sample"),
typredis.Module(),
},
ReadmeGenerator: typreadme.Generator{},
Expand Down

0 comments on commit 9ab3e73

Please sign in to comment.