Skip to content

Commit

Permalink
Merge branch 'master' into feat-route-group-1
Browse files Browse the repository at this point in the history
  • Loading branch information
juzhiyuan authored Dec 25, 2020
2 parents f15ed3f + b3819c5 commit 97af52f
Show file tree
Hide file tree
Showing 16 changed files with 861 additions and 165 deletions.
1 change: 0 additions & 1 deletion .actions/openwhisk-utilities
Submodule openwhisk-utilities deleted from 8636f9
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 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}" . && cd ..

cp ./api/conf/schema.json ./output/conf/schema.json
cp ./api/conf/conf.yaml ./output/conf/conf.yaml
Expand Down
118 changes: 118 additions & 0 deletions api/cmd/managerapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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/shiningrush/droplet"
"github.com/spf13/cobra"

"github.com/apisix/manager-api/conf"
"github.com/apisix/manager-api/internal"
"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/utils"
"github.com/apisix/manager-api/log"
)

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
}
5 changes: 1 addition & 4 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package conf

import (
"flag"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -105,13 +104,11 @@ type Config struct {
func init() {
InitConf()
}

func InitConf() {
//go test
if workDir := os.Getenv("APISIX_API_WORKDIR"); workDir != "" {
WorkDir = workDir
} else {
flag.StringVar(&WorkDir, "p", ".", "current work dir")
flag.Parse()
}

setConf()
Expand Down
12 changes: 12 additions & 0 deletions api/filter/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ func parseCert(crt, key string) ([]string, error) {
}

func handleSpecialField(resource string, reqBody []byte) ([]byte, error) {
var bodyMap map[string]interface{}
err := json.Unmarshal(reqBody, &bodyMap)
if err != nil {
return reqBody, fmt.Errorf("read request body failed: %s", err)
}
if _, ok := bodyMap["create_time"]; ok {
return reqBody, errors.New("we don't accept create_time from client")
}
if _, ok := bodyMap["update_time"]; ok {
return reqBody, errors.New("we don't accept update_time from client")
}

// remove script, because it's a map, and need to be parsed into lua code
if resource == "routes" {
var route map[string]interface{}
Expand Down
19 changes: 15 additions & 4 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@ go 1.13

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

replace github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.5

require (
github.com/api7/go-jsonpatch v0.0.0-20180223123257-a8710867776e
github.com/cameront/go-jsonpatch v0.0.0-20180223123257-a8710867776e // indirect
github.com/coreos/bbolt v0.0.0-00010101000000-000000000000 // indirect
github.com/coreos/etcd v3.3.25+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/evanphx/json-patch/v5 v5.1.0
github.com/gin-contrib/pprof v1.3.0
github.com/gin-contrib/sessions v0.0.3
github.com/gin-contrib/static v0.0.0-20200916080430-d45d9a37d28e
github.com/gin-gonic/gin v1.6.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.3 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/prometheus/client_golang v1.8.0 // indirect
github.com/satori/go.uuid v1.2.0
github.com/shiningrush/droplet v0.2.3
github.com/shiningrush/droplet/wrapper/gin v0.2.0
github.com/sirupsen/logrus v1.7.0
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/sony/sonyflake v1.0.0
github.com/spf13/cobra v0.0.3
github.com/stretchr/testify v1.6.1
github.com/tidwall/gjson v1.6.1
github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da
go.etcd.io/etcd v3.3.25+incompatible
go.uber.org/zap v1.16.0
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
gopkg.in/yaml.v2 v2.3.0
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading

0 comments on commit 97af52f

Please sign in to comment.