Changing companydb to read config file
This commit is contained in:
parent
d760194f1a
commit
4c3dec838e
6
conf/dbconfig.toml
Normal file
6
conf/dbconfig.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Host = "127.0.0.1"
|
||||||
|
User = "postgres"
|
||||||
|
Password = "postgres"
|
||||||
|
Port = 5435
|
||||||
|
Db = "system"
|
||||||
|
Ssl = "disable"
|
3
main.go
3
main.go
@ -12,9 +12,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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()
|
tokenTools.InitTokenToolsService()
|
||||||
companydb.InitCompanyDBService()
|
companydb.InitCompanyDBService()
|
||||||
orm.DefaultTimeLoc = time.UTC
|
orm.DefaultTimeLoc = time.UTC
|
||||||
|
@ -4,19 +4,41 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
tokenTools "multitenantStack/services/tokenTools"
|
tokenTools "multitenantStack/services/tokenTools"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
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
|
var dbs map[string]*sql.DB
|
||||||
|
|
||||||
// InitCompanyDBService Init companydb service and open system db connection
|
// InitCompanyDBService Init companydb service and open system db connection
|
||||||
func InitCompanyDBService() {
|
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)
|
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 {
|
if err != nil {
|
||||||
fmt.Println("Fatal: could not connect to db, exiting... Error:", err)
|
fmt.Println("Fatal: could not connect to db, exiting... Error:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -26,7 +48,6 @@ func InitCompanyDBService() {
|
|||||||
|
|
||||||
// GetSystemDatabase returns system db
|
// GetSystemDatabase returns system db
|
||||||
func GetSystemDatabase() *sql.DB {
|
func GetSystemDatabase() *sql.DB {
|
||||||
fmt.Println(dbs)
|
|
||||||
return dbs["system"]
|
return dbs["system"]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +58,7 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) {
|
|||||||
return dbs[companyName], nil
|
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)
|
db, err := sql.Open("postgres", conStr)
|
||||||
dbs[companyName] = db
|
dbs[companyName] = db
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user