github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/swagger_access.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 httpserver
    19  
    20  import (
    21  	"github.com/emicklei/go-restful/v3"
    22  	"github.com/go-openapi/spec"
    23  	restfulspec "github.com/polarismesh/go-restful-openapi/v2"
    24  )
    25  
    26  func (h *HTTPServer) enableSwaggerAPI(wsContainer *restful.Container) {
    27  	log.Infof("[HTTPServer] open http access for swagger API")
    28  	config := restfulspec.Config{
    29  		WebServices:                   wsContainer.RegisteredWebServices(), // you control what services are visible
    30  		APIPath:                       "/apidocs.json",
    31  		PostBuildSwaggerObjectHandler: enrichSwaggerObject,
    32  	}
    33  
    34  	wsContainer.Add(restfulspec.NewOpenAPIService(config))
    35  }
    36  
    37  func enrichSwaggerObject(swo *spec.Swagger) {
    38  	swo.Tags = []spec.Tag{
    39  		{TagProps: spec.TagProps{
    40  			Name:        "Client",
    41  			Description: "客户端接口"}},
    42  		{TagProps: spec.TagProps{
    43  			Name:        "ConfigConsole",
    44  			Description: "配置管理"}},
    45  		{TagProps: spec.TagProps{
    46  			Name:        "CircuitBreakers",
    47  			Description: "熔断规则管理"}},
    48  		{TagProps: spec.TagProps{
    49  			Name:        "Instances",
    50  			Description: "实例管理"}},
    51  		{TagProps: spec.TagProps{
    52  			Name:        "Maintain",
    53  			Description: "运维接口"}},
    54  		{TagProps: spec.TagProps{
    55  			Name:        "Namespaces",
    56  			Description: "命名空间管理"}},
    57  		{TagProps: spec.TagProps{
    58  			Name:        "RoutingRules",
    59  			Description: "路由规则管理"}},
    60  		{TagProps: spec.TagProps{
    61  			Name:        "RateLimits",
    62  			Description: "限流规则管理"}},
    63  		{TagProps: spec.TagProps{
    64  			Name:        "Services",
    65  			Description: "服务管理"}},
    66  		{TagProps: spec.TagProps{
    67  			Name:        "AuthRule",
    68  			Description: "鉴权规则管理"}},
    69  		{TagProps: spec.TagProps{
    70  			Name:        "Users",
    71  			Description: "用户/用户组管理"}},
    72  	}
    73  
    74  	swo.SecurityDefinitions = map[string]*spec.SecurityScheme{
    75  		"api_key": spec.APIKeyAuth("X-Polaris-Token", "header"),
    76  	}
    77  
    78  	var securitySetting []map[string][]string
    79  	apiKey := make(map[string][]string, 0)
    80  	apiKey["api_key"] = []string{}
    81  	securitySetting = append(securitySetting, apiKey)
    82  	swo.Security = securitySetting
    83  }