serviceCachemdl.go 3.7 KiB
Newer Older
Roshan Patil's avatar
Roshan Patil committed
package routebuildermdl

import (
	"mime/multipart"
	"time"

	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/servicebuildermdl"

	"github.com/hashicorp/go-version"
	"github.com/tidwall/gjson"

Roshan Patil's avatar
Roshan Patil committed
	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/cachemdl"
)

var (
	restrictedServices cachemdl.FastCacheHelper
	roleBasedServices  cachemdl.FastCacheHelper
	openServices       cachemdl.FastCacheHelper
	loginService       func(*gjson.Result, servicebuildermdl.Principal) (interface{}, string, error)
Roshan Patil's avatar
Roshan Patil committed

	applicationVersion      *version.Version
	minimumSupportedVersion *version.Version
	isAppVersionEnabled     bool
	isStrictMode            bool
)
Roshan Patil's avatar
Roshan Patil committed

func init() {
	restrictedServices.Setup(10, time.Second*600, time.Second*600)
	openServices.Setup(10, time.Second*600, time.Second*600)
	roleBasedServices.Setup(10, time.Second*600, time.Second*600)
Roshan Patil's avatar
Roshan Patil committed
}

// ServiceCache ServiceCache object
type ServiceCache struct {
	Service         func(*gjson.Result, servicebuildermdl.Principal) (interface{}, error)
	FormService     func(*multipart.Form, servicebuildermdl.Principal) (interface{}, error)
Roshan Patil's avatar
Roshan Patil committed
	MasterService   *Runable
Roshan Patil's avatar
Roshan Patil committed
	IsMasterService bool
	IsFormService   bool
}

// RegisterNormalService is for transactional services
func RegisterNormalService(serviceName string, servicePtr func(*gjson.Result, servicebuildermdl.Principal) (interface{}, error), isRestricted, isRoleBased bool) {
Roshan Patil's avatar
Roshan Patil committed
	service := ServiceCache{
		Service: servicePtr,
	}
	commonServiceRegistration(serviceName, service, isRestricted, isRoleBased)
// RegisterFormBasedService if for multipart form based services
func RegisterFormBasedService(serviceName string, servicePtr func(*multipart.Form, servicebuildermdl.Principal) (interface{}, error), isRestricted, isRoleBased bool) {
Roshan Patil's avatar
Roshan Patil committed
	service := ServiceCache{
		FormService:   servicePtr,
		IsFormService: true,
	}
	commonServiceRegistration(serviceName, service, isRestricted, isRoleBased)
Roshan Patil's avatar
Roshan Patil committed
// // RegisterMasterService is for getting master type of file
// func RegisterMasterService(serviceName string, servicePtr *servicebuildermdl.ServiceBuilder, isRestricted, isRoleBased bool) {
// 	service := ServiceCache{
// 		MasterService:   servicePtr,
// 		IsMasterService: true,
// 	}
// 	commonServiceRegistration(serviceName, service, isRestricted, isRoleBased)
// }

// RegisterMasterService is for getting master type of file
Roshan Patil's avatar
Roshan Patil committed
func RegisterMasterService(serviceName string, isRestricted, isRoleBased bool) *Master {
	return &Master{
		serviceName:  serviceName,
		isRoleBased:  isRoleBased,
		isRestricted: isRestricted,
Roshan Patil's avatar
Roshan Patil committed
	}
// RegisterLoginSerivce is for login service
func RegisterLoginSerivce(service func(*gjson.Result, servicebuildermdl.Principal) (interface{}, string, error)) {
	loginService = service
}

func commonServiceRegistration(serviceName string, service ServiceCache, isRestricted, isRoleBased bool) {
Roshan Patil's avatar
Roshan Patil committed
	if isRestricted {
		if isRoleBased {
			roleBasedServices.SetNoExpiration(serviceName, service)
		} else {
			restrictedServices.SetNoExpiration(serviceName, service)
	} else {
		openServices.SetNoExpiration(serviceName, service)
Roshan Patil's avatar
Roshan Patil committed
	}
}
Roshan Patil's avatar
Roshan Patil committed

// SetAppVersion -SetAppVersion
func SetAppVersion(appVersion, minSupportedVersion string) error {
Roshan Patil's avatar
Roshan Patil committed

	app, err := version.NewVersion(appVersion)
	if errormdl.CheckErr(err) != nil {
		return errormdl.CheckErr(err)
Roshan Patil's avatar
Roshan Patil committed
	}
	min, err := version.NewVersion(minSupportedVersion)
	if errormdl.CheckErr(err) != nil {
		return errormdl.CheckErr(err)
	versionValidation := min.Compare(app)
	if versionValidation == 1 {
		return errormdl.Wrap("Minimum version is more than app version")
	}
	if versionValidation == 0 {
		isStrictMode = true
Roshan Patil's avatar
Roshan Patil committed
	}
	applicationVersion = app
	minimumSupportedVersion = min
	isAppVersionEnabled = true
	return nil
Roshan Patil's avatar
Roshan Patil committed
}