Skip to content

Commit

Permalink
feat: 增加几个重要配置支持通过环境变量加载的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf committed Apr 4, 2024
1 parent 103d765 commit 09d857f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ WORKDIR /app

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

ADD . .

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

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

### build final image
FROM registry.cn-hangzhou.aliyuncs.com/ali_eryajf/alpine:3.19
Expand Down
60 changes: 60 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"os"
"strconv"

"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
Expand Down Expand Up @@ -72,6 +73,65 @@ func InitConfig() {
Conf.System.RSAPublicBytes = pub
Conf.System.RSAPrivateBytes = priv

// 部分配合通过环境变量加载
dbDriver := os.Getenv("DB_DRIVER")
if dbDriver != "" {
Conf.Database.Driver = dbDriver
}
mysqlHost := os.Getenv("MYSQL_HOST")
if mysqlHost != "" {
Conf.Mysql.Host = mysqlHost
}
mysqlUsername := os.Getenv("MYSQL_USERNAME")
if mysqlUsername != "" {
Conf.Mysql.Username = mysqlUsername
}
mysqlPassword := os.Getenv("MYSQL_PASSWORD")
if mysqlPassword != "" {
Conf.Mysql.Password = mysqlPassword
}
mysqlDatabase := os.Getenv("MYSQL_DATABASE")
if mysqlDatabase != "" {
Conf.Mysql.Database = mysqlDatabase
}
mysqlPort := os.Getenv("MYSQL_PORT")
if mysqlPort != "" {
Conf.Mysql.Port, _ = strconv.Atoi(mysqlPort)
}

ldapUrl := os.Getenv("LDAP_URL")
if ldapUrl != "" {
Conf.Ldap.Url = ldapUrl
}
ldapBaseDN := os.Getenv("LDAP_BASE_DN")
if ldapBaseDN != "" {
Conf.Ldap.BaseDN = ldapBaseDN
}
ldapAdminDN := os.Getenv("LDAP_ADMIN_DN")
if ldapAdminDN != "" {
Conf.Ldap.AdminDN = ldapAdminDN
}
ldapAdminPass := os.Getenv("LDAP_ADMIN_PASS")
if ldapAdminPass != "" {
Conf.Ldap.AdminPass = ldapAdminPass
}
ldapUserDN := os.Getenv("LDAP_USER_DN")
if ldapUserDN != "" {
Conf.Ldap.UserDN = ldapUserDN
}
ldapUserInitPassword := os.Getenv("LDAP_USER_INIT_PASSWORD")
if ldapUserInitPassword != "" {

Conf.Ldap.UserInitPassword = ldapUserInitPassword
}
ldapDefaultEmailSuffix := os.Getenv("LDAP_DEFAULT_EMAIL_SUFFIX")
if ldapDefaultEmailSuffix != "" {
Conf.Ldap.DefaultEmailSuffix = ldapDefaultEmailSuffix
}
ldapUserPasswordEncryptionType := os.Getenv("LDAP_USER_PASSWORD_ENCRYPTION_TYPE")
if ldapUserPasswordEncryptionType != "" {
Conf.Ldap.UserPasswordEncryptionType = ldapUserPasswordEncryptionType
}
}

type SystemConfig struct {
Expand Down

0 comments on commit 09d857f

Please sign in to comment.