2018-11-08 10:21:06 +00:00
|
|
|
package companydb
|
2018-11-07 10:10:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-11-07 19:13:26 +00:00
|
|
|
"errors"
|
2018-11-07 10:10:51 +00:00
|
|
|
"fmt"
|
2018-11-13 18:02:47 +00:00
|
|
|
"io/ioutil"
|
2018-11-08 10:21:06 +00:00
|
|
|
tokenTools "multitenantStack/services/tokenTools"
|
2018-11-07 10:10:51 +00:00
|
|
|
"os"
|
|
|
|
|
2018-11-13 18:02:47 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2018-11-07 19:13:26 +00:00
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
2018-11-07 10:10:51 +00:00
|
|
|
)
|
|
|
|
|
2018-11-13 18:02:47 +00:00
|
|
|
type DBConfig struct {
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Db string
|
|
|
|
Ssl string
|
|
|
|
}
|
|
|
|
|
|
|
|
var Conf DBConfig
|
2018-11-07 10:10:51 +00:00
|
|
|
var dbs map[string]*sql.DB
|
|
|
|
|
2018-11-08 10:21:06 +00:00
|
|
|
// InitCompanyDBService Init companydb service and open system db connection
|
|
|
|
func InitCompanyDBService() {
|
2018-11-13 18:02:47 +00:00
|
|
|
|
|
|
|
tomlData, _ := ioutil.ReadFile("conf/dbConfig.toml")
|
|
|
|
if _, err := toml.Decode(string(tomlData), &Conf); err != nil {
|
|
|
|
// handle error
|
|
|
|
panic(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:21:06 +00:00
|
|
|
dbs = make(map[string]*sql.DB)
|
2018-11-07 10:10:51 +00:00
|
|
|
|
2018-11-13 18:02:47 +00:00
|
|
|
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)
|
2018-11-07 10:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Fatal: could not connect to db, exiting... Error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
dbs["system"] = systemDB
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:36:08 +00:00
|
|
|
// GetSystemDatabase returns system db
|
|
|
|
func GetSystemDatabase() *sql.DB {
|
|
|
|
return dbs["system"]
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:42:31 +00:00
|
|
|
// GetDatabaseWithName Get orm and user information
|
2018-11-08 07:36:08 +00:00
|
|
|
func GetDatabaseWithName(companyName string) (*sql.DB, error) {
|
|
|
|
if dbs[companyName] != nil {
|
|
|
|
fmt.Println("DB Already open")
|
|
|
|
return dbs[companyName], nil
|
|
|
|
}
|
|
|
|
|
2018-11-13 18:02:47 +00:00
|
|
|
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)
|
2018-11-08 07:36:08 +00:00
|
|
|
db, err := sql.Open("postgres", conStr)
|
2018-11-08 10:21:06 +00:00
|
|
|
dbs[companyName] = db
|
2018-11-08 07:36:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: call upper function in this one to reduce code
|
2018-11-08 10:42:31 +00:00
|
|
|
|
2018-11-07 10:10:51 +00:00
|
|
|
// GetDatabase Get orm and user information
|
2018-11-07 19:13:26 +00:00
|
|
|
func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
2018-11-07 10:10:51 +00:00
|
|
|
// validate token
|
2018-11-08 10:21:06 +00:00
|
|
|
valid, token := tokenTools.Validate(tokenString)
|
2018-11-07 19:13:26 +00:00
|
|
|
if !valid {
|
|
|
|
return nil, nil, errors.New("Token is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenMap := token.Claims.(jwt.MapClaims)
|
|
|
|
companyName := tokenMap["companyName"].(string)
|
|
|
|
|
|
|
|
if dbs[companyName] != nil {
|
|
|
|
fmt.Println("DB Already open")
|
|
|
|
return tokenMap, dbs[companyName], nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:36:08 +00:00
|
|
|
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName)
|
2018-11-07 19:13:26 +00:00
|
|
|
fmt.Println(conStr)
|
|
|
|
db, err := sql.Open("postgres", conStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:10:51 +00:00
|
|
|
// return db with orm or error
|
2018-11-07 19:13:26 +00:00
|
|
|
return tokenMap, db, nil
|
2018-11-07 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDatabase Create a database by copying the template
|
2018-11-12 10:43:14 +00:00
|
|
|
func CreateDatabase(companyName string) {
|
2018-11-07 10:10:51 +00:00
|
|
|
/*
|
2018-11-12 10:43:14 +00:00
|
|
|
if dbs[companyName] != nil {
|
|
|
|
fmt.Println("DB Already open")
|
|
|
|
return dbs[companyName], nil
|
2018-11-07 10:10:51 +00:00
|
|
|
}
|
2018-11-12 10:43:14 +00:00
|
|
|
/*
|
|
|
|
db, err = sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company5 sslmode=disable")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-07 10:10:51 +00:00
|
|
|
|
2018-11-12 10:43:14 +00:00
|
|
|
or, err := orm.NewOrmWithDB("postgres", "temp", db)
|
2018-11-07 10:10:51 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDatabase Delete an entire database, this is very very dangerous :-)
|
|
|
|
func DeleteDatabase(token string) {
|
2018-11-08 07:36:08 +00:00
|
|
|
//_, db, err := GetDatabase(token)
|
2018-11-07 10:10:51 +00:00
|
|
|
/*
|
2018-11-08 07:36:08 +00:00
|
|
|
remove from map!
|
|
|
|
db.Close()
|
|
|
|
fmt.Println("Closed company5")
|
|
|
|
//}
|
|
|
|
|
|
|
|
res, err := o.Raw("DROP DATABASE company5;").Exec()
|
|
|
|
if err == nil {
|
|
|
|
num, _ := res.RowsAffected()
|
|
|
|
fmt.Println("mysql row affected number: ", num)
|
|
|
|
}
|
2018-11-07 10:10:51 +00:00
|
|
|
*/
|
|
|
|
}
|