|
| 1 | +// Copyright 2022, OpenSergo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package config |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "io/ioutil" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + "github.com/opensergo/opensergo-control-plane/pkg/util" |
| 25 | + "gopkg.in/yaml.v2" |
| 26 | +) |
| 27 | + |
| 28 | +// LoadConfig for start OpenSergo Server. |
| 29 | +// Priority of loading config: LoadOption > SystemEnv > ConfigFile > DefaultConfig. |
| 30 | +// 1. NewDefaultConfig() to get the default config. |
| 31 | +// 2. Get ConfPath and override config from config file by overrideFromYml() . |
| 32 | +// 3. Override config from SystemEnv by overrideFromSystemEnv(). |
| 33 | +// 4. Override config from Option by overrideFromOpts(). |
| 34 | +func LoadConfig(opts ...Option) (*OpenSergoConfig, error) { |
| 35 | + // default config |
| 36 | + mergedConfig := NewDefaultConfig() |
| 37 | + |
| 38 | + // update initial config from File from Opts |
| 39 | + tmpConfig := NewDefaultConfig() |
| 40 | + tmpConfig.overrideFromOpts(opts...) |
| 41 | + mergedConfig.ConfPath = tmpConfig.ConfPath |
| 42 | + if err := mergedConfig.overrideFromYml(mergedConfig.ConfPath); err != nil { |
| 43 | + log.Println("read config from ConfPath[{}] error", tmpConfig.ConfPath) |
| 44 | + } |
| 45 | + |
| 46 | + // update initial config from System Env |
| 47 | + if err := mergedConfig.overrideFromSystemEnv(); err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + // update initial config from func args |
| 52 | + mergedConfig.overrideFromOpts(opts...) |
| 53 | + |
| 54 | + return mergedConfig, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (c *OpenSergoConfig) overrideFromOpts(opts ...Option) { |
| 58 | + if len(opts) > 0 { |
| 59 | + for _, opt := range opts { |
| 60 | + opt(c) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (c *OpenSergoConfig) InitOptsFromCommand() (opts []Option) { |
| 66 | + flag.StringVar(&c.ConfPath, "c", DefaultConfPath, "file path of config") |
| 67 | + flag.UintVar(&c.Port, "p", DefaultPort, "endpoint port of OpenSergo Control Plane.[ SystemEnvName: "+EnvKeyEndpointPort+" ][ YamlPath:endpointPort ]") |
| 68 | + flag.Parse() |
| 69 | + if c.ConfPath != DefaultConfPath { |
| 70 | + opts = append(opts, WithConfPath(c.ConfPath)) |
| 71 | + } |
| 72 | + if c.Port != DefaultPort { |
| 73 | + opts = append(opts, WithEndpointPort(c.Port)) |
| 74 | + } |
| 75 | + return opts |
| 76 | +} |
| 77 | + |
| 78 | +func (c *OpenSergoConfig) overrideFromYml(confPath string) error { |
| 79 | + _, err := os.Stat(confPath) |
| 80 | + if err != nil && !os.IsExist(err) { |
| 81 | + return err |
| 82 | + } |
| 83 | + content, err := ioutil.ReadFile(confPath) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + source := NewDefaultConfig() |
| 88 | + source.ConfPath = confPath |
| 89 | + err = yaml.Unmarshal(content, source) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + c.overrideFromOpenSergoConfig(source) |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (c *OpenSergoConfig) overrideFromOpenSergoConfig(source *OpenSergoConfig) error { |
| 99 | + c.ConfPath = source.ConfPath |
| 100 | + c.Port = source.Port |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +const ( |
| 105 | + EnvKeyEndpointPort = "OPENSERGO_ENDPOINT_PORT" |
| 106 | +) |
| 107 | + |
| 108 | +func (c *OpenSergoConfig) overrideFromSystemEnv() error { |
| 109 | + if portEnv := os.Getenv(EnvKeyEndpointPort); !util.IsBlank(portEnv) { |
| 110 | + port, err := strconv.ParseUint(portEnv, 10, 32) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } else { |
| 114 | + c.Port = uint(port) |
| 115 | + } |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
0 commit comments