github.com/vmware/transport-go@v1.3.4/service/service_lifecycle_manager.go (about)

     1  package service
     2  
     3  import (
     4  	"github.com/vmware/transport-go/model"
     5  	"net/http"
     6  )
     7  
     8  var svcLifecycleManagerInstance ServiceLifecycleManager
     9  
    10  type RequestBuilder func(w http.ResponseWriter, r *http.Request) model.Request
    11  
    12  type ServiceLifecycleManager interface {
    13  	GetServiceHooks(serviceChannelName string) ServiceLifecycleHookEnabled
    14  	OverrideRESTBridgeConfig(serviceChannelName string, config []*RESTBridgeConfig) error
    15  }
    16  
    17  type ServiceLifecycleHookEnabled interface {
    18  	OnServiceReady() chan bool                // service initialization logic should be implemented here
    19  	OnServerShutdown()                        // teardown logic goes here and will be automatically invoked on graceful server shutdown
    20  	GetRESTBridgeConfig() []*RESTBridgeConfig // service-to-REST endpoint mappings go here
    21  }
    22  
    23  type SetupRESTBridgeRequest struct {
    24  	ServiceChannel string
    25  	Override       bool
    26  	Config         []*RESTBridgeConfig
    27  }
    28  
    29  type RESTBridgeConfig struct {
    30  	ServiceChannel       string         // transport service channel
    31  	Uri                  string         // URI to map the transport service to
    32  	Method               string         // HTTP verb to map the transport service request to URI with
    33  	AllowHead            bool           // whether HEAD calls are allowed for this bridge point
    34  	AllowOptions         bool           // whether OPTIONS calls are allowed for this bridge point
    35  	FabricRequestBuilder RequestBuilder // function to transform HTTP request into a transport request
    36  }
    37  
    38  type serviceLifecycleManager struct {
    39  	serviceRegistryRef ServiceRegistry // service registry reference
    40  }
    41  
    42  // GetServiceHooks looks up the ServiceRegistry by service channel and returns the found service
    43  // lifecycle hooks implementation. returns nil if no such service channel exists.
    44  func (lm *serviceLifecycleManager) GetServiceHooks(serviceChannelName string) ServiceLifecycleHookEnabled {
    45  	service, err := lm.serviceRegistryRef.GetService(serviceChannelName)
    46  	if err != nil {
    47  		return nil
    48  	}
    49  
    50  	if lifecycleHookEnabled, ok := service.(ServiceLifecycleHookEnabled); ok {
    51  		return lifecycleHookEnabled
    52  	}
    53  	return nil
    54  }
    55  
    56  // OverrideRESTBridgeConfig overrides the REST bridge configuration currently present with the provided new bridge configs
    57  func (lm *serviceLifecycleManager) OverrideRESTBridgeConfig(serviceChannelName string, config []*RESTBridgeConfig) error {
    58  	_, err := lm.serviceRegistryRef.GetService(serviceChannelName)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	reg := lm.serviceRegistryRef.(*serviceRegistry)
    63  	if err = reg.bus.SendResponseMessage(
    64  		LifecycleManagerChannelName,
    65  		&SetupRESTBridgeRequest{ServiceChannel: serviceChannelName, Config: config, Override: true},
    66  		reg.bus.GetId()); err != nil {
    67  		return err
    68  	}
    69  	return nil
    70  }
    71  
    72  // GetServiceLifecycleManager returns a singleton instance of ServiceLifecycleManager
    73  func GetServiceLifecycleManager() ServiceLifecycleManager {
    74  	if svcLifecycleManagerInstance == nil {
    75  		svcLifecycleManagerInstance = &serviceLifecycleManager{
    76  			serviceRegistryRef: registry,
    77  		}
    78  	}
    79  	return svcLifecycleManagerInstance
    80  }
    81  
    82  // newServiceLifecycleManager returns a new instance of ServiceLifecycleManager
    83  func newServiceLifecycleManager(reg ServiceRegistry) ServiceLifecycleManager {
    84  	return &serviceLifecycleManager{serviceRegistryRef: reg}
    85  }