Skip to content

Commit

Permalink
feat: add admin api server skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii committed Aug 1, 2022
1 parent ddbdd43 commit 6dd4f81
Show file tree
Hide file tree
Showing 17 changed files with 809 additions and 48 deletions.
91 changes: 91 additions & 0 deletions cmd/admin/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 admin

import (
"context"
"fmt"
"os"
"path/filepath"
)

import (
"github.com/spf13/cobra"
)

import (
"github.com/arana-db/arana/cmd/cmds"
"github.com/arana-db/arana/pkg/admin"
_ "github.com/arana-db/arana/pkg/admin/router"
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/constants"
"github.com/arana-db/arana/pkg/util/log"
)

const (
_keyPort = "port"
_defaultPort = 8080
)

func init() {
cmd := &cobra.Command{
Use: "admin",
Short: "admin",
Example: "arana admin -c bootstrap.yaml -p 8080",
RunE: run,
}
cmd.PersistentFlags().
StringP(constants.ConfigPathKey, "c", os.Getenv(constants.EnvBootstrapPath), "bootstrap configuration file path")
cmd.PersistentFlags().
Uint16P(_keyPort, "p", _defaultPort, "listen port")

cmds.Handle(func(root *cobra.Command) {
root.AddCommand(cmd)
})
}

func Run(bootstrapPath string, addr string) error {
discovery := boot.NewDiscovery(bootstrapPath)
if err := discovery.Init(context.Background()); err != nil {
log.Fatal("start admin api server failed: %v", err)
return err
}
adminServer := admin.New(discovery)
return adminServer.Listen(addr)
}

func run(cmd *cobra.Command, args []string) error {
_ = args
btPath, _ := cmd.PersistentFlags().GetString(constants.ConfigPathKey)
port, _ := cmd.PersistentFlags().GetUint16("port")
if len(btPath) < 1 {
// search bootstrap yaml
for _, path := range constants.GetConfigSearchPathList() {
btPath = filepath.Join(path, "bootstrap.yaml")
if _, err := os.Stat(btPath); err == nil {
break
}
btPath = filepath.Join(path, "bootstrap.yml")
if _, err := os.Stat(btPath); err == nil {
break
}
}
}

return Run(btPath, fmt.Sprintf(":%d", port))
}
8 changes: 4 additions & 4 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func Run(bootstrapConfigPath string) {
// print slogan
fmt.Printf("\033[92m%s\033[0m\n", slogan) // 92m: light green

provider := boot.NewProvider(bootstrapConfigPath)
if err := boot.Boot(context.Background(), provider); err != nil {
discovery := boot.NewDiscovery(bootstrapConfigPath)
if err := boot.Boot(context.Background(), discovery); err != nil {
log.Fatal("start failed: %v", err)
return
}

filters, err := provider.ListFilters(context.Background())
filters, err := discovery.ListFilters(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
Expand All @@ -99,7 +99,7 @@ func Run(bootstrapConfigPath string) {

propeller := server.NewServer()

listenersConf, err := provider.ListListeners(context.Background())
listenersConf, err := discovery.ListListeners(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func init() {
func Run(cmd *cobra.Command, args []string) {
_, _ = cmd, args

provider := boot.NewProvider(importBootConfPath)
if err := provider.Init(context.Background()); err != nil {
discovery := boot.NewDiscovery(importBootConfPath)
if err := discovery.Init(context.Background()); err != nil {
log.Fatal("init failed: %+v", err)
return
}
Expand All @@ -73,7 +73,7 @@ func Run(cmd *cobra.Command, args []string) {
return
}

c := provider.GetConfigCenter()
c := discovery.GetConfigCenter()

if err := c.ImportConfiguration(cfg); err != nil {
log.Fatal("persist config to config.store failed: %+v", err)
Expand Down
11 changes: 11 additions & 0 deletions example/admin_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/arana-db/arana/cmd/admin"
"github.com/arana-db/arana/testdata"
)

func main() {
bootstrap := testdata.Path("../conf/bootstrap.yaml")
_ = admin.Run(bootstrap, ":8080")
}
24 changes: 15 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ require (
github.com/cespare/xxhash/v2 v2.1.2
github.com/dop251/goja v0.0.0-20220422102209-3faab1d8f20e
github.com/dubbogo/gost v1.12.3
github.com/go-playground/validator/v10 v10.10.1
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/mock v1.5.0
github.com/hashicorp/golang-lru v0.5.4
Expand All @@ -19,7 +20,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.7.2
github.com/testcontainers/testcontainers-go v0.12.0
github.com/tidwall/gjson v1.14.0
go.etcd.io/etcd/api/v3 v3.5.1
Expand All @@ -32,7 +33,7 @@ require (
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -55,12 +56,14 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -74,20 +77,22 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -100,6 +105,7 @@ require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0 // indirect
Expand All @@ -108,13 +114,13 @@ require (
go.etcd.io/etcd/raft/v3 v3.5.0-alpha.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220725212005-46097bf591d3 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 // indirect
google.golang.org/grpc v1.42.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 6dd4f81

Please sign in to comment.