github.com/pavlo67/common@v0.5.3/common/server_http/endpoints_join.go (about)

     1  package server_http
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/pavlo67/common/common/errors"
     8  	"github.com/pavlo67/common/common/joiner"
     9  
    10  	"github.com/pavlo67/common/common/logger"
    11  )
    12  
    13  type Endpoints []Endpoint
    14  
    15  func (eps Endpoints) Join(joinerOp joiner.Operator) error {
    16  	for i, ep := range eps {
    17  
    18  		if err := joinerOp.Join(&eps[i], ep.InternalKey); err != nil {
    19  			return errors.CommonError(err, fmt.Sprintf("can't join %#v as Endpoint with key '%s'", ep, ep.InternalKey))
    20  		}
    21  	}
    22  
    23  	return nil
    24  }
    25  
    26  const onHandleEndpoints = "on server_http.HandleEndpoints()"
    27  
    28  func (c *Config) HandleEndpoints(srvOp Operator, l logger.Operator) error {
    29  	if c == nil {
    30  		return nil
    31  	}
    32  
    33  	if srvOp == nil {
    34  		return errors.New(onHandleEndpoints + ": srvOp == nil")
    35  	}
    36  
    37  	for key, ep := range c.EndpointsSettled {
    38  		if ep.Endpoint == nil {
    39  			return fmt.Errorf(onHandleEndpoints+": endpoint %s to handle is nil (%#v)", key, ep)
    40  		}
    41  		if err := srvOp.HandleEndpoint(key, c.Prefix+ep.Path, *ep.Endpoint); err != nil {
    42  			return fmt.Errorf(onHandleEndpoints+": handling %s, %s, %#v got %s", key, ep.Path, ep, err)
    43  		}
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  // joining endpoints -----------------------------------------------------
    50  
    51  func (c *Config) CompleteWithJoiner(joinerOp joiner.Operator, host string, port int) error {
    52  	if c == nil {
    53  		return errors.New("no server_http.Config to be completed")
    54  	}
    55  
    56  	var portStr string
    57  	if port > 0 {
    58  		portStr = ":" + strconv.Itoa(port)
    59  	}
    60  	c.Host, c.Port = host, portStr
    61  
    62  	for key, ep := range c.EndpointsSettled {
    63  		//if c.EndpointsSettled[key].Endpoint != nil {
    64  		//	continue
    65  		//}
    66  
    67  		if endpoint, ok := joinerOp.Interface(key).(Endpoint); ok {
    68  			ep.Endpoint = &endpoint
    69  			c.EndpointsSettled[key] = ep
    70  		} else if endpointPtr, _ := joinerOp.Interface(key).(*Endpoint); endpointPtr != nil {
    71  			ep.Endpoint = endpointPtr
    72  			c.EndpointsSettled[key] = ep
    73  		} else {
    74  			return fmt.Errorf("no server_http.Endpoint joined with key %s", key)
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  // TODO: be careful, it's method for http-client only
    82  // TODO: be careful, it shouldn't be used on server side because it uses non-initiated (without starter.Operator.Run()) endpoints
    83  func (c *Config) CompleteDirectly(endpoints Endpoints, host string, port int, ignoreAbsent bool) error {
    84  	if c == nil {
    85  		return errors.New("no server_http.Config to be completed")
    86  	}
    87  
    88  	var portStr string
    89  	if port > 0 {
    90  		portStr = ":" + strconv.Itoa(port)
    91  	}
    92  	c.Host, c.Port = host, portStr
    93  
    94  EP_SETTLED:
    95  	for key, epSettled := range c.EndpointsSettled {
    96  		// TODO??? use epSettled.InternalKey to correct the main key value
    97  
    98  		for _, ep := range endpoints {
    99  			if ep.InternalKey == key {
   100  				epSettled.Endpoint = &ep
   101  				c.EndpointsSettled[key] = epSettled
   102  				continue EP_SETTLED
   103  			}
   104  		}
   105  		if !ignoreAbsent {
   106  			return fmt.Errorf("no server_http.Endpoint with key %s", key)
   107  		}
   108  		fmt.Printf("no server_http.Endpoint with key %s\n", key)
   109  	}
   110  
   111  	return nil
   112  }