dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/rest/config/rest_config.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"github.com/creasty/defaults"
    22  )
    23  
    24  var (
    25  	restConsumerServiceConfigMap map[string]*RestServiceConfig
    26  	restProviderServiceConfigMap map[string]*RestServiceConfig
    27  )
    28  
    29  // nolint
    30  type RestConsumerConfig struct {
    31  	Client                string                        `default:"resty" yaml:"rest_client" json:"rest_client,omitempty" property:"rest_client"`
    32  	Produces              string                        `default:"application/json" yaml:"rest_produces"  json:"rest_produces,omitempty" property:"rest_produces"`
    33  	Consumes              string                        `default:"application/json" yaml:"rest_consumes"  json:"rest_consumes,omitempty" property:"rest_consumes"`
    34  	RestServiceConfigsMap map[string]*RestServiceConfig `yaml:"references" json:"references,omitempty" property:"references"`
    35  }
    36  
    37  // UnmarshalYAML unmarshals the RestConsumerConfig by @unmarshal function
    38  func (c *RestConsumerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
    39  	if err := defaults.Set(c); err != nil {
    40  		return err
    41  	}
    42  	type plain RestConsumerConfig
    43  	if err := unmarshal((*plain)(c)); err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  // nolint
    50  type RestProviderConfig struct {
    51  	Server                string                        `default:"go-restful" yaml:"rest_server" json:"rest_server,omitempty" property:"rest_server"`
    52  	Produces              string                        `default:"*/*" yaml:"rest_produces"  json:"rest_produces,omitempty" property:"rest_produces"`
    53  	Consumes              string                        `default:"*/*" yaml:"rest_consumes"  json:"rest_consumes,omitempty" property:"rest_consumes"`
    54  	RestServiceConfigsMap map[string]*RestServiceConfig `yaml:"services" json:"services,omitempty" property:"services"`
    55  }
    56  
    57  // UnmarshalYAML unmarshals the RestProviderConfig by @unmarshal function
    58  func (c *RestProviderConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
    59  	if err := defaults.Set(c); err != nil {
    60  		return err
    61  	}
    62  	type plain RestProviderConfig
    63  	if err := unmarshal((*plain)(c)); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  // nolint
    70  type RestServiceConfig struct {
    71  	InterfaceName        string              `required:"true"  yaml:"interface"  json:"interface,omitempty" property:"interface"`
    72  	URL                  string              `yaml:"url"  json:"url,omitempty" property:"url"`
    73  	Path                 string              `yaml:"rest_path"  json:"rest_path,omitempty" property:"rest_path"`
    74  	Produces             string              `yaml:"rest_produces"  json:"rest_produces,omitempty" property:"rest_produces"`
    75  	Consumes             string              `yaml:"rest_consumes"  json:"rest_consumes,omitempty" property:"rest_consumes"`
    76  	MethodType           string              `yaml:"rest_method"  json:"rest_method,omitempty" property:"rest_method"`
    77  	Client               string              `yaml:"rest_client" json:"rest_client,omitempty" property:"rest_client"`
    78  	Server               string              `yaml:"rest_server" json:"rest_server,omitempty" property:"rest_server"`
    79  	RestMethodConfigs    []*RestMethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"`
    80  	RestMethodConfigsMap map[string]*RestMethodConfig
    81  }
    82  
    83  // UnmarshalYAML unmarshals the RestServiceConfig by @unmarshal function
    84  func (c *RestServiceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
    85  	if err := defaults.Set(c); err != nil {
    86  		return err
    87  	}
    88  	type plain RestServiceConfig
    89  	if err := unmarshal((*plain)(c)); err != nil {
    90  		return err
    91  	}
    92  	return nil
    93  }
    94  
    95  // nolint
    96  type RestMethodConfig struct {
    97  	InterfaceName  string
    98  	MethodName     string `required:"true" yaml:"name"  json:"name,omitempty" property:"name"`
    99  	URL            string `yaml:"url"  json:"url,omitempty" property:"url"`
   100  	Path           string `yaml:"rest_path"  json:"rest_path,omitempty" property:"rest_path"`
   101  	Produces       string `yaml:"rest_produces"  json:"rest_produces,omitempty" property:"rest_produces"`
   102  	Consumes       string `yaml:"rest_consumes"  json:"rest_consumes,omitempty" property:"rest_consumes"`
   103  	MethodType     string `yaml:"rest_method"  json:"rest_method,omitempty" property:"rest_method"`
   104  	PathParams     string `yaml:"rest_path_params"  json:"rest_path_params,omitempty" property:"rest_path_params"`
   105  	PathParamsMap  map[int]string
   106  	QueryParams    string `yaml:"rest_query_params"  json:"rest_query_params,omitempty" property:"rest_query_params"`
   107  	QueryParamsMap map[int]string
   108  	Body           int    `default:"-1" yaml:"rest_body"  json:"rest_body,omitempty" property:"rest_body"`
   109  	Headers        string `yaml:"rest_headers"  json:"rest_headers,omitempty" property:"rest_headers"`
   110  	HeadersMap     map[int]string
   111  }
   112  
   113  // UnmarshalYAML unmarshals the RestMethodConfig by @unmarshal function
   114  func (c *RestMethodConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
   115  	if err := defaults.Set(c); err != nil {
   116  		return err
   117  	}
   118  	type plain RestMethodConfig
   119  	if err := unmarshal((*plain)(c)); err != nil {
   120  		return err
   121  	}
   122  	return nil
   123  }
   124  
   125  // nolint
   126  func GetRestConsumerServiceConfig(id string) *RestServiceConfig {
   127  	return restConsumerServiceConfigMap[id]
   128  }
   129  
   130  // nolint
   131  func GetRestProviderServiceConfig(id string) *RestServiceConfig {
   132  	return restProviderServiceConfigMap[id]
   133  }
   134  
   135  // nolint
   136  func SetRestConsumerServiceConfigMap(configMap map[string]*RestServiceConfig) {
   137  	restConsumerServiceConfigMap = configMap
   138  }
   139  
   140  // nolint
   141  func SetRestProviderServiceConfigMap(configMap map[string]*RestServiceConfig) {
   142  	restProviderServiceConfigMap = configMap
   143  }
   144  
   145  // nolint
   146  func GetRestConsumerServiceConfigMap() map[string]*RestServiceConfig {
   147  	return restConsumerServiceConfigMap
   148  }
   149  
   150  // nolint
   151  func GetRestProviderServiceConfigMap() map[string]*RestServiceConfig {
   152  	return restProviderServiceConfigMap
   153  }