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: introduce corba as the cli scafford #773

Merged
merged 5 commits into from
Dec 25, 2020
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
1 change: 0 additions & 1 deletion .actions/openwhisk-utilities
Submodule openwhisk-utilities deleted from 8636f9
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
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