multitenantStack/controllers/baseAPI.go

48 lines
909 B
Go

package controllers
import (
"database/sql"
"fmt"
companydb "multitenantStack/services"
"github.com/astaxie/beego/orm"
jwt "github.com/dgrijalva/jwt-go"
)
// BaseAPIController operations for APIs
type BaseAPIController struct {
BaseController
}
var jwtSession jwt.MapClaims
var companyDB *sql.DB
var o orm.Ormer
//var database sql.database
// Prepare parse all requests that come after this controller for valid auth
func (c *BaseAPIController) Prepare() {
tokenString := c.Ctx.Request.Header.Get("X-JWTtoken")
if tokenString == "" {
c.ServeJSONError("No Token provided")
return
}
token, db, err := companydb.GetDatabase(tokenString)
if err != nil {
c.ServeJSONError("Token invalid")
return
}
jwtSession = token
companyDB = db
o, err = orm.NewOrmWithDB("postgres", "company", companyDB)
if err != nil {
fmt.Println(err.Error())
c.ServeJSONError("internal")
return
}
}