Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions .github/apisix-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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.
#
# If you want to set the specified configuration value, you can set the new
# in this file. For example if you want to specify the etcd address:
#

etcd:
host:
- "http://etcd:2379"

apisix:
allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 0.0.0.0/0 # If we don't set any IP list, then any IP access is allowed by default.
40 changes: 29 additions & 11 deletions .github/workflows/api_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ jobs:
env:
ALLOW_NONE_AUTHENTICATION: yes

apisix:
image: johz/apisix:v1.4-github-action
env:
APISIX_ETCD_HOST: http://etcd:2379
ports:
- 9080:9080

mysql:
image: mysql:8.0
env:
Expand All @@ -43,14 +36,39 @@ jobs:

- uses: actions/checkout@v2

- name: run apisix
run: |
network=$(docker network ls | grep github_network | awk '{print $2}')
docker run --name apisix -d -p 9080:9080 \
-v ${{ github.workspace }}/.github/apisix-config.yaml:/usr/local/apisix/conf/config.yaml \
--network "$network" --network-alias apisix \
apache/apisix:dev
sleep 5
docker logs apisix

- name: setting up database
run: |
mysql -h 127.0.0.1 --port 3306 -u root -p123456 < ./api/script/db/schema.sql

- name: setup go
uses: actions/setup-go@v1
with:
go-version: '1.13'
- name: ping apisix
run: |
curl 127.0.0.1:9080

- name: get lua lib
run: |
wget https://github.com/api7/dag-to-lua/archive/v1.0.tar.gz
sudo mkdir -p /go/api7-manager-api/dag-to-lua/
tar -zxvf v1.0.tar.gz
sudo mv ./dag-to-lua-1.0/lib/* /go/api7-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: run test
working-directory: ./api
Expand Down
41 changes: 27 additions & 14 deletions .github/workflows/api_cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ jobs:
env:
ALLOW_NONE_AUTHENTICATION: yes

apisix:
image: johz/apisix:v1.4-github-action
env:
APISIX_ETCD_HOST: http://etcd:2379
ports:
- 9080:9080

mysql:
image: mysql:8.0
env:
Expand All @@ -37,19 +30,39 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: run apisix
run: |
network=$(docker network ls | grep github_network | awk '{print $2}')
docker run --name apisix -d -p 9080:9080 \
-v ${{ github.workspace }}/.github/apisix-config.yaml:/usr/local/apisix/conf/config.yaml \
--network "$network" --network-alias apisix \
apache/apisix:dev
sleep 5
docker logs apisix

- name: setting up database
run: |
mysql -h 127.0.0.1 --port 3306 -u root -p123456 < ./api/script/db/schema.sql

- name: setup go
uses: actions/setup-go@v1
with:
go-version: '1.13'
- name: ping apisix
run: |
curl 127.0.0.1:9080

- name: get lua lib
run: |
wget https://github.com/api7/dag-to-lua/archive/v1.0.tar.gz
sudo mkdir -p /go/api7-manager-api/dag-to-lua/
tar -zxvf v1.0.tar.gz
sudo mv ./dag-to-lua-1.0/lib/* /go/api7-manager-api/dag-to-lua/

- name: run test
working-directory: ./api
- name: install runtime
run: |
go test ./...
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

- uses: Azure/docker-login@v1
with:
Expand Down
11 changes: 10 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,13 @@ MIT licenses
The following components are provided under the MIT License. See project link for details.
The text of each license is also included at licenses/LICENSE-[project].txt.

files from ant-design-pro: https://github.com/ant-design/ant-design-pro MIT
files from ant-design-pro: https://github.com/ant-design/ant-design-pro MIT

========================================================================
Apache 2.0 licenses
========================================================================

The following components are provided under the Apache 2.0 License. See project link for details.
The text of each license is also included at licenses/LICENSE-[project].txt.

files from dag-to-lua: https://github.com/api7/dag-to-lua Apache 2.0
22 changes: 14 additions & 8 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,37 @@ FROM golang:1.13.8 AS build-env

WORKDIR /go/src/github.com/apisix/manager-api
COPY . .
RUN mkdir /root/manager-api \
RUN mkdir /go/manager-api \
&& go env -w GOPROXY=https://goproxy.io,direct \
&& export GOPROXY=https://goproxy.io \
&& go build -o /root/manager-api/manager-api \
&& mv /go/src/github.com/apisix/manager-api/build.sh /root/manager-api/ \
&& mv /go/src/github.com/apisix/manager-api/conf/conf_preview.json /root/manager-api/conf.json \
&& go build -o /go/manager-api/manager-api \
&& mv /go/src/github.com/apisix/manager-api/build.sh /go/manager-api/ \
&& mv /go/src/github.com/apisix/manager-api/conf/conf_preview.json /go/manager-api/conf.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.0.tar.gz \
&& tar -zxvf v1.0.tar.gz \
&& mkdir /go/manager-api/dag-to-lua \
&& mv ./dag-to-lua-1.0/lib/* /go/manager-api/dag-to-lua/

FROM alpine:3.11

RUN mkdir /root/manager-api \
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/*

RUN apk add lua5.1

WORKDIR /root/manager-api
COPY --from=build-env /root/manager-api/* /root/manager-api/
WORKDIR /go/manager-api
COPY --from=build-env /go/manager-api/ /go/manager-api/
COPY --from=build-env /usr/share/zoneinfo/Hongkong /etc/localtime
EXPOSE 8080
RUN chmod +x ./build.sh
CMD ["/bin/ash", "-c", "/root/manager-api/build.sh"]
CMD ["/bin/ash", "-c", "/go/manager-api/build.sh"]
2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ sed -i -e "s%#syslogAddress#%`echo $SYSLOG_HOST`%g" ${pwd}/conf.json
sed -i -e "s%#apisixBaseUrl#%`echo $APISIX_BASE_URL`%g" ${pwd}/conf.json
sed -i -e "s%#apisixApiKey#%`echo $APISIX_API_KEY`%g" ${pwd}/conf.json

cd /root/manager-api
cd /go/manager-api
exec ./manager-api

16 changes: 8 additions & 8 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const PROD = "prod"
const BETA = "beta"
const DEV = "dev"
const LOCAL = "local"
const confPath = "/root/manager-api/conf.json"
const confPath = "/go/manager-api/conf.json"
const RequestId = "requestId"

var (
Expand Down Expand Up @@ -81,10 +81,10 @@ type user struct {
}

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

var UserList = make(map[string]user, 1)
Expand Down Expand Up @@ -129,12 +129,12 @@ func initAuthentication() {
userList := configuration.Get("authentication.user").Array()

// create user list
for _, item := range userList{
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()
AuthenticationConfig.Session.ExpireTime = configuration.Get("authentication.session.expireTime").Uint()
AuthenticationConfig.Session.Secret = configuration.Get("authentication.session.secret").String()
AuthenticationConfig.Session.ExpireTime = configuration.Get("authentication.session.expireTime").Uint()
}
}
6 changes: 6 additions & 0 deletions api/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ func findRoute(c *gin.Context) {
return
}
result.Name = route.Name
var script map[string]interface{}
if err = json.Unmarshal([]byte(route.Script), &script); err != nil {
script = map[string]interface{}{}
}
result.Script = script

resp, _ := json.Marshal(result)
c.Data(http.StatusOK, service.ContentType, resp)
}
Expand Down
1 change: 1 addition & 0 deletions api/script/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE `routes` (
`priority` int NOT NULL DEFAULT 0,
`state` int NOT NULL DEFAULT 1, -- 1-normal 0-disable
`content` text,
`script` text,
`content_admin_api` text,
`create_time` bigint(20),
`update_time` bigint(20),
Expand Down
41 changes: 41 additions & 0 deletions api/service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package service
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"time"

"github.com/apisix/manager-api/conf"
Expand Down Expand Up @@ -75,6 +77,11 @@ func (rd *Route) Parse(r *RouteRequest, arr *ApisixRouteRequest) error {
} else {
rd.Content = string(content)
}
if script, err := json.Marshal(r.Script); err != nil {
return err
} else {
rd.Script = string(script)
}
timestamp := time.Now().Unix()
rd.CreateTime = timestamp
rd.Priority = r.Priority
Expand Down Expand Up @@ -172,6 +179,7 @@ type RouteRequest struct {
UpstreamPath *UpstreamPath `json:"upstream_path,omitempty"`
UpstreamHeader map[string]string `json:"upstream_header,omitempty"`
Plugins map[string]interface{} `json:"plugins"`
Script map[string]interface{} `json:"script"`
}

func (r *ApisixRouteResponse) Parse() (*RouteRequest, error) {
Expand Down Expand Up @@ -361,6 +369,7 @@ type ApisixRouteRequest struct {
Upstream *Upstream `json:"upstream,omitempty"`
UpstreamId string `json:"upstream_id,omitempty"`
Plugins map[string]interface{} `json:"plugins,omitempty"`
Script string `json:"script,omitempty"`
//Name string `json:"name"`
}

Expand Down Expand Up @@ -399,6 +408,7 @@ type Route struct {
UpstreamId string `json:"upstream_id"`
Priority int64 `json:"priority"`
Content string `json:"content"`
Script string `json:"script"`
ContentAdminApi string `json:"content_admin_api"`
}

Expand Down Expand Up @@ -513,9 +523,40 @@ func ToApisixRequest(routeRequest *RouteRequest) *ApisixRouteRequest {
arr.Plugins = nil
}

if routeRequest.Script != nil {
arr.Script, _ = generateLuaCode(routeRequest.Script)
}

return arr
}

func generateLuaCode(script map[string]interface{}) (string, error) {
scriptString, err := json.Marshal(script)
if err != nil {
return "", err
}

cmd := exec.Command("sh", "-c",
"cd /go/manager-api/dag-to-lua/ && lua cli.lua "+
"'"+string(scriptString)+"'")

logger.Info("generate conf:", string(scriptString))

stdout, _ := cmd.StdoutPipe()
defer stdout.Close()
if err := cmd.Start(); err != nil {
logger.Info("generate err:", err)
return "", err
}

result, _ := ioutil.ReadAll(stdout)
resData := string(result)

logger.Info("generated code:", resData)

return resData, nil
}

func ToRoute(routeRequest *RouteRequest,
arr *ApisixRouteRequest,
u4 uuid.UUID,
Expand Down
Loading