Skip to content

Commit

Permalink
Merge branch 'master' into add-e2e-test-smoketest-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis authored Dec 28, 2020
2 parents 81ee187 + 47e5345 commit 352083b
Show file tree
Hide file tree
Showing 122 changed files with 4,202 additions and 1,485 deletions.
1 change: 0 additions & 1 deletion .actions/openwhisk-utilities
Submodule openwhisk-utilities deleted from 8636f9
2 changes: 1 addition & 1 deletion .github/workflows/backend-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
- name: run test
working-directory: ./api/test/e2e
run: go test
run: go test -v
2 changes: 1 addition & 1 deletion .github/workflows/backend-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
export GO111MOUDULE=on
export APISIX_CONF_PATH=$PWD/conf
sed -i 's/9000/8088/' conf/conf.yaml
go build -o ./manager-api
go build -o ./manager-api ./cmd/manager
./manager-api > ./api.log 2>&1 &
sleep 2
cat ./api.log
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frontend-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Start manager-api
working-directory: ./api
run: nohup go run . &
run: nohup go run ./cmd/manager &

- name: Install front-end dependencies
run: yarn install
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/spellchecker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: spellchecker
on:
pull_request:
branches:
- master
jobs:
misspell:
name: runner/misspell
runs-on: ubuntu-latest
steps:
- name: Check out code.
uses: actions/checkout@v1
- name: Install
run: |
wget -O - -q https://git.io/misspell | sh -s -- -b .
- name: Misspell
run: |
find . -type f -maxdepth 1 | xargs ./misspell -error
find . -name "*.go" -type f | xargs ./misspell -error
find docs -type f | xargs ./misspell -error
find web/src web/cypress -type f | xargs ./misspell -error
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RUN wget https://github.com/api7/dag-to-lua/archive/v1.1.tar.gz -O /tmp/v1.1.tar
RUN if [ "$ENABLE_PROXY" = "true" ] ; then go env -w GOPROXY=https://goproxy.io,direct ; fi

RUN go env -w GO111MODULE=on \
&& CGO_ENABLED=0 go build -o ../output/manager-api .
&& CGO_ENABLED=0 go build -o ../output/manager-api ./cmd/manager

FROM node:14-alpine as fe-builder

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ api-test: api-default
### api-run: Run the manager-api
.PHONY: api-run
api-run: api-default
cd api/ && go run .
cd api/ && go run ./cmd/manager

### api-stop: Stop the manager-api
api-stop:
Expand Down
23 changes: 23 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ This is a backend project which the dashboard depends on, implemented by Golang.
## Installation

[Please refer to the doc](../README.md)

## Project structure

```text
├── README.md
├── VERSION
├── build-tools
├── build.sh
├── cmd
├── conf
├── entry.sh
├── go.mod
├── go.sum
├── internal
├── run.sh
└── test
```

1. The `cmd` directory is the project entrance.
2. The `internal` directory contains the main logic of manager-api.
3. The `conf` directory contains the default configuration file.
4. The `test` directory contains E2E test cases.

2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [[ ! -f "dag-to-lua-1.1/lib/dag-to-lua.lua" ]]; then
fi

# build
cd ./api && go build -o ../output/manager-api -ldflags "-X main.Version=${VERSION}" . && cd ..
cd ./api && go build -o ../output/manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION}" ./cmd/manager && cd ..

cp ./api/conf/schema.json ./output/conf/schema.json
cp ./api/conf/conf.yaml ./output/conf/conf.yaml
Expand Down
30 changes: 30 additions & 0 deletions api/cmd/manager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/
package main

import (
"fmt"
"os"

"github.com/apisix/manager-api/cmd"
)

func main() {
if err := cmd.NewManagerAPICommand().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}
116 changes: 116 additions & 0 deletions api/cmd/managerapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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.
*/
package cmd

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/apisix/manager-api/internal"
"github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/core/storage"
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/log"
"github.com/apisix/manager-api/internal/utils"
"github.com/shiningrush/droplet"
"github.com/spf13/cobra"
)

var Version string

func printInfo() {
fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Version", Version)
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, conf.ServerPort)
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Loglevel", conf.ErrorLogLevel)
fmt.Fprintf(os.Stdout, "%-8s: %s\n\n", "Logfile", conf.ErrorLogPath)
}

// NewManagerAPICommand creates the manager-api command.
func NewManagerAPICommand() *cobra.Command {
cmd := &cobra.Command{
Use: "manager-api [flags]",
Short: "APISIX Manager API",
RunE: func(cmd *cobra.Command, args []string) error {
conf.InitConf()
droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware {
var newMws []droplet.Middleware
// default middleware order: resp_reshape, auto_input, traffic_log
// We should put err_transform at second to catch all error
newMws = append(newMws, mws[0], &handler.ErrorTransformMiddleware{})
newMws = append(newMws, mws[1:]...)
return newMws
}

if err := storage.InitETCDClient(conf.ETCDConfig); err != nil {
log.Errorf("init etcd client fail: %w", err)
panic(err)
}
if err := store.InitStores(); err != nil {
log.Errorf("init stores fail: %w", err)
panic(err)
}
// routes
r := internal.SetUpRouter()
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.Infof("The Manager API is listening on %s", addr)

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
utils.CloseAll()
log.Fatalf("listen and serv fail: %s", err)
}
}()

printInfo()

sig := <-quit
log.Infof("The Manager API server receive %s and start shutting down", sig.String())

ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()

if err := s.Shutdown(ctx); err != nil {
log.Errorf("Shutting down server error: %s", err)
}

log.Infof("The Manager API server exited")

utils.CloseAll()
return nil
},
}

cmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", ".", "current work directory")
return cmd
}
Loading

0 comments on commit 352083b

Please sign in to comment.