-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add admin api server skeleton (#330)
- Loading branch information
Showing
17 changed files
with
809 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.