DB Caching mechanism working and final service structure

This commit is contained in:
2018-11-08 11:21:06 +01:00
parent 11eed92c15
commit df822d6ff9
6 changed files with 35 additions and 47 deletions

View File

@ -1,10 +1,10 @@
package companydbservice
package companydb
import (
"database/sql"
"errors"
"fmt"
"multitenantStack/services/jwtservice"
tokenTools "multitenantStack/services/tokenTools"
"os"
jwt "github.com/dgrijalva/jwt-go"
@ -12,10 +12,9 @@ import (
var dbs map[string]*sql.DB
// InitCompanyService Init companydb service and open system db connection
func InitCompanyService() {
fmt.Println("Hello from init") // test if init gets called from each orm
dbs := make(map[string]*sql.DB)
// InitCompanyDBService Init companydb service and open system db connection
func InitCompanyDBService() {
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")
if err != nil {
@ -23,30 +22,28 @@ func InitCompanyService() {
os.Exit(1)
}
dbs["system"] = systemDB
}
// GetSystemDatabase returns system db
func GetSystemDatabase() *sql.DB {
fmt.Println(dbs)
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)
dbs[companyName] = db
if err != nil {
return nil, err
}
// return db with orm or error
return db, nil
}
@ -54,7 +51,7 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) {
// GetDatabase Get orm and user information
func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
// validate token
valid, token := jwtservice.Validate(tokenString)
valid, token := tokenTools.Validate(tokenString)
if !valid {
return nil, nil, errors.New("Token is invalid")
}

View File

@ -1,4 +1,4 @@
package jwtservice
package tokenTools
import (
"crypto/rand"
@ -17,7 +17,7 @@ func GenerateSecret() []byte {
}
// InitJWTService generate the secret to verify JWTs and store it in memory
func InitJWTService() {
func InitTokenToolsService() {
hmacSecret = GenerateSecret()
fmt.Println("InitJWTService", hmacSecret)
// TODO: This needs to be replaced with reading rsa keys, there needs to be a automatic generation of these if they do not exist