Skip to content

Commit

Permalink
feat: support custom server host, port and DAG lib path (#625)
Browse files Browse the repository at this point in the history
* feat: support custom server port and dag lib path

* test: custom host

* test: add test case

* fix bug

* test

* test

* test

* feat support config etcd endpoints

* Update conf.json

* Update conf.go

* Update test-api.yml

* Update deploy.md

* Update deploy.zh-CN.md

* Update deploy.md

* Update conf.json

Co-authored-by: 琚致远 <juzhiyuan@apache.org>
  • Loading branch information
nic-chen and juzhiyuan authored Oct 29, 2020
1 parent 1e10e8d commit 0c26e2e
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 33 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 39 additions & 10 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
)

const (
ServerPort = 8080
WebDir = "./dist"
WebDir = "./dist"

EnvPROD = "prod"
EnvBETA = "beta"
Expand All @@ -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() {
Expand All @@ -61,10 +94,6 @@ func setEnvironment() {
ENV = env
}

if env := os.Getenv("APIX_DAG_LIB_PATH"); env != "" {
DagLibPath = env
}

_, basePath, _, _ = runtime.Caller(1)
}

Expand Down
8 changes: 8 additions & 0 deletions api/conf/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 9 additions & 6 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package main

import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"

Expand All @@ -30,28 +30,31 @@ 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 {
panic(err)
}
// 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)
}
Expand Down
3 changes: 0 additions & 3 deletions api/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 48 additions & 7 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
}
```
55 changes: 48 additions & 7 deletions docs/deploy.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
}
```

0 comments on commit 0c26e2e

Please sign in to comment.