DB Caching mechanism working and final service structure
This commit is contained in:
parent
11eed92c15
commit
df822d6ff9
@ -2,9 +2,9 @@ package controllers
|
||||
|
||||
import (
|
||||
"multitenantStack/models"
|
||||
companydb "multitenantStack/services/companydbservice"
|
||||
companydb "multitenantStack/services/companydb"
|
||||
|
||||
jwtservice "multitenantStack/services/jwtservice"
|
||||
tokenTools "multitenantStack/services/tokenTools"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
@ -36,8 +36,9 @@ func (c *AuthController) URLMapping() {
|
||||
func (c *AuthController) Login() {
|
||||
|
||||
type AuthResponse struct {
|
||||
Status int
|
||||
Jwt string
|
||||
Status int `json:"status"`
|
||||
Jwt string `json:"jwt"`
|
||||
User models.CompanyUser `json:"user"`
|
||||
}
|
||||
|
||||
if c.Ctx.Input.Method() != "POST" {
|
||||
@ -47,7 +48,7 @@ func (c *AuthController) Login() {
|
||||
|
||||
tokenHeader := c.Ctx.Request.Header.Get("X-JWTtoken")
|
||||
if tokenHeader != "" {
|
||||
valid, _ := jwtservice.Validate(tokenHeader)
|
||||
valid, _ := tokenTools.Validate(tokenHeader)
|
||||
if valid {
|
||||
c.ServeJSONError("You are already logged in")
|
||||
return
|
||||
@ -61,21 +62,18 @@ func (c *AuthController) Login() {
|
||||
c.ServeJSONError("Email/Password missing")
|
||||
return
|
||||
}
|
||||
/*
|
||||
systemdb := companydb.GetSystemDatabase()
|
||||
|
||||
if systemdb == nil {
|
||||
c.ServeJSONError("Error retrieving User")
|
||||
return
|
||||
}
|
||||
o, err := orm.NewOrmWithDB("postgres", "default", systemdb)
|
||||
if err != nil {
|
||||
c.ServeJSONError("Error retrieving User")
|
||||
return
|
||||
}
|
||||
*/
|
||||
o := orm.NewOrm()
|
||||
o.Using("system") //TODO: Replace this with something cleverer (manager) once implemented
|
||||
systemdb := companydb.GetSystemDatabase()
|
||||
|
||||
if systemdb == nil {
|
||||
c.ServeJSONError("Error retrieving User")
|
||||
return
|
||||
}
|
||||
o, err := orm.NewOrmWithDB("postgres", "default", systemdb)
|
||||
if err != nil {
|
||||
c.ServeJSONError("Error retrieving User")
|
||||
return
|
||||
}
|
||||
|
||||
userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, email)
|
||||
if err != nil {
|
||||
@ -109,20 +107,13 @@ func (c *AuthController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
//TODO: if found query the company database to get roleID, and name
|
||||
|
||||
name := companyUser.Name
|
||||
roleID := companyUser.Role
|
||||
|
||||
tokenString := ""
|
||||
if email == "admin@admin.at" && password == "my password" {
|
||||
// The jwtClaims are our trusted clientside session
|
||||
tokenString = jwtservice.CreateToken(jwt.MapClaims{
|
||||
tokenString = tokenTools.CreateToken(jwt.MapClaims{
|
||||
"email": email,
|
||||
"companyName": companyName,
|
||||
"companyUserID": companyUserID,
|
||||
"name": name,
|
||||
"roleID": roleID,
|
||||
"exp": time.Now().Unix() + 3600,
|
||||
})
|
||||
} else {
|
||||
@ -130,7 +121,7 @@ func (c *AuthController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
json := AuthResponse{200, tokenString}
|
||||
json := AuthResponse{200, tokenString, *companyUser}
|
||||
c.Data["json"] = &json
|
||||
|
||||
c.ServeJSON()
|
||||
|
@ -3,7 +3,7 @@ package controllers
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
companydb "multitenantStack/services/companydbservice"
|
||||
companydb "multitenantStack/services/companydb"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
|
@ -1 +1 @@
|
||||
{"/Users/LB/go/src/multitenantStack/controllers":1541629384273036460}
|
||||
{"/Users/LB/go/src/multitenantStack/controllers":1541672200497550656}
|
8
main.go
8
main.go
@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
_ "multitenantStack/routers"
|
||||
companydb "multitenantStack/services/companydbservice"
|
||||
jwt "multitenantStack/services/jwtservice"
|
||||
companydb "multitenantStack/services/companydb"
|
||||
tokenTools "multitenantStack/services/tokenTools"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
@ -15,8 +15,8 @@ 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")
|
||||
|
||||
jwt.InitJWTService()
|
||||
companydb.InitCompanyService()
|
||||
tokenTools.InitTokenToolsService()
|
||||
companydb.InitCompanyDBService()
|
||||
orm.DefaultTimeLoc = time.UTC
|
||||
if beego.BConfig.RunMode == "dev" {
|
||||
beego.BConfig.WebConfig.DirectoryIndex = true
|
||||
|
@ -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")
|
||||
}
|
@ -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
|
Loading…
Reference in New Issue
Block a user