Changing companydb to read config file
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user