Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor conf of manager api #693

Merged
merged 22 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .github/workflows/e2e-test-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: install runtime
run: |
sudo apt-get update
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
export GO111MOUDULE=on
sudo apt install golang-1.14-go
- name: setup go
uses: actions/setup-go@v1
with:
go-version: '1.13'

- name: run docker compose
working-directory: ./api/test/docker
Expand Down
21 changes: 11 additions & 10 deletions .github/workflows/test-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ jobs:
tar -zxvf v1.1.tar.gz
sudo mv ./dag-to-lua-1.1/lib/* /go/manager-api/dag-to-lua/

- name: install runtime
run: |
sudo apt-get update
sudo apt-get install lua5.1
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
export GO111MOUDULE=on
sudo apt install golang-1.14-go
- name: setup go
uses: actions/setup-go@v1
with:
go-version: '1.13'

- name: setup lua
uses: leafo/gh-actions-lua@v7
with:
luaVersion: "5.1.5"

- name: run test
run: |
Expand All @@ -55,12 +56,12 @@ jobs:
run: |
export GO111MOUDULE=on
export APISIX_CONF_PATH=$PWD/conf
sed -i 's/8080/8088/' conf/conf.json
sed -i 's/8080/8088/' conf/conf.yaml
go build -o ./manager-api
./manager-api > ./api.log 2>&1 &
sleep 2
cat ./api.log
cat conf/conf.json
cat conf/conf.yaml

- name: run with custom port
working-directory: ./api
Expand Down
57 changes: 23 additions & 34 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,34 @@

FROM golang:1.13.8 AS build-env

WORKDIR /go/src/github.com/apisix/manager-api
RUN mkdir -p /usr/local/apisix-dashboard/api
nic-chen marked this conversation as resolved.
Show resolved Hide resolved

WORKDIR /usr/local/apisix-dashboard/api

COPY . .
RUN mkdir -p /go/manager-api \
&& mkdir -p /go/manager-api/build-tools \

RUN mkdir -p ../output/conf \
&& cp ./conf/* ../output/conf

RUN wget https://github.com/api7/dag-to-lua/archive/v1.1.tar.gz -O /tmp/v1.1.tar.gz \
membphis marked this conversation as resolved.
Show resolved Hide resolved
&& mkdir /tmp/dag-to-lua \
&& tar -xvf /tmp/v1.1.tar.gz -C /tmp/dag-to-lua --strip 1 \
&& mkdir -p ../output/dag-to-lua \
&& mv /tmp/dag-to-lua/lib/* ../output/dag-to-lua/

RUN go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.io,direct \
&& export GOPROXY=https://goproxy.io \
&& go build -o /go/manager-api/manager-api \
&& mv /go/src/github.com/apisix/manager-api/entry.sh /go/manager-api/ \
&& mv /go/src/github.com/apisix/manager-api/build-tools/* /go/manager-api/build-tools/ \
&& mv /go/src/github.com/apisix/manager-api/conf/conf_preview.json /go/manager-api/conf.json \
&& mv /go/src/github.com/apisix/manager-api/conf/schema.json /go/manager-api/schema.json \
&& rm -rf /go/src/github.com/apisix/manager-api \
&& rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Hongkong /etc/localtime \
&& dpkg-reconfigure -f noninteractive tzdata

RUN wget https://github.com/api7/dag-to-lua/archive/v1.1.tar.gz \
&& tar -zxvf v1.1.tar.gz \
&& mkdir -p /go/manager-api/dag-to-lua \
&& mv -u ./dag-to-lua-1.1/lib/* /go/manager-api/dag-to-lua/

FROM alpine:3.11

RUN mkdir -p /go/manager-api \
&& apk update \
&& apk add ca-certificates \
&& update-ca-certificates \
&& apk add --no-cache libc6-compat \
&& echo "hosts: files dns" > /etc/nsswitch.conf \
&& rm -rf /var/cache/apk/*
&& CGO_ENABLED=0 go build -o ../output/manager-api .


FROM alpine:latest as prod

RUN apk add lua5.1

WORKDIR /go/manager-api
COPY --from=build-env /go/manager-api/ /go/manager-api/
COPY --from=build-env /usr/share/zoneinfo/Hongkong /etc/localtime
WORKDIR /usr/local/apisix-dashboard

EXPOSE 8080
COPY --from=build-env /usr/local/apisix-dashboard/output/ ./

RUN chmod +x ./entry.sh
EXPOSE 8080

CMD ["/bin/ash", "-c", "/go/manager-api/entry.sh"]
CMD [ "/usr/local/apisix-dashboard/manager-api" ]
2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ fi
cd ./api && go build -o ../output/manager-api . && cd ..

cp ./api/conf/schema.json ./output/conf/schema.json
cp ./api/conf/conf.json ./output/conf/conf.json
cp ./api/conf/conf.yaml ./output/conf/conf.yaml

echo "Build the Manager API successfully"
164 changes: 77 additions & 87 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,147 +17,137 @@
package conf

import (
"flag"
"fmt"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"

"github.com/tidwall/gjson"

"github.com/apisix/manager-api/internal/utils"
"strings"
)

const (
WebDir = "./output/html"

EnvPROD = "prod"
EnvBETA = "beta"
EnvDEV = "dev"
EnvLOCAL = "local"

confJsonPath = "/go/manager-api/conf.json"
schemaPath = "/go/manager-api/schema.json"
WebDir = "./html/"
)

var (
ENV string
basePath string
Schema gjson.Result
DagLibPath = "/go/manager-api/dag-to-lua/"
confDir = "./conf/"
DagLibPath = "./dag-to-lua/"
ServerHost = "127.0.0.1"
ServerPort = 80
ETCDEndpoints = "127.0.0.1:2379"
ETCDEndpoints = []string{"127.0.0.1:2379"}
UserList = make(map[string]User, 2)
AuthConf Authentication
)

type Etcd struct {
Endpoints []string
}

type Listen struct {
Host string
Port int
}

type Conf struct {
DagLib string `yaml:"dag_lib_path"`
Etcd Etcd
Listen Listen
}

type User struct {
Username string
Password string
}

type Authentication struct {
Secret string
ExpireTime int
Users []User
}

type Config struct {
Conf Conf
Authentication Authentication
}

func init() {
//go test
if strings.HasSuffix(os.Args[0], ".test") {
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
_, basePath, _, _ := runtime.Caller(0)
confDir = filepath.Dir(basePath) + "/"
fmt.Println("confDir:", confDir)
} else {
flag.StringVar(&confDir, "c", "./conf/", "conf dir")
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
flag.Parse()
}

setConf()
setEnvironment()
initAuthentication()
initSchema()
setConf()
}

func setConf() {
filePath := configurationPath()
filePath := confDir + "conf.yaml"
if configurationContent, err := ioutil.ReadFile(filePath); err != nil {
panic(fmt.Sprintf("fail to read configuration: %s", filePath))
} else {
configuration := gjson.ParseBytes(configurationContent)
//configuration := gjson.ParseBytes(configurationContent)
config := Config{}
err := yaml.Unmarshal(configurationContent, &config)
if err != nil {
log.Printf("conf: %s, error: %v", configurationContent, err)
}
//listen
serverPort := int(configuration.Get("conf.listen.port").Int())
if serverPort != 0 {
ServerPort = serverPort
if config.Conf.Listen.Port != 0 {
ServerPort = config.Conf.Listen.Port
}
serverHost := configuration.Get("conf.listen.host").String()
if serverHost != "" {
ServerHost = serverHost
if config.Conf.Listen.Host != "" {
ServerHost = config.Conf.Listen.Host
}

//dag lib path
dagLibPath := configuration.Get("conf.dag-lib-path").String()
if dagLibPath != "" {
DagLibPath = dagLibPath
if config.Conf.DagLib != "" {
DagLibPath = config.Conf.DagLib
}
//etcd
eTCDEndpoints := configuration.Get("conf.etcd.endpoints").String()
if eTCDEndpoints != "" {
ETCDEndpoints = eTCDEndpoints
if len(config.Conf.Etcd.Endpoints) > 0 {
ETCDEndpoints = config.Conf.Etcd.Endpoints
}

//auth
initAuthentication(config.Authentication)
}
}

func setEnvironment() {
if env := os.Getenv("ENV"); env == "" {
ENV = EnvLOCAL
} else {
ENV = EnvPROD
if env := os.Getenv("ENV"); env != "" {
ENV = env
}

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

func configurationPath() string {
if confPath := os.Getenv("APISIX_CONF_PATH"); confPath != "" {
return filepath.Join(confPath, "/conf.json")
} else if ENV == EnvLOCAL {
return filepath.Join(filepath.Dir(basePath), "conf.json")
} else {
return confJsonPath
}
}

func getSchemaPath() string {
if confPath := os.Getenv("APISIX_CONF_PATH"); confPath != "" {
return filepath.Join(confPath, "/schema.json")
} else if ENV == EnvLOCAL {
return filepath.Join(filepath.Dir(basePath), "schema.json")
} else {
return schemaPath
}
}

type user struct {
Username string
Password string
}

type authenticationConfig struct {
Session struct {
Secret string
ExpireTime uint64
}
}

var UserList = make(map[string]user, 1)

var AuthenticationConfig authenticationConfig

func initAuthentication() {
filePath := configurationPath()
configurationContent, err := ioutil.ReadFile(filePath)
if err != nil {
panic(fmt.Sprintf("fail to read configuration: %s", filePath))
}

configuration := gjson.ParseBytes(configurationContent)
userList := configuration.Get("authentication.user").Array()
func initAuthentication(conf Authentication) {
AuthConf = conf
userList := conf.Users
// create user list
for _, item := range userList {
username := item.Map()["username"].String()
password := item.Map()["password"].String()
UserList[item.Map()["username"].String()] = user{Username: username, Password: password}
}
AuthenticationConfig.Session.Secret = configuration.Get("authentication.session.secret").String()
if "secret" == AuthenticationConfig.Session.Secret {
AuthenticationConfig.Session.Secret = utils.GetFlakeUidStr()
UserList[item.Username] = item
}

AuthenticationConfig.Session.ExpireTime = configuration.Get("authentication.session.expireTime").Uint()
}

func initSchema() {
filePath := getSchemaPath()
filePath := confDir + "schema.json"
if schemaContent, err := ioutil.ReadFile(filePath); err != nil {
panic(fmt.Sprintf("fail to read configuration: %s", filePath))
} else {
Expand Down
37 changes: 37 additions & 0 deletions api/conf/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# PLEASE DO NOT UPDATE THIS FILE!
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
# If you want to set the specified configuration value, you can set the new
# value in the conf/config.yaml file.
#

conf:
listen:
host: 127.0.0.1
membphis marked this conversation as resolved.
Show resolved Hide resolved
port: 8080
dag_lib_path: ''
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
etcd:
endpoints:
- 127.0.0.1:2379
authentication:
secret: secret
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
expireTime: 3600
users:
- username: admin
password: admin
- username: user
password: user
Loading