diff --git a/.github/workflows/test-api.yml b/.github/workflows/test-api.yml index 5678d5f8b7..cd4b308384 100644 --- a/.github/workflows/test-api.yml +++ b/.github/workflows/test-api.yml @@ -49,3 +49,19 @@ jobs: run: | export APIX_ETCD_ENDPOINTS=127.0.0.1:2379 go test ./... + + - name: run with custom port + working-directory: ./api + run: | + sed -i 's/8080/\# 8088/' conf/conf.json + go run main.go & + + - name: run with custom port + working-directory: ./api + run: | + curl http://127.0.0.1:8088/apisix/admin/user/login -X POST -i -d '{"username":"admin", "password": "admin"}' + code=$(curl -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:8088/apisix/admin/user/login -X POST -i -d '{"username":"admin", "password": "admin"}') + if [ ! $code -eq 200 ]; then + echo "failed: failed to custom port" + exit 1 + fi diff --git a/api/conf/conf.go b/api/conf/conf.go index 184613d637..11ff7733d9 100644 --- a/api/conf/conf.go +++ b/api/conf/conf.go @@ -29,8 +29,7 @@ import ( ) const ( - ServerPort = 8080 - WebDir = "./dist" + WebDir = "./dist" EnvPROD = "prod" EnvBETA = "beta" @@ -42,16 +41,50 @@ const ( ) var ( - ENV string - basePath string - Schema gjson.Result - DagLibPath = "/go/manager-api/dag-to-lua/" + ENV string + basePath string + Schema gjson.Result + DagLibPath = "/go/manager-api/dag-to-lua/" + ServerHost = "127.0.0.1" + ServerPort = 80 + ETCDEndpoints = "127.0.0.1:2379" ) func init() { setEnvironment() initAuthentication() initSchema() + setConf() +} + +func setConf() { + filePath := configurationPath() + if configurationContent, err := ioutil.ReadFile(filePath); err != nil { + panic(fmt.Sprintf("fail to read configuration: %s", filePath)) + } else { + configuration := gjson.ParseBytes(configurationContent) + //listen + serverPort := int(configuration.Get("conf.listen.port").Int()) + if serverPort != 0 { + ServerPort = serverPort + } + serverHost := configuration.Get("conf.listen.host").String() + if serverHost != "" { + ServerHost = serverHost + } + + //dag lib path + dagLibPath := configuration.Get("conf.dag-lib-path").String() + if dagLibPath != "" { + DagLibPath = dagLibPath + } + //etcd + eTCDEndpoints := configuration.Get("conf.etcd.endpoints").String() + if eTCDEndpoints != "" { + ETCDEndpoints = eTCDEndpoints + } + + } } func setEnvironment() { @@ -61,10 +94,6 @@ func setEnvironment() { ENV = env } - if env := os.Getenv("APIX_DAG_LIB_PATH"); env != "" { - DagLibPath = env - } - _, basePath, _, _ = runtime.Caller(1) } diff --git a/api/conf/conf.json b/api/conf/conf.json index d00c492c29..c369afe93c 100644 --- a/api/conf/conf.json +++ b/api/conf/conf.json @@ -2,6 +2,14 @@ "conf": { "syslog": { "host": "127.0.0.1" + }, + "listen": { + "host": "127.0.0.1", + "port": 8080 + }, + "dag-lib-path": "", + "etcd": { + "endpoints": "127.0.0.1:2379" } }, "authentication": { diff --git a/api/main.go b/api/main.go index 2e24fd2e18..1664567977 100644 --- a/api/main.go +++ b/api/main.go @@ -18,8 +18,8 @@ package main import ( "fmt" + "log" "net/http" - "os" "strings" "time" @@ -30,14 +30,14 @@ import ( "github.com/apisix/manager-api/internal/core/storage" "github.com/apisix/manager-api/internal/core/store" "github.com/apisix/manager-api/internal/utils" - "github.com/apisix/manager-api/log" + alog "github.com/apisix/manager-api/log" ) -var logger = log.GetLogger() +var logger = alog.GetLogger() func main() { - dlog.DefLogger = log.DefLogger{} - if err := storage.InitETCDClient(strings.Split(os.Getenv("APIX_ETCD_ENDPOINTS"), ",")); err != nil { + dlog.DefLogger = alog.DefLogger{} + if err := storage.InitETCDClient(strings.Split(conf.ETCDEndpoints, ",")); err != nil { panic(err) } if err := store.InitStores(); err != nil { @@ -45,13 +45,16 @@ func main() { } // routes r := internal.SetUpRouter() - addr := fmt.Sprintf(":%d", conf.ServerPort) + addr := fmt.Sprintf("%s:%d", conf.ServerHost, conf.ServerPort) s := &http.Server{ Addr: addr, Handler: r, ReadTimeout: time.Duration(1000) * time.Millisecond, WriteTimeout: time.Duration(5000) * time.Millisecond, } + + log.Printf("The Manager API is listening on %s ", addr) + if err := s.ListenAndServe(); err != nil { logger.WithError(err) } diff --git a/api/run.sh b/api/run.sh index deced4a70b..40fd4a7074 100755 --- a/api/run.sh +++ b/api/run.sh @@ -19,8 +19,5 @@ export ENV=local pwd=`pwd` -# config -export APIX_DAG_LIB_PATH="${pwd}/dag-to-lua-1.1/lib/" -export APIX_ETCD_ENDPOINTS="127.0.0.1:2379" exec ./manager-api diff --git a/docs/deploy.md b/docs/deploy.md index 33229864ad..c0226d565b 100644 --- a/docs/deploy.md +++ b/docs/deploy.md @@ -77,13 +77,42 @@ $ yarn build ## Run -1. According to your local deployment environment, check the environment variables in `./api/run.sh`, modify the environment variables if needed. For example, change the ETCD endpoints to your ETCD instances work with APISIX: - -```sh -$ export APIX_ETCD_ENDPOINTS="127.0.0.1:2379" - -# If you have multiple instances, please use commas to separate: -$ export APIX_ETCD_ENDPOINTS="127.0.0.1:2379,127.0.0.1:3379" +1. According to your local deployment environment, check the related configurations in `./conf/conf.json`, modify those variables if needed. + +Example: + +```json +{ + "conf": { + "syslog": { + "host": "127.0.0.1" + }, + "listen": { + "host": "127.0.0.1", + "port": 8080 + }, + "dag-lib-path": "/home/www/workspace/apisix-dashboard/dag-to-lua-1.1/", + "etcd": { + "endpoints": "127.0.0.1:2379" + } + }, + "authentication": { + "session": { + "secret": "secret", + "expireTime": 3600 + }, + "user": [ + { + "username": "admin", + "password": "admin" + }, + { + "username": "user", + "password": "user" + } + ] + } +} ``` 2. Run manager-api @@ -103,3 +132,15 @@ $ ps aux | grep manager-api $ kill $process_id ``` + +2. If you have multiple ETCD instances, please use commas to separate each endpoint in `/conf/conf.json`. + +Example: + +```json +{ + "etcd": { + "endpoints": "127.0.0.1:2379,127.0.0.1:3379" + } +} +``` diff --git a/docs/deploy.zh-CN.md b/docs/deploy.zh-CN.md index 5c875b3a5e..41ee3906ca 100644 --- a/docs/deploy.zh-CN.md +++ b/docs/deploy.zh-CN.md @@ -79,13 +79,42 @@ $ yarn build ## 启动 -1. 根据您的本地部署环境,检查 `./api/run.sh` 中的环境变量,如果需要请修改环境变量。例如:把 ETCD 地址改为与你的 Apache APISIX 一起工作的 ETCD 实例: - -```sh -$ export APIX_ETCD_ENDPOINTS="127.0.0.1:2379" - -# 如果有多个实例,请使用英文逗号分隔,如: -$ export APIX_ETCD_ENDPOINTS="127.0.0.1:2379,127.0.0.1:3379" +1. 根据您的本地部署环境,检查并修改 `./conf/conf.json` 中的配置。 + +例如: + +```json +{ + "conf": { + "syslog": { + "host": "127.0.0.1" + }, + "listen": { + "host": "127.0.0.1", + "port": 8080 + }, + "dag-lib-path": "/home/www/workspace/apisix-dashboard/dag-to-lua-1.1/", + "etcd": { + "endpoints": "127.0.0.1:2379" + } + }, + "authentication": { + "session": { + "secret": "secret", + "expireTime": 3600 + }, + "user": [ + { + "username": "admin", + "password": "admin" + }, + { + "username": "user", + "password": "user" + } + ] + } +} ``` 2. 启动 manager-api @@ -105,3 +134,15 @@ $ ps aux | grep manager-api $ kill $process_id ``` + +2. 若您有多个 ETCD 地址,请修改 `./conf/conf.json` 中的相关配置。 + +例如: + +```json +{ + "etcd": { + "endpoints": "127.0.0.1:2379,127.0.0.1:3379" + } +} +```