github.com/polarismesh/polaris@v1.17.8/bootstrap/config/config.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 config
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  
    26  	"gopkg.in/yaml.v2"
    27  
    28  	"github.com/polarismesh/polaris/admin"
    29  	"github.com/polarismesh/polaris/apiserver"
    30  	"github.com/polarismesh/polaris/auth"
    31  	"github.com/polarismesh/polaris/cache"
    32  	"github.com/polarismesh/polaris/common/log"
    33  	"github.com/polarismesh/polaris/config"
    34  	"github.com/polarismesh/polaris/namespace"
    35  	"github.com/polarismesh/polaris/plugin"
    36  	"github.com/polarismesh/polaris/service"
    37  	"github.com/polarismesh/polaris/service/healthcheck"
    38  	"github.com/polarismesh/polaris/store"
    39  )
    40  
    41  // Config 配置
    42  type Config struct {
    43  	Bootstrap    Bootstrap          `yaml:"bootstrap"`
    44  	APIServers   []apiserver.Config `yaml:"apiservers"`
    45  	Cache        cache.Config       `yaml:"cache"`
    46  	Namespace    namespace.Config   `yaml:"namespace"`
    47  	Naming       service.Config     `yaml:"naming"`
    48  	Config       config.Config      `yaml:"config"`
    49  	HealthChecks healthcheck.Config `yaml:"healthcheck"`
    50  	Maintain     admin.Config       `yaml:"maintain"`
    51  	Store        store.Config       `yaml:"store"`
    52  	Auth         auth.Config        `yaml:"auth"`
    53  	Plugin       plugin.Config      `yaml:"plugin"`
    54  }
    55  
    56  // Bootstrap 启动引导配置
    57  type Bootstrap struct {
    58  	Logger         map[string]*log.Options
    59  	StartInOrder   map[string]interface{} `yaml:"startInOrder"`
    60  	PolarisService PolarisService         `yaml:"polaris_service"`
    61  }
    62  
    63  // PolarisService polaris-server的自注册配置
    64  type PolarisService struct {
    65  	EnableRegister    bool       `yaml:"enable_register"`
    66  	ProbeAddress      string     `yaml:"probe_address"`
    67  	SelfAddress       string     `yaml:"self_address"`
    68  	NetworkInter      string     `yaml:"network_inter"`
    69  	Isolated          bool       `yaml:"isolated"`
    70  	DisableHeartbeat  bool       `yaml:"disable_heartbeat"`
    71  	HeartbeatInterval int        `yaml:"heartbeat_interval"`
    72  	Services          []*Service `yaml:"services"`
    73  }
    74  
    75  // Service 服务的自注册的配置
    76  type Service struct {
    77  	Name      string            `yaml:"name"`
    78  	Namespace string            `yaml:"namespace"`
    79  	Protocols []string          `yaml:"protocols"`
    80  	Metadata  map[string]string `yaml:"metadata"`
    81  }
    82  
    83  // APIEntries 对外提供的apiServers
    84  type APIEntries struct {
    85  	Name      string   `yaml:"name"`
    86  	Protocols []string `yaml:"protocols"`
    87  }
    88  
    89  const (
    90  	// DefaultPolarisName default polaris name
    91  	DefaultPolarisName = "polaris-server"
    92  	// DefaultPolarisNamespace default namespace
    93  	DefaultPolarisNamespace = "Polaris"
    94  	// DefaultFilePath default file path
    95  	DefaultFilePath = "polaris-server.yaml"
    96  	// DefaultHeartbeatInterval default interval second for heartbeat
    97  	DefaultHeartbeatInterval = 5
    98  )
    99  
   100  // Load 加载配置
   101  func Load(filePath string) (*Config, error) {
   102  	if filePath == "" {
   103  		err := errors.New("invalid config file path")
   104  		fmt.Printf("[ERROR] %v\n", err)
   105  		return nil, err
   106  	}
   107  
   108  	fmt.Printf("[INFO] load config from %v\n", filePath)
   109  
   110  	file, err := os.Open(filePath)
   111  	if err != nil {
   112  		fmt.Printf("[ERROR] %v\n", err)
   113  		return nil, err
   114  	}
   115  	defer func() {
   116  		_ = file.Close()
   117  	}()
   118  	buf, err := ioutil.ReadFile(filePath)
   119  	if nil != err {
   120  		return nil, fmt.Errorf("read file %s error", filePath)
   121  	}
   122  
   123  	conf := &Config{
   124  		Bootstrap: defaultBootstrap(),
   125  		Maintain:  *admin.DefaultConfig(),
   126  	}
   127  	if err = parseYamlContent(string(buf), conf); err != nil {
   128  		fmt.Printf("[ERROR] %v\n", err)
   129  		return nil, err
   130  	}
   131  
   132  	return conf, nil
   133  }
   134  
   135  func parseYamlContent(content string, conf *Config) error {
   136  	if err := yaml.Unmarshal([]byte(replaceEnv(content)), conf); nil != err {
   137  		return fmt.Errorf("parse yaml %s error:%w", content, err)
   138  	}
   139  	return nil
   140  }
   141  
   142  // replaceEnv replace holder by env list
   143  func replaceEnv(configContent string) string {
   144  	return os.ExpandEnv(configContent)
   145  }