This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 88dadc2  feat: support APISIX_PROFILE for env-specific configuration 
(#2293)
88dadc2 is described below

commit 88dadc2024d95b5ce94eb953f8dd564a7b28010f
Author: Zeping Bai <[email protected]>
AuthorDate: Sun Feb 6 20:13:40 2022 +0800

    feat: support APISIX_PROFILE for env-specific configuration (#2293)
---
 api/build.sh                       |  2 +-
 api/internal/conf/conf.go          | 12 ++++++------
 api/internal/core/server/server.go |  3 +++
 api/test/shell/cli_test.sh         | 38 ++++++++++++++++++++++++++++++++++++--
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/api/build.sh b/api/build.sh
index f3b9652..4df783f 100755
--- a/api/build.sh
+++ b/api/build.sh
@@ -45,6 +45,6 @@ fi
 cd ./api && go build -o ../output/manager-api -ldflags "${GOLDFLAGS}" 
./main.go && cd ..
 
 cp ./api/conf/schema.json ./output/conf/schema.json
-cp ./api/conf/conf.yaml ./output/conf/conf.yaml
+cp ./api/conf/conf*.yaml ./output/conf/
 
 echo "Build the Manager API successfully"
diff --git a/api/internal/conf/conf.go b/api/internal/conf/conf.go
index de599c0..b0a7328 100644
--- a/api/internal/conf/conf.go
+++ b/api/internal/conf/conf.go
@@ -151,7 +151,11 @@ func InitConf() {
 func setupConfig() {
        // setup config file path
        if ConfigFile == "" {
-               viper.SetConfigName("conf")
+               ConfigFile = "conf.yaml"
+               if profile := os.Getenv("APISIX_PROFILE"); profile != "" {
+                       ConfigFile = "conf" + "-" + profile + ".yaml"
+               }
+               viper.SetConfigName(ConfigFile)
                viper.SetConfigType("yaml")
                viper.AddConfigPath(WorkDir + "/conf")
        } else {
@@ -160,11 +164,7 @@ func setupConfig() {
 
        // load config
        if err := viper.ReadInConfig(); err != nil {
-               if _, ok := err.(viper.ConfigFileNotFoundError); ok {
-                       panic(fmt.Sprintf("fail to find configuration: %s", 
ConfigFile))
-               } else {
-                       panic(fmt.Sprintf("fail to read configuration: %s, err: 
%s", ConfigFile, err.Error()))
-               }
+               panic(fmt.Sprintf("fail to read configuration, err: %s", 
err.Error()))
        }
 
        // unmarshal config
diff --git a/api/internal/core/server/server.go 
b/api/internal/core/server/server.go
index 6d0d20b..ea3ddc1 100644
--- a/api/internal/core/server/server.go
+++ b/api/internal/core/server/server.go
@@ -23,6 +23,8 @@ import (
        "os"
        "time"
 
+       "github.com/spf13/viper"
+
        "github.com/apisix/manager-api/internal/conf"
        "github.com/apisix/manager-api/internal/log"
        "github.com/apisix/manager-api/internal/utils"
@@ -109,6 +111,7 @@ func (s *server) shutdownServer(server *http.Server) {
 func (s *server) printInfo() {
        fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
        utils.PrintVersion()
+       fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Config File", 
viper.ConfigFileUsed())
        fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, 
conf.ServerPort)
        if conf.SSLCert != "" && conf.SSLKey != "" {
                fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "HTTPS Listen", 
conf.SSLHost, conf.SSLPort)
diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh
old mode 100644
new mode 100755
index ef903ec..be1d3a8
--- a/api/test/shell/cli_test.sh
+++ b/api/test/shell/cli_test.sh
@@ -31,6 +31,7 @@
 VERSION=$(cat ./VERSION)
 KERNEL=$(uname -s)
 CONF_FILE="/usr/local/apisix-dashboard/conf/conf.yaml"
+APISIX_PROFILE_CONF_FILE="/usr/local/apisix-dashboard/conf/conf-test.yaml"
 LOG_FILE="/usr/local/apisix-dashboard/logs/error.log"
 ACCESS_LOG_FILE="/usr/local/apisix-dashboard/logs/access.log"
 SERVICE_NAME="apisix-dashboard"
@@ -47,6 +48,7 @@ fi
 
 recover_conf() {
   run cp -rf ./conf/conf.yaml ${CONF_FILE}
+  run cp -rf ./conf/conf.yaml ${APISIX_PROFILE_CONF_FILE}
   [ "$status" -eq 0 ]
 }
 check_logfile() {
@@ -56,6 +58,12 @@ clean_logfile() {
   echo > $LOG_FILE
 }
 
+recover_service_file() {
+  run cp -f ./service/apisix-dashboard.service 
/usr/lib/systemd/system/${SERVICE_NAME}.service
+  run systemctl daemon-reload
+  [ "$status" -eq 0 ]
+}
+
 start_dashboard() {
   run systemctl start ${SERVICE_NAME}
   [ "$status" -eq 0 ]
@@ -99,7 +107,7 @@ stop_dashboard() {
 }
 
 #2
-@test "Check info log leve and signal" {
+@test "Check info log level and signal" {
   if [[ $KERNEL = "Darwin" ]]; then
     sed -i "" 's/level: warn/level: info/' ${CONF_FILE}
   else
@@ -241,7 +249,7 @@ stop_dashboard() {
   recover_conf
 
   # add root user
-  curl -L http://localhost:2379/v3/auth/user/add -d '{"name": "root", 
"password": "root"}'
+  curl -L http://localhost:2379/v3/auth/user/add -X POST -d '{"name": "root", 
"password": "root"}'
 
   # add root role
   curl -L http://localhost:2379/v3/auth/role/add -d '{"name": "root"}'
@@ -420,6 +428,32 @@ stop_dashboard() {
   stop_dashboard 6
 }
 
+
+#14
+@test "Check APISIX_PROFILE" {
+  recover_conf
+
+  start_dashboard 3
+
+  run journalctl -u ${SERVICE_NAME}.service -n 30
+  [ $(echo "$output" | grep -c "conf.yaml") -eq '1' ]
+
+  stop_dashboard 3
+
+  sed -i 's#-c /usr/local/apisix-dashboard/conf/conf.yaml##g' 
/usr/lib/systemd/system/${SERVICE_NAME}.service
+  sed -i '$a\Environment=APISIX_PROFILE=test' 
/usr/lib/systemd/system/${SERVICE_NAME}.service
+  run systemctl daemon-reload
+
+  start_dashboard 3
+
+  run journalctl -u ${SERVICE_NAME}.service -n 30
+  [ $(echo "$output" | grep -c "conf-test.yaml") -eq '1' ]
+
+  stop_dashboard 3
+
+  recover_service_file
+}
+
 #post
 @test "Clean test environment" {
   # kill etcd

Reply via email to