Skip to content

Commit

Permalink
chore: cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers committed Dec 11, 2020
1 parent 4196844 commit a4f1c51
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 85 deletions.
89 changes: 89 additions & 0 deletions api/cmd/managerapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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"
)

// 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.Init()
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)
}
}()

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
}
6 changes: 1 addition & 5 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 @@ -101,13 +100,10 @@ type Config struct {
Authentication Authentication
}

func init() {
func Init() {
//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,31 +4,42 @@ 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/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
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 a4f1c51

Please sign in to comment.