Refactor network handling and endpoint retrieval; deprecate unused commands in startup
This commit is contained in:
parent
8388b78a0e
commit
6b6edab97c
3 changed files with 95 additions and 37 deletions
15
cmd/root.go
15
cmd/root.go
|
|
@ -7,7 +7,6 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/yaoapp/kun/exception"
|
"github.com/yaoapp/kun/exception"
|
||||||
"github.com/yaoapp/yao/cmd/studio"
|
|
||||||
"github.com/yaoapp/yao/cmd/sui"
|
"github.com/yaoapp/yao/cmd/sui"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/pack"
|
"github.com/yaoapp/yao/pack"
|
||||||
|
|
@ -132,7 +131,7 @@ var suiCmd = &cobra.Command{
|
||||||
// Command initialize
|
// Command initialize
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
studioCmd.AddCommand(studio.RunCmd)
|
// studioCmd.AddCommand(studio.RunCmd)
|
||||||
|
|
||||||
// Sui
|
// Sui
|
||||||
suiCmd.AddCommand(sui.WatchCmd)
|
suiCmd.AddCommand(sui.WatchCmd)
|
||||||
|
|
@ -145,15 +144,15 @@ func init() {
|
||||||
inspectCmd,
|
inspectCmd,
|
||||||
startCmd,
|
startCmd,
|
||||||
runCmd,
|
runCmd,
|
||||||
getCmd,
|
// getCmd,
|
||||||
dumpCmd,
|
// dumpCmd,
|
||||||
restoreCmd,
|
// restoreCmd,
|
||||||
// socketCmd,
|
// socketCmd,
|
||||||
// websocketCmd,
|
// websocketCmd,
|
||||||
packCmd,
|
// packCmd,
|
||||||
studioCmd,
|
// studioCmd,
|
||||||
suiCmd,
|
suiCmd,
|
||||||
upgradeCmd,
|
// upgradeCmd,
|
||||||
)
|
)
|
||||||
// rootCmd.SetHelpCommand(helpCmd)
|
// rootCmd.SetHelpCommand(helpCmd)
|
||||||
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
|
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
|
||||||
|
|
|
||||||
51
cmd/start.go
51
cmd/start.go
|
|
@ -116,12 +116,6 @@ var startCmd = &cobra.Command{
|
||||||
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
||||||
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
||||||
|
|
||||||
root, _ := adminRoot()
|
|
||||||
urls := []string{fmt.Sprintf("http://%s:%s", host, port)}
|
|
||||||
if host == "0.0.0.0" {
|
|
||||||
urls, _ = setup.URLs(config.Conf)
|
|
||||||
}
|
|
||||||
|
|
||||||
// print the messages under the development mode
|
// print the messages under the development mode
|
||||||
if mode == "development" {
|
if mode == "development" {
|
||||||
|
|
||||||
|
|
@ -153,12 +147,45 @@ var startCmd = &cobra.Command{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, url := range urls {
|
root, _ := adminRoot()
|
||||||
fmt.Println(color.CyanString("\n%s", url))
|
endpoints := []setup.Endpoint{{URL: fmt.Sprintf("http://%s:%s", "127.0.0.1", port), Interface: "localhost"}}
|
||||||
|
switch host {
|
||||||
|
case "0.0.0.0":
|
||||||
|
// All interfaces
|
||||||
|
if values, err := setup.Endpoints(config.Conf); err == nil {
|
||||||
|
endpoints = append(endpoints, values...)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case "127.0.0.1":
|
||||||
|
// Localhost only
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
// Filter by the host IP
|
||||||
|
matched := false
|
||||||
|
endpoints = []setup.Endpoint{}
|
||||||
|
if values, err := setup.Endpoints(config.Conf); err == nil {
|
||||||
|
for _, value := range values {
|
||||||
|
if strings.HasPrefix(value.URL, fmt.Sprintf("http://%s:", host)) {
|
||||||
|
endpoints = append(endpoints, value)
|
||||||
|
matched = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
fmt.Println(color.RedString(L("Host %s not found"), host))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||||
|
fmt.Println(color.WhiteString(L("Access Points")))
|
||||||
|
fmt.Println(color.WhiteString("---------------------------------"))
|
||||||
|
for _, endpoint := range endpoints {
|
||||||
|
fmt.Println(color.CyanString("\n%s", endpoint.Interface))
|
||||||
fmt.Println(color.WhiteString("--------------------------"))
|
fmt.Println(color.WhiteString("--------------------------"))
|
||||||
fmt.Println(color.WhiteString(L("Website")), color.GreenString(" %s", url))
|
fmt.Println(color.WhiteString(L("Website")), color.GreenString(" %s", endpoint.URL))
|
||||||
fmt.Println(color.WhiteString(L("Admin")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
|
fmt.Println(color.WhiteString(L("Admin")), color.GreenString(" %s/%s/login/admin", endpoint.URL, strings.Trim(root, "/")))
|
||||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
|
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", endpoint.URL))
|
||||||
}
|
}
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
|
||||||
|
|
@ -419,7 +446,7 @@ func printApis(silent bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||||
fmt.Println(color.WhiteString(L("API List")))
|
fmt.Println(color.WhiteString(L("APIs List")))
|
||||||
fmt.Println(color.WhiteString("---------------------------------"))
|
fmt.Println(color.WhiteString("---------------------------------"))
|
||||||
|
|
||||||
for _, api := range api.APIs { // API信息
|
for _, api := range api.APIs { // API信息
|
||||||
|
|
|
||||||
|
|
@ -9,19 +9,23 @@ import (
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// URLs get admin url
|
// Endpoints get endpoints
|
||||||
func URLs(cfg config.Config) ([]string, error) {
|
func Endpoints(cfg config.Config) ([]Endpoint, error) {
|
||||||
|
networks, err := getNetworks()
|
||||||
ips, err := Ips()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range ips {
|
var endpoints []Endpoint
|
||||||
ips[i] = fmt.Sprintf("http://%s:%d", ips[i], cfg.Port)
|
for _, network := range networks {
|
||||||
|
endpoint := Endpoint{
|
||||||
|
URL: fmt.Sprintf("http://%s:%d", network.IPv4, cfg.Port),
|
||||||
|
Interface: network.Interface,
|
||||||
|
}
|
||||||
|
endpoints = append(endpoints, endpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ips, nil
|
return endpoints, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printError(message string, args ...interface{}) {
|
func printError(message string, args ...interface{}) {
|
||||||
|
|
@ -33,21 +37,49 @@ func printInfo(message string, args ...interface{}) {
|
||||||
fmt.Println(color.GreenString(message, args...))
|
fmt.Println(color.GreenString(message, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ips get the local ip list
|
func getNetworks() ([]Network, error) {
|
||||||
func Ips() ([]string, error) {
|
interfaces, err := net.Interfaces()
|
||||||
addrs, err := net.InterfaceAddrs()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
iplist := []string{"127.0.0.1"}
|
var networks []Network
|
||||||
for _, address := range addrs {
|
for _, iface := range interfaces {
|
||||||
// check the address type and if it is not a loopback the display it
|
// 跳过 loopback 接口(如 lo0)
|
||||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||||
if ipnet.IP.To4() != nil {
|
continue
|
||||||
iplist = append(iplist, ipnet.IP.String())
|
}
|
||||||
|
|
||||||
|
// 获取每个接口的地址信息
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 过滤只获取 IPv4 地址
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
|
||||||
|
// 将网卡名称和 IPv4 地址存储到 Network 结构体中
|
||||||
|
network := Network{
|
||||||
|
IPv4: ipnet.IP.String(),
|
||||||
|
Interface: iface.Name,
|
||||||
|
}
|
||||||
|
// 添加到结果切片
|
||||||
|
networks = append(networks, network)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return iplist, nil
|
return networks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Network network
|
||||||
|
type Network struct {
|
||||||
|
IPv4 string
|
||||||
|
Interface string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint endpoint
|
||||||
|
type Endpoint struct {
|
||||||
|
URL string
|
||||||
|
Interface string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue