github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/discover/v2/server.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package v2
    19  
    20  import (
    21  	"github.com/emicklei/go-restful/v3"
    22  
    23  	"github.com/polarismesh/polaris/apiserver/httpserver/docs"
    24  	"github.com/polarismesh/polaris/namespace"
    25  	"github.com/polarismesh/polaris/service"
    26  	"github.com/polarismesh/polaris/service/healthcheck"
    27  )
    28  
    29  // HTTPServerV2
    30  type HTTPServerV2 struct {
    31  	namespaceServer   namespace.NamespaceOperateServer
    32  	namingServer      service.DiscoverServer
    33  	healthCheckServer *healthcheck.Server
    34  }
    35  
    36  // NewV2Server 创建V2版本的HTTPServer
    37  func NewV2Server(
    38  	namespaceServer namespace.NamespaceOperateServer,
    39  	namingServer service.DiscoverServer,
    40  	healthCheckServer *healthcheck.Server) *HTTPServerV2 {
    41  	return &HTTPServerV2{
    42  		namespaceServer:   namespaceServer,
    43  		namingServer:      namingServer,
    44  		healthCheckServer: healthCheckServer,
    45  	}
    46  }
    47  
    48  const (
    49  	defaultReadAccess    string = "default-read"
    50  	defaultAccess        string = "default"
    51  	circuitBreakerAccess string = "circuitbreaker"
    52  	routingAccess        string = "router"
    53  	rateLimitAccess      string = "ratelimit"
    54  )
    55  
    56  // GetNamingConsoleAccessServer 注册管理端接口
    57  func (h *HTTPServerV2) GetNamingConsoleAccessServer(include []string) (*restful.WebService, error) {
    58  	consoleAccess := []string{defaultAccess}
    59  	ws := new(restful.WebService)
    60  	ws.Path("/naming/v2").Consumes(restful.MIME_JSON).Produces(restful.MIME_JSON)
    61  
    62  	// 如果为空,则开启全部接口
    63  	if len(include) == 0 {
    64  		include = consoleAccess
    65  	}
    66  	oldInclude := include
    67  
    68  	for _, item := range oldInclude {
    69  		if item == defaultReadAccess {
    70  			include = []string{defaultReadAccess}
    71  			break
    72  		}
    73  	}
    74  
    75  	for _, item := range oldInclude {
    76  		if item == defaultAccess {
    77  			include = consoleAccess
    78  			break
    79  		}
    80  	}
    81  	for _, item := range include {
    82  		switch item {
    83  		case defaultReadAccess:
    84  			h.addDefaultReadAccess(ws)
    85  		case defaultAccess:
    86  			h.addDefaultAccess(ws)
    87  		case routingAccess:
    88  			h.addRouterRuleAccess(ws)
    89  		}
    90  	}
    91  	return ws, nil
    92  }
    93  
    94  // addDefaultReadAccess 增加默认读接口
    95  func (h *HTTPServerV2) addDefaultReadAccess(ws *restful.WebService) {
    96  	ws.Route(docs.EnrichGetRouterRuleApiDocs(ws.GET("/routings").To(h.GetRoutings)))
    97  }
    98  
    99  // addDefaultAccess 增加默认接口
   100  func (h *HTTPServerV2) addDefaultAccess(ws *restful.WebService) {
   101  	h.addRouterRuleAccess(ws)
   102  }
   103  
   104  // addDefaultAccess 增加默认接口
   105  func (h *HTTPServerV2) addRouterRuleAccess(ws *restful.WebService) {
   106  	ws.Route(docs.EnrichCreateRouterRuleApiDocs(ws.POST("/routings").To(h.CreateRoutings)))
   107  	ws.Route(docs.EnrichDeleteRouterRuleApiDocs(ws.POST("/routings/delete").To(h.DeleteRoutings)))
   108  	ws.Route(docs.EnrichUpdateRouterRuleApiDocs(ws.PUT("/routings").To(h.UpdateRoutings)))
   109  	ws.Route(docs.EnrichGetRouterRuleApiDocs(ws.GET("/routings").To(h.GetRoutings)))
   110  	ws.Route(docs.EnrichEnableRouterRuleApiDocs(ws.PUT("/routings/enable").To(h.EnableRoutings)))
   111  }