Moving Services to seperate namespaces again, auth endpoint working
This commit is contained in:
@ -1,12 +1,12 @@
|
||||
package services
|
||||
package companydbservice
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"multitenantStack/services/jwtservice"
|
||||
"os"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
@ -17,9 +17,7 @@ func InitCompanyService() {
|
||||
fmt.Println("Hello from init") // test if init gets called from each orm
|
||||
dbs := make(map[string]*sql.DB)
|
||||
|
||||
orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre sslmode=disable")
|
||||
|
||||
systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company5 sslmode=disable")
|
||||
systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable")
|
||||
if err != nil {
|
||||
fmt.Println("Fatal: could not connect to db, exiting... Error:", err)
|
||||
os.Exit(1)
|
||||
@ -28,10 +26,35 @@ func InitCompanyService() {
|
||||
|
||||
}
|
||||
|
||||
// GetSystemDatabase returns system db
|
||||
func GetSystemDatabase() *sql.DB {
|
||||
return dbs["system"]
|
||||
}
|
||||
|
||||
// GetDatabase Get orm and user information
|
||||
func GetDatabaseWithName(companyName string) (*sql.DB, error) {
|
||||
|
||||
if dbs[companyName] != nil {
|
||||
fmt.Println("DB Already open")
|
||||
return dbs[companyName], nil
|
||||
}
|
||||
|
||||
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName)
|
||||
fmt.Println(conStr)
|
||||
db, err := sql.Open("postgres", conStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// return db with orm or error
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// TODO: call upper function in this one to reduce code
|
||||
// GetDatabase Get orm and user information
|
||||
func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
||||
// validate token
|
||||
valid, token := Validate(tokenString)
|
||||
valid, token := jwtservice.Validate(tokenString)
|
||||
if !valid {
|
||||
return nil, nil, errors.New("Token is invalid")
|
||||
}
|
||||
@ -44,7 +67,7 @@ func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
||||
return tokenMap, dbs[companyName], nil
|
||||
}
|
||||
|
||||
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", tokenMap["companyName"])
|
||||
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName)
|
||||
fmt.Println(conStr)
|
||||
db, err := sql.Open("postgres", conStr)
|
||||
if err != nil {
|
||||
@ -69,15 +92,17 @@ func CreateDatabase(token string) {
|
||||
|
||||
// DeleteDatabase Delete an entire database, this is very very dangerous :-)
|
||||
func DeleteDatabase(token string) {
|
||||
//_, db, err := GetDatabase(token)
|
||||
/*
|
||||
db.Close()
|
||||
fmt.Println("Closed company5")
|
||||
//}
|
||||
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)
|
||||
}
|
||||
res, err := o.Raw("DROP DATABASE company5;").Exec()
|
||||
if err == nil {
|
||||
num, _ := res.RowsAffected()
|
||||
fmt.Println("mysql row affected number: ", num)
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package services
|
||||
package jwtservice
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
Reference in New Issue
Block a user