diff --git a/api/build.sh b/api/build.sh index f3b9652a14..4df783f49a 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 de599c0d02..b0a7328e75 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 6d0d20b3de..ea3ddc119a 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 ef903eca20..be1d3a834c --- 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