-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconf.go
More file actions
133 lines (120 loc) · 4.05 KB
/
conf.go
File metadata and controls
133 lines (120 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package conf
import (
"bytes"
"os"
"path/filepath"
"github.com/apache/answer/internal/base/data"
"github.com/apache/answer/internal/base/server"
"github.com/apache/answer/internal/base/translator"
"github.com/apache/answer/internal/cli"
"github.com/apache/answer/internal/router"
"github.com/apache/answer/internal/service/service_config"
"github.com/apache/answer/pkg/writer"
"github.com/segmentfault/pacman/contrib/conf/viper"
"github.com/segmentfault/pacman/log"
"gopkg.in/yaml.v3"
)
// AllConfig all config
type AllConfig struct {
Debug bool `json:"debug" mapstructure:"debug" yaml:"debug"`
Server *Server `json:"server" mapstructure:"server" yaml:"server"`
Data *Data `json:"data" mapstructure:"data" yaml:"data"`
I18n *translator.I18n `json:"i18n" mapstructure:"i18n" yaml:"i18n"`
ServiceConfig *service_config.ServiceConfig `json:"service_config" mapstructure:"service_config" yaml:"service_config"`
Swaggerui *router.SwaggerConfig `json:"swaggerui" mapstructure:"swaggerui" yaml:"swaggerui"`
UI *server.UI `json:"ui" mapstructure:"ui" yaml:"ui"`
}
type envConfigOverrides struct {
SwaggerHost string
SwaggerAddressPort string
SiteAddr string
StorageMode string
}
func loadEnvs() (envOverrides *envConfigOverrides) {
return &envConfigOverrides{
SwaggerHost: os.Getenv("SWAGGER_HOST"),
SwaggerAddressPort: os.Getenv("SWAGGER_ADDRESS_PORT"),
SiteAddr: os.Getenv("SITE_ADDR"),
StorageMode: os.Getenv("FILE_STORAGE_MODE"),
}
}
type PathIgnore struct {
Users []string `yaml:"users"`
}
// Server server config
type Server struct {
HTTP *server.HTTP `json:"http" mapstructure:"http" yaml:"http"`
}
// Data data config
type Data struct {
Database *data.Database `json:"database" mapstructure:"database" yaml:"database"`
Cache *data.CacheConf `json:"cache" mapstructure:"cache" yaml:"cache"`
}
// SetDefault set default config
func (c *AllConfig) SetDefault() {
if c.UI == nil {
c.UI = &server.UI{}
}
}
func (c *AllConfig) SetEnvironmentOverrides() {
envs := loadEnvs()
if envs.SiteAddr != "" {
c.Server.HTTP.Addr = envs.SiteAddr
}
if envs.SwaggerHost != "" {
c.Swaggerui.Host = envs.SwaggerHost
}
if envs.SwaggerAddressPort != "" {
c.Swaggerui.Address = envs.SwaggerAddressPort
}
if envs.StorageMode == "db" {
c.ServiceConfig.UseDbFileStorage = true
log.Info("saving files as blob in db")
}
}
// ReadConfig read config
func ReadConfig(configFilePath string) (c *AllConfig, err error) {
if len(configFilePath) == 0 {
configFilePath = filepath.Join(cli.ConfigFileDir, cli.DefaultConfigFileName)
}
c = &AllConfig{}
config, err := viper.NewWithPath(configFilePath)
if err != nil {
return nil, err
}
if err = config.Parse(&c); err != nil {
return nil, err
}
c.SetDefault()
c.SetEnvironmentOverrides()
return c, nil
}
// RewriteConfig rewrite config file path
func RewriteConfig(configFilePath string, allConfig *AllConfig) error {
buf := bytes.Buffer{}
enc := yaml.NewEncoder(&buf)
defer enc.Close()
enc.SetIndent(2)
if err := enc.Encode(allConfig); err != nil {
return err
}
return writer.ReplaceFile(configFilePath, buf.String())
}