Skip to content

Commit 92354ed

Browse files
authored
feat: 增加几个重要配置支持通过环境变量加载的方式 (opsre#326)
1 parent cb5f002 commit 92354ed

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ WORKDIR /app
44

55
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories \
66
&& apk upgrade && apk add --no-cache --virtual .build-deps \
7-
ca-certificates gcc g++ curl
7+
ca-certificates gcc g++ curl upx
88

99
ADD . .
1010

1111
RUN release_url=$(curl -s https://api.github.com/repos/eryajf/go-ldap-admin-ui/releases/latest | grep "browser_download_url" | grep -v 'dist.zip.md5' | cut -d '"' -f 4); wget $release_url && unzip dist.zip && rm dist.zip && mv dist public/static
1212

1313
RUN sed -i 's@localhost:389@openldap:389@g' /app/config.yml \
14-
&& sed -i 's@host: localhost@host: mysql@g' /app/config.yml && go build -o go-ldap-admin .
14+
&& sed -i 's@host: localhost@host: mysql@g' /app/config.yml && go build -o go-ldap-admin . && upx -9 go-ldap-admin
1515

1616
### build final image
1717
FROM registry.cn-hangzhou.aliyuncs.com/ali_eryajf/alpine:3.19

config/config.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"fmt"
66
"os"
7+
"strconv"
78

89
"github.com/fsnotify/fsnotify"
910
"github.com/spf13/viper"
@@ -72,6 +73,65 @@ func InitConfig() {
7273
Conf.System.RSAPublicBytes = pub
7374
Conf.System.RSAPrivateBytes = priv
7475

76+
// 部分配合通过环境变量加载
77+
dbDriver := os.Getenv("DB_DRIVER")
78+
if dbDriver != "" {
79+
Conf.Database.Driver = dbDriver
80+
}
81+
mysqlHost := os.Getenv("MYSQL_HOST")
82+
if mysqlHost != "" {
83+
Conf.Mysql.Host = mysqlHost
84+
}
85+
mysqlUsername := os.Getenv("MYSQL_USERNAME")
86+
if mysqlUsername != "" {
87+
Conf.Mysql.Username = mysqlUsername
88+
}
89+
mysqlPassword := os.Getenv("MYSQL_PASSWORD")
90+
if mysqlPassword != "" {
91+
Conf.Mysql.Password = mysqlPassword
92+
}
93+
mysqlDatabase := os.Getenv("MYSQL_DATABASE")
94+
if mysqlDatabase != "" {
95+
Conf.Mysql.Database = mysqlDatabase
96+
}
97+
mysqlPort := os.Getenv("MYSQL_PORT")
98+
if mysqlPort != "" {
99+
Conf.Mysql.Port, _ = strconv.Atoi(mysqlPort)
100+
}
101+
102+
ldapUrl := os.Getenv("LDAP_URL")
103+
if ldapUrl != "" {
104+
Conf.Ldap.Url = ldapUrl
105+
}
106+
ldapBaseDN := os.Getenv("LDAP_BASE_DN")
107+
if ldapBaseDN != "" {
108+
Conf.Ldap.BaseDN = ldapBaseDN
109+
}
110+
ldapAdminDN := os.Getenv("LDAP_ADMIN_DN")
111+
if ldapAdminDN != "" {
112+
Conf.Ldap.AdminDN = ldapAdminDN
113+
}
114+
ldapAdminPass := os.Getenv("LDAP_ADMIN_PASS")
115+
if ldapAdminPass != "" {
116+
Conf.Ldap.AdminPass = ldapAdminPass
117+
}
118+
ldapUserDN := os.Getenv("LDAP_USER_DN")
119+
if ldapUserDN != "" {
120+
Conf.Ldap.UserDN = ldapUserDN
121+
}
122+
ldapUserInitPassword := os.Getenv("LDAP_USER_INIT_PASSWORD")
123+
if ldapUserInitPassword != "" {
124+
125+
Conf.Ldap.UserInitPassword = ldapUserInitPassword
126+
}
127+
ldapDefaultEmailSuffix := os.Getenv("LDAP_DEFAULT_EMAIL_SUFFIX")
128+
if ldapDefaultEmailSuffix != "" {
129+
Conf.Ldap.DefaultEmailSuffix = ldapDefaultEmailSuffix
130+
}
131+
ldapUserPasswordEncryptionType := os.Getenv("LDAP_USER_PASSWORD_ENCRYPTION_TYPE")
132+
if ldapUserPasswordEncryptionType != "" {
133+
Conf.Ldap.UserPasswordEncryptionType = ldapUserPasswordEncryptionType
134+
}
75135
}
76136

77137
type SystemConfig struct {

0 commit comments

Comments
 (0)