github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/clusters/proxy/manager.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package proxy
    19  
    20  import (
    21  	"github.com/aacfactory/errors"
    22  	"github.com/aacfactory/fns/commons/signatures"
    23  	"github.com/aacfactory/fns/context"
    24  	"github.com/aacfactory/fns/services"
    25  	"github.com/aacfactory/fns/transports"
    26  	"github.com/aacfactory/json"
    27  	"strconv"
    28  )
    29  
    30  var (
    31  	managerHandlerPath = append(handlerPathPrefix, []byte("/clusters/manager")...)
    32  )
    33  
    34  func FetchEndpointInfos(ctx context.Context, client transports.Client, signature signatures.Signature) (infos services.EndpointInfos, err error) {
    35  	cmd := Command{
    36  		Command: "infos",
    37  		Payload: nil,
    38  	}
    39  	body, sign, bodyErr := encodeCommand(cmd, signature)
    40  	if bodyErr != nil {
    41  		err = errors.Warning("fns: fetch endpoint infos failed").WithCause(bodyErr)
    42  		return
    43  	}
    44  	header := transports.AcquireHeader()
    45  	defer transports.ReleaseHeader(header)
    46  	header.Set(transports.ContentTypeHeaderName, contentType)
    47  	header.Set(transports.SignatureHeaderName, sign)
    48  	status, _, respBody, doErr := client.Do(ctx, transports.MethodPost, managerHandlerPath, header, body)
    49  	if doErr != nil {
    50  		err = errors.Warning("fns: fetch endpoint infos failed").WithCause(doErr)
    51  		return
    52  	}
    53  	if status == 200 {
    54  		infos = make(services.EndpointInfos, 0, 1)
    55  		err = json.Unmarshal(respBody, &infos)
    56  		if err != nil {
    57  			err = errors.Warning("fns: fetch endpoint infos failed").WithCause(err)
    58  			return
    59  		}
    60  		return
    61  	}
    62  	err = errors.Warning("fns: fetch endpoint infos failed").WithCause(errors.Warning("status is not ok").WithMeta("status", strconv.Itoa(status)))
    63  	return
    64  }
    65  
    66  func NewManagerHandler(manager services.EndpointsManager) transports.Handler {
    67  	return &ManagerHandler{
    68  		manager: manager,
    69  	}
    70  }
    71  
    72  type ManagerHandler struct {
    73  	manager services.EndpointsManager
    74  }
    75  
    76  func (handler *ManagerHandler) Handle(w transports.ResponseWriter, r transports.Request) {
    77  	cmd, cmdErr := ParseCommand(r)
    78  	if cmdErr != nil {
    79  		w.Failed(cmdErr)
    80  		return
    81  	}
    82  	switch cmd.Command {
    83  	case "infos":
    84  		infos := handler.manager.Info()
    85  		w.Succeed(infos)
    86  		break
    87  	default:
    88  		w.Failed(errors.Warning("fns: invalid proxy command"))
    89  	}
    90  }