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

import (
Roshan Patil's avatar
Roshan Patil committed
	"bytes"
	"fmt"
Roshan Patil's avatar
Roshan Patil committed
	"mime/multipart"
Roshan Patil's avatar
Roshan Patil committed
	"strings"
Roshan Patil's avatar
Roshan Patil committed
	"time"

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

	"github.com/tidwall/gjson"

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

	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/filemdl"

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

Roshan Patil's avatar
Roshan Patil committed
const (
	// EXPORT :- js export
	EXPORT = "export"
	// CONSTT :- js const
	CONSTT     = "const"
	open       = "O_"
	restricted = "R_"
	acl        = "C_"
)

Roshan Patil's avatar
Roshan Patil committed
var restrictedServices cachemdl.FastCacheHelper
var roleBasedServices cachemdl.FastCacheHelper
Roshan Patil's avatar
Roshan Patil committed
var openServices cachemdl.FastCacheHelper
var loginService func(*gjson.Result, servicebuildermdl.Principal) (interface{}, string, error)
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
	RegisterNormalService("CreateServiceJS", createServiceJS, false, false)
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)
		restrictedServices.SetNoExpiration(serviceName, service)
	} else {
		openServices.SetNoExpiration(serviceName, service)
Roshan Patil's avatar
Roshan Patil committed

func createServiceJS(rs *gjson.Result, principalObj servicebuildermdl.Principal) (interface{}, error) {
Roshan Patil's avatar
Roshan Patil committed
	buf := new(bytes.Buffer)

	for k := range openServices.FastCache.Items() {
		srv := EXPORT + " " + CONSTT + " " + open + strings.ToUpper(k) + " = 'O." + k + "'"
		buf.WriteString(fmt.Sprintln(srv))
	}
	for k := range restrictedServices.FastCache.Items() {
		srv := EXPORT + " " + CONSTT + " " + restricted + strings.ToUpper(k) + " = 'R." + k + "'"
		buf.WriteString(fmt.Sprintln(srv))
	}
	for k := range roleBasedServices.FastCache.Items() {
		srv := EXPORT + " " + CONSTT + " " + acl + strings.ToUpper(k) + " = 'C." + k + "'"
		buf.WriteString(fmt.Sprintln(srv))
	}

	saveError := filemdl.GetInstance().Save("./services.js", buf.Bytes(), true, false)
	if errormdl.CheckErr(saveError) != nil {
		return nil, errormdl.CheckErr(saveError)
	}
	return "success", nil
}