Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

conf.go: Added an option for environment variables ovveride

EkkoKo0183875742b6
diff --git a/internal/base/conf/conf.go b/internal/base/conf/conf.go
index 67e02340..4261d74d 100644
--- a/internal/base/conf/conf.go
+++ b/internal/base/conf/conf.go
@@ -21,6 +21,7 @@ package conf
import (
"bytes"
+ "os"
"path/filepath"
"github.com/apache/incubator-answer/internal/base/data"
@@ -45,6 +46,21 @@ type AllConfig struct {
UI *server.UI `json:"ui" mapstructure:"ui" yaml:"ui"`
}
+type EnvConfigOverrides struct {
+ SWAGGER_HOST string
+ SWAGGER_ADDRESS_PORT string
+
+ SITE_ADDR string
+}
+
+func loadEnvs() (envOverrides *EnvConfigOverrides) {
+ return &EnvConfigOverrides{
+ SWAGGER_HOST: os.Getenv("SWAGGER_HOST"),
+ SWAGGER_ADDRESS_PORT: os.Getenv("SWAGGER_ADDRESS_PORT"),
+ SITE_ADDR: os.Getenv("SITE_ADDR"),
+ }
+}
+
type PathIgnore struct {
Users []string `yaml:"users"`
}
@@ -67,6 +83,19 @@ func (c *AllConfig) SetDefault() {
}
}
+func (c *AllConfig) SetEnvironmentOverrides() {
+ envs := loadEnvs()
+ if envs.SITE_ADDR != "" {
+ c.Server.HTTP.Addr = envs.SITE_ADDR
+ }
+ if envs.SWAGGER_HOST != "" {
+ c.Swaggerui.Host = envs.SWAGGER_HOST
+ }
+ if envs.SWAGGER_ADDRESS_PORT != "" {
+ c.Swaggerui.Address = envs.SWAGGER_ADDRESS_PORT
+ }
+}
+
// ReadConfig read config
func ReadConfig(configFilePath string) (c *AllConfig, err error) {
if len(configFilePath) == 0 {
@@ -81,6 +110,7 @@ func ReadConfig(configFilePath string) (c *AllConfig, err error) {
return nil, err
}
c.SetDefault()
+ c.SetEnvironmentOverrides()
return c, nil
}