diff --git a/conf/app.conf b/conf/app.conf index 090d2a2..7ff26a4 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -3,4 +3,4 @@ httpport = 8080 runmode = dev autorender = false copyrequestbody = true -EnableDocs = true +EnableDocs = true \ No newline at end of file diff --git a/conf/dbconfig.toml b/conf/dbconfig.toml new file mode 100644 index 0000000..7847160 --- /dev/null +++ b/conf/dbconfig.toml @@ -0,0 +1,6 @@ +Host = "127.0.0.1" +User = "postgres" +Password = "postgres" +Port = 5435 +Db = "system" +Ssl = "disable" diff --git a/main.go b/main.go index e940a63..5114980 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,6 @@ import ( ) func init() { - orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre sslmode=disable") - orm.RegisterDataBase("system", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable") - tokenTools.InitTokenToolsService() companydb.InitCompanyDBService() orm.DefaultTimeLoc = time.UTC diff --git a/services/companydb/companydb.go b/services/companydb/companydb.go index b49b7df..ab9a69a 100644 --- a/services/companydb/companydb.go +++ b/services/companydb/companydb.go @@ -4,19 +4,41 @@ import ( "database/sql" "errors" "fmt" + "io/ioutil" tokenTools "multitenantStack/services/tokenTools" "os" + "github.com/BurntSushi/toml" jwt "github.com/dgrijalva/jwt-go" ) +type DBConfig struct { + User string + Password string + Host string + Port int + Db string + Ssl string +} + +var Conf DBConfig var dbs map[string]*sql.DB // InitCompanyDBService Init companydb service and open system db connection func InitCompanyDBService() { + + tomlData, _ := ioutil.ReadFile("conf/dbConfig.toml") + if _, err := toml.Decode(string(tomlData), &Conf); err != nil { + // handle error + panic(err.Error()) + os.Exit(1) + } + dbs = make(map[string]*sql.DB) - systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable") + conStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", Conf.Host, Conf.Port, Conf.User, Conf.Password, Conf.Db, Conf.Ssl) + + systemDB, err := sql.Open("postgres", conStr) if err != nil { fmt.Println("Fatal: could not connect to db, exiting... Error:", err) os.Exit(1) @@ -26,7 +48,6 @@ func InitCompanyDBService() { // GetSystemDatabase returns system db func GetSystemDatabase() *sql.DB { - fmt.Println(dbs) return dbs["system"] } @@ -37,7 +58,7 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) { return dbs[companyName], nil } - conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName) + conStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", Conf.Host, Conf.Port, Conf.User, Conf.Password, companyName, Conf.Ssl) db, err := sql.Open("postgres", conStr) dbs[companyName] = db if err != nil {